<?php

namespace Modules\Reception\Service;

use Entity\Model\BrLigne;
use Entity\Model\Fourninc;
use Entity\Model\Incident;
use Helpers\Outil\Roles;
use Helpers\Outil\Sql;
use Helpers\Parametres\Constante;
use Helpers\Trad\ITrad;
use Helpers\Validator\Validator;
use Modules\Reception\Repository\ControleQualite;
use Modules\Reception\Repository\Incident as RepositoryIncident;
use PDO;

class ControleQualiteService
{
    private PDO $pdo;
    private ITrad $trad;


    public function __construct(PDO $pdo, ITrad $trad)
    {
        $this->trad = $trad;
        $this->pdo = $pdo;
    }

    /**
     * @param Validator $validator
     * @param array<mixed> $post
     * @return array<mixed>
     */
    public function validator(Validator &$validator, $post)
    {
        $updateLigne    = [];
        $updateIncident = [];

        foreach ($post as $k => $v) {
            if (strpos($k, 'controle') === 0) {
                $idLigne = str_replace('controle', '', $k);
                $validator->isFloat(
                    $this->trad->t('Quantité contrôlée') . ' ' . $idLigne,
                    $post['qte_a-' . $idLigne],
                    Constante::formatQTE,
                    true,
                    $post['qte_c-' . $idLigne]
                );
                $updateLigne[] = [
                    'NO_PO_BR'  => $idLigne,
                    'QTE_ACCEP' => $post['qte_a-' . $idLigne]
                ];
            } elseif (strpos($k, 'incident-') === 0) {
                $idLigne = str_replace('incident-', '', $k);

                $validator->isStringAvecEspace($this->trad->t('Commentaire') . ' ' . $idLigne, $post['comment_inc-' . $idLigne], 0, 3000);

                $updateIncident[] = [
                    'NO_PO_BR'      => $idLigne,
                    'COMMENTAIRE'   => $post['comment_inc-' . $idLigne],
                    'QTE_REFUS'     => $post['qte_c-' . $idLigne] - $post['qte_a-' . $idLigne],
                ];
            }
        }

        return [
            'updateLigne'       => $updateLigne,
            'updateIncident'    => $updateIncident
        ];
    }

    /**
     * @param array<mixed> $updateLigne
     * @param array<mixed> $updateIncident
     * @return void
     */
    public function saveControleQualite(string $idDoc, $updateLigne, $updateIncident): void
    {
        $brLigne                    = new BrLigne($this->pdo);
        $fourninc                   = new Fourninc($this->pdo);
        $ctrQualit                  = new ControleQualite($this->pdo);
        $incident                   = new Incident($this->pdo);
        $repositoryIncident         = new RepositoryIncident($this->pdo);
        $infoIncidentRefus          = $incident->find('REFUS');

        $params['INT_EVT']          = Sql::paravrac($this->pdo, 'INT_EVT', 'FOURNINC');
        $params['BR_INC_MAIL_ACH']  = Roles::droit($this->pdo, 'BR_INC_MAIL_ACH', 'PARAMGEN', true);

        $listeLigneTmp              = $ctrQualit->getListeBrForIncident($idDoc);
        $listeLigne                 = [];
        foreach ($listeLigneTmp as $l) {
            $listeLigne[$l['NO_PO_BR']] = $l;
        }

        foreach ($updateLigne as $ligne) {
            $brLigne->update([
                'QTE_ACCEP'             => $ligne['QTE_ACCEP'],
                'numerique_CTRL_FAIT'   => Sql::setBoolSql(1),
            ], [
                'NUM_BR'                => $idDoc,
                'NO_PO_BR'              => $ligne['NO_PO_BR']
            ]);
        }

        foreach ($updateIncident as $inc) {
            $ligneBR = $listeLigne[$inc['NO_PO_BR']];

            $fourninc->insert([
                'FOU'                   => $ligneBR['CODE_FOU'],
                'NIVEAU'                => $infoIncidentRefus['NIVEAU'],
                'DTE_OCCUR'             => $ligneBR['DATE_BL'],
                'CODE'                  => 'REFUS',
                'DESCRIPT'              => '',
                'NO_CDE'                => $ligneBR['NUM_CDE'],
                'PO_CDE'                => $ligneBR['NO_PO_CDE'],
                'NO_BR'                 => $ligneBR['NUM_BR'],
                'PO_BR'                 => $ligneBR['NO_PO_BR'],
                'ART'                   => $ligneBR['CODE_ART'],
                'DES_ART'               => $ligneBR['DESIGN'],
                'QTE_REFU'              => $inc['QTE_REFUS'],
                'DTE_LIV'               => $ligneBR['DATE_BL'],
                'DTE_CREAT'             => $ligneBR['DTE_CREAT'],
                'NM_COM'                => sql::updateMemo($this->pdo, '', $inc['COMMENTAIRE']),
            ]);

            if ($params['INT_EVT'] == 'O') {
                $repositoryIncident->setXtEvent((int) $idDoc);
            }
        }
    }
}
