<?php

namespace Modules\API\APIInterne\OxaMobility\Controller;

use Helpers\Fichier\Fichier;
use Helpers\Log\LogSysteme;

use Helpers\Trad\Trad;
use Modules\API\APIInterne\OxaMobility\Repository\MobilitySession;
use Skeleton\Controller\Controller;

class SESSIONController extends Controller
{

    
    /**
     * init
     *
     * @return void
     */
    public function init() : void
    {
        /** A deplacer dans absAPIcontroller plus tard car pas top */
        $trad = $this->getTranslationService();
        if ($trad instanceof Trad) {
            $trad->setLang($_SESSION['Session_Langue']);
        }
    }
    
    /**
     * INIT_PAR_SOCIETEAction
     *
     * @param  array<mixed> $body
     * @return string|false
     */
    // phpcs:ignore
    public function INIT_PAR_SOCIETEAction(array $body = [])
    {
        $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'];
        }

        $retourJSON['MPROC_Out'] = $return;
        header('content-type:application/json');
        if ($_SESSION['_debug'] == true) {
            Fichier::createFichierTmp(LogSysteme::getServerName().'.'.str_replace('|', '_', $body['Action']) . '_Out.json', (string)json_encode($retourJSON));
        }
        return json_encode($retourJSON);
    }
}
