<?php

namespace Modules\Reception\Controller;

use GuzzleHttp\Psr7\Response;
use Helpers\Parametres\Constante;
use Helpers\Validator\Validator;
use Modules\Reception\Repository\CreationBR;
use Modules\Reception\Service\CreationBRService;
use Skeleton\Controller\Controller;

class ValidatorCreationController extends Controller
{
    /**
     * @var array<mixed>
     */
    protected array $params = [];
    private Validator $validator;
    private CreationBRService $service;
    private CreationBR $repository;

    /**
     * init
     * gestion des droits
     *
     * @return void
     */
    public function init(): void
    {
        $this->service = new CreationBRService($this->pdo, $this->getTranslationService());
        $this->repository = new CreationBR($this->pdo);
        $this->validator = $this->createValidator();

        $params = $this->service->getCreationParam();

        $this->params = $params;
    }

    /**
     * @return Response
     */
    public function saisieAction()
    {
        $post       = $this->request->getParsedBody();
        $section    = $post['section'] ?? '';
        $field      = $post['field'] ?? '';
        $line       = $post['line'] ?? '';
        $batch      = $post['batch'] ?? '';
        $value      = $post['value'];

        if ($section == 'entete') {
            $this->validator->brControleEntete($field, $value, $this->params);
        }

        if ($section == 'ligne') {
            $order = $this->repository->getInfoBrEntete($_SESSION['Session_Login'], 'no_cde')['no_cde'] ?? '';
            $this->service->getTauxMargeBrExcedentaire($order, $this->params);
            if ($field == 'qte_a' || $field == 'qte_r' || $field == 'qte_c' || $field == 'QTE_ANNULE') {
                $lineData = $this->repository->getOxaBrLigne($_SESSION['Session_Login'], $line);
                $this->validator->isFloat(
                    '',
                    $value,
                    ($lineData['NB_DECIM'] == '0'
                        ? 15
                        : (15 - floatval($lineData['NB_DECIM'])) . '.' . $lineData['NB_DECIM'])
                );
            }

            if ($field == 'MNT_REC' || $field == 'MNT_REC_TTC' || $field == 'MNT_RET') {
                $this->validator->isFloat(
                    '',
                    $value,
                    Constante::formatMontant
                );
            }
            $this->validator->brControleLigne($field, $line, $value, $this->params);
        }

        if ($section == 'lot') {
            $this->validator->brControleLot($line, $batch, $field, $value);
        }

        if ($this->validator->isValid()) {
            return new Response(200, [], $this->validator->getError());
        } elseif ($this->validator->isNonBloquant()) {
            return new Response(200, [], $this->validator->getErrorNonBloquant(true));
        } else {
            return new Response(200, [], $this->validator->getError());
        }
    }

    /**
     * @return Response
     */
    public function batchAction()
    {
        $post = $this->request->getParsedBody();

        $this->validator->brControleLotRetour($post['line'], $post['batch'], $post['field'], $post['value']);

        if ($this->validator->isValid()) {
            return new Response(200, [], $this->validator->getError());
        } elseif ($this->validator->isNonBloquant()) {
            return new Response(200, [], $this->validator->getErrorNonBloquant(true));
        } else {
            return new Response(200, [], $this->validator->getError());
        }
    }
}
