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

use Entity\Model\AoEntet;
use Entity\Model\AoExcFofo;
use Helpers\Fichier\Fichier;
use Helpers\Session\ParseBase1;
use Modules\API\APIInterne\Magic\Controller\AbstractController;
use GuzzleHttp\Psr7\Response;
use Modules\Consultation\Controller\ImportExcelController;
use Modules\Consultation\Service\EnvoieExcelService;
use Modules\Consultation\Service\ImportExcelService;

class ApiConsultationController extends AbstractController
{

    public function importRepExcelAction() : Response
    {

        $idAO = $this->request->getAttribute('idAO');
        $post = $this->request->getParsedBody();

        $validator = $this->createValidator();

        $file = $this->request->getUploadedFiles()['import'] ?? null;

        if ($file) {
            $file_name = $file->getClientFilename();
            $fileSize = $file->getSize();
        } else {
            $file_name = null;
            $fileSize = 0;
        }
        $validator->fileCheck("Excel", $file_name??'', [], '', $fileSize);

        if (! $validator->isValid()) {
            return new Response(400, [], $validator->getError());
        }
        
        $newPath = Fichier::UploadDocAtt($file_name);
        $filePath = $newPath["racine"].$newPath["new_name"];
        $file->moveTo($filePath);

        $importService = new ImportExcelService(
            $this->pdo,
            $this->getTranslationService(),
            $this->renderer
        );

        return $importService->importConsultation(
            $idAO,
            $filePath,
            boolval($post['confirm'] ?? false),
            isset($post['bddart99_ignore']) ? boolval($post['bddart99_ignore']) : null
        );
    }

    /**
     * @return Response
     */
    public function exportExcelAction()
    {
        $idAO       = $this->request->getAttribute('idAO');
        $idFrs      = $this->request->getAttribute('idFrs');
        $fileName   = $this->request->getAttribute('fileName');

        $envoieExcelService = new EnvoieExcelService($this->pdo, $this->getTranslationService(), $this->router);

        $consultation = (new AoEntet($this->pdo))->find($idAO);
        $AoExcFofo = new AoExcFofo($this->pdo);
        $paramExcelByfrs = $AoExcFofo->find(['SOCIETE' => $consultation['CODE_STE'], 'CTRE' => $consultation['EMETTEUR'], 'CODE_FOU' => $idFrs]);
        $paramExcelDefaut = $AoExcFofo->getForm($consultation);

        if ($paramExcelByfrs) {
            $typeFormatExcel    = $paramExcelByfrs['CODEFORM']??'';
            $versionFormat      = $paramExcelByfrs['VERSFORM']??'';
        } else {
            $typeFormatExcel    = $paramExcelDefaut['CODEFORM']??'';
            $versionFormat      = $paramExcelDefaut['VERSFORM']??'';
        }

        $excel = [
            'CODE_FOU'          => $idFrs,
            'TYPE_FG'           => 'F',
            'typeFormatExcel'   => $typeFormatExcel,
            'versionFormat'     => $versionFormat,
            'parMail'           => false
        ];

        $file = $envoieExcelService->generationEnvoieExcel(
            $idAO,
            $excel,
            [],
            [],
            null,
            true
        );

        copy((new ParseBase1())->getRep()->tmp.$file, (new ParseBase1())->getRep()->tmp.$fileName);

        return new Response(200);
    }
}
