<?php

namespace Modules\API\APIInterne\OxaMobility\Controller;

use GuzzleHttp\Psr7\Response;
use Modules\API\APIInterne\OxaMobility\Repository\MobilitySession;

class SESSIONController extends AbsApiController
{
    // phpcs:ignore
    public function INIT_PAR_SOCIETEAction() :Response
    {
        $body = $this->request->getMetaBody();
        $trad = $this->getTranslationService();
        $return = [];

        if (empty($body['CodeSoc'])) {
            $return['Statut'] = 'E';
            $return['MsgErreur'] = $trad->t('Le code société est obligatoire.');
        } elseif (empty($body['Utilisateur'])) {
            $return['Statut'] = 'E';
            $return['MsgErreur'] = $trad->t('Le code utilisateur est obligatoire.');
        } else {
            $user = $body['Utilisateur'];
            $company = $body['CodeSoc'];

            $mobilitySessionRepo = new MobilitySession($this->pdo);
            $data = $mobilitySessionRepo->getCompanyInfos($user, $company);

            $return['Statut'] = 'O';
            $return['SI_CODE'] = $data['SI_CODE'];
            $return['SI_LIB'] = $data['label_si'];
            $return['CDC_CODE'] = $data['CDC_CODE'];
            $return['CDC_LIB'] = $data['label_cdc'];
            $return['SCA_CODE'] = $data['SCA_CODE'];
            $return['SCA_LIB'] = $data['label_sca'];
            $return['CTR_EMET_CODE'] = $data['CTR_EMET_CODE'];
            $return['CTR_EMET_LIB'] = $data['ctr_emet_label'];
            $return['CTR_LIVR_CODE'] = $data['CTR_LIVR_CODE'];
            $return['CTR_LIVR_LIB'] = $data['ctr_livr_label'];
        }

        return $this->createJsonResponse($return);
    }
}
