<?php

namespace Modules\API\APIInterne\OxaMobility\Controller;

use Helpers\Fichier\Fichier;
use Helpers\Log\LogSysteme;
use Helpers\Trad\Trad;
use Modules\Ajax\Repository\Listes;
use Modules\API\APIInterne\OxaMobility\Outil\DocAtt;
use Modules\GlobalApplication\Repository\DocumentsRattaches;
use Skeleton\Controller\Controller;

// @phpcs:ignore
class DOCS_RATTACHESController extends Controller
{
    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']);
        }
    }

    /**
     * @param array<mixed> $body
     * @param array<mixed> $jwtPayload
     * @param bool $json
     * @return false|string
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    // @phpcs:ignore
    public function ACTIONSAction($body = [], $jwtPayload = [], $json = true)
    {
        $return     = [];
        $trad       = $this->contenaire->get('translationService');

        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';
            }
        }

        // @phpstan-ignore-next-line faux positif
        if ($json) {
            $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);
        }

        $xml = new \SimpleXMLElement('<MPROC_Out/>');
        header('Content-Type: application/xml; charset=utf-8');
        // @phpstan-ignore-next-line
        \Helpers\Outil\Outils::array_to_xml($return, $xml, $body['Action'], ($arrayString ?? ''));
        $domxml = new \DOMDocument('1.0');
        $domxml->preserveWhiteSpace = false;
        $domxml->formatOutput = true;
        // @phpstan-ignore-next-line
        $domxml->loadXML($xml->asXML());
        return $domxml->saveXML();
    }
}
