<?php
namespace Modules\API\APIInterne\OxaMobility\Controller;

use GuzzleHttp\Psr7\Response;
use Entity\Model\Hastutil;

// phpcs:ignore
class PAGE_ProfilController extends AbsApiController
{
    /**
     * @OA\Post(
     *      path="/page_profil/get",
     *      summary="retourne les informations d'un utlisateur",
     *      tags={"Profil"},
     *      @OA\RequestBody(
     *          description="CodeSoc",
     *          required=true,
     *          @OA\MediaType(
     *              mediaType="application/xml",
     *          )
     *      ),
     *      @Oa\Response(
     *          response="200",
     *          description="Succès",
     *          @OA\XmlContent(
     *             type="array",
     *             @OA\items(
     *                  type="string"))
     *       )
     * )
     */
    public function GETAction() :Response
    {
        $body = $this->request->getMetaBody();
        
        $user = new \Entity\Model\Hastutil($this->pdo);
        $user = $user->find($body['Utilisateur']);
  
        $return['Trads']['NOM'     ] = $user['NOM'     ];
        $return['Trads']['FONCTION'] = $user['FONCTION'];
        $return['Trads']['CODE_UO' ] = $user['CODE_UO' ];
        $return['Trads']['TEL'     ] = $user['TEL'     ];
        $return['Trads']['GSM'     ] = $user['GSM'     ];
        $return['Trads']['EMAIL'   ] = $user['EMAIL'   ];
        $return['Trads']['CODE'    ] = $user['CODE_STE'];
        $return['Trads']['SI'      ] = $user['SI'      ];
        $return['Trads']['CDC'     ] = $user['CDC'     ];
        $return['Trads']['SCA'     ] = $user['SCA'     ];
        $return['Trads']['CTR_ADR' ] = $user['CTR_ADR' ];
        $return['Trads']['CTRLIV'  ] = $user['CTRLIV'  ];
     
    
        return $this->createApiResponse($return);
    }

    /**
     * @OA\Post(
     *      path="/page_profil/update",
     *      summary="permet de mettre à jour les informations d'un utilisateur",
     *      tags={"Profil"},
     *      @OA\RequestBody(
     *          description="CodeSoc",
     *          required=true,
     *          @OA\MediaType(
     *              mediaType="application/xml",
     *          )
     *      ),
     *      @Oa\Response(
     *          response="200",
     *          description="Succès",
     *          @OA\XmlContent(
     *             type="array",
     *             @OA\items(
     *                  type="string"))
     *       )
     * )
     */
    public function UPDATEAction() :Response
    {
        $body = $this->request->getMetaBody();
        $user = new \Entity\Model\Hastutil($this->pdo);
        $userUpdate = new Hastutil($this->pdo);
        $user = $user->find($body['Utilisateur']);

        // Mise à jours
        $arrayupdate =
        [
            'FONCTION' => is_array($body['Profil']['FONCTION'])? '':$body['Profil']['FONCTION'],
            'TEL'      => is_array($body['Profil']['TEL'])? '':$body['Profil']['TEL'],
            'EMAIL'    => is_array($body['Profil']['EMAIL'])? '':$body['Profil']['EMAIL'],
            'CODE_STE' => is_array($body['Profil']['CODE_STE'])? '':$body['Profil']['CODE_STE'],
            'SI'       => is_array($body['Profil']['SI'])? '':$body['Profil']['SI'],
            'CDC'      => is_array($body['Profil']['CDC'])? '':$body['Profil']['CDC'],
            'SCA'      => is_array($body['Profil']['SCA'])? '':$body['Profil']['SCA'],
            'CTR_ADR'  => is_array($body['Profil']['CTR_ADR'])? '':$body['Profil']['CTR_ADR'],
            'CTRLIV'   => is_array($body['Profil']['CTRLIV'])? '':$body['Profil']['CTRLIV'],
        ];

        $userUpdate->update($arrayupdate, $body['Utilisateur']);
     
        // A voir ici si on récup les info de la base ou non

        $return['Trads']['NOM'      ] = $user['NOM'      ];
        $return['Trads']['FONCTION' ] = $user['FONCTION' ];
        $return['Trads']['CODE_UO'  ] = $user['CODE_UO'  ];
        $return['Trads']['TEL'      ] = $user['TEL'      ];
        $return['Trads']['GSM'      ] = $user['GSM'      ];
        $return['Trads']['EMAIL'    ] = $user['EMAIL'    ];
        $return['Trads']['CODE'     ] = $user['CODE_STE' ];
        $return['Trads']['SI'       ] = $user['SI'       ];
        $return['Trads']['CDC'      ] = $user['CDC'      ];
        $return['Trads']['SCA'      ] = $user['SCA'      ];
        $return['Trads']['CTR_ADR'  ] = $user['CTR_ADR'  ];
        $return['Trads']['CTRLIV'   ] = $user['CTRLIV'   ];
     
        return $this->createApiResponse($return);
    }
}
