<?php

namespace Modules\API\APIInterne\OxaMobility\Controller;

use GuzzleHttp\Psr7\Response;
use Modules\API\APIInterne\OxaMobility\Outil\DocAtt;

// @phpcs:ignore
class DOCS_RATTACHESController extends AbsApiController
{
    /**
     * @return void|Response
     */
    public function init()
    {
        if (($_SESSION["Session_Login"] ?? '') == '') {
            $trad = $this->getTranslationService();

            $return['Statut'] = 'E';
            $return['MsgErreur'] = $trad->t('Accès refusé.');
            return $this->createApiResponse($return);
        }
    }

    // @phpcs:ignore
    public function ACTIONSAction() :Response
    {
        $body = $this->request->getMetaBody();
        $return     = [];
        $trad       = $this->contenaire->get(\Helpers\Trad\ITrad::class);

        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');
        } elseif (empty($body['CodeTable'])) {
            $return['Statut']    = 'E';
            $return['MsgErreur'] = $trad->t('Le code table est obligatoire');
        } elseif (empty($body['NumDoc'])) {
            $return['Statut']    = 'E';
            $return['MsgErreur'] = $trad->t('Le numéro de document est obligatoire');
        } else {
            $return['Statut']    = 'O';
            $return['MsgErreur'] = '';

            $service = new DocAtt($trad, $this->pdo);
            $service->hydrateDocumentFromMobility($return, $body, [
                'CodeDossierRacine',
                'NomFichier',
                'NomFichierAff',
            ]);

            if ($service->hasError()) {
                $return['Statut'] = 'E';
            }
        }

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