<?php

namespace Modules\Stock\Service\Form;

use Entity\Model\SdsLign;
use Entity\Model\SdsPlan;
use Helpers\ChampsSupplementaire\ChampSupp;
use Modules\Stock\Enum\SoldeDemandeServiceEnum;
use Modules\Stock\Helper\StringHelper;
use Modules\Stock\Interface\FormServiceInterface;
use Modules\Stock\Repository\LigneDemandeServiceRepo;
use Modules\Stock\Trait\FormTrait;
use PDO;

readonly class LigneDemandeServiceFormService implements FormServiceInterface
{
    use FormTrait;

    private LigneDemandeServiceRepo $ligneDemandeServiceRepo;

    public function __construct(private PDO $pdo)
    {
        $this->ligneDemandeServiceRepo = new LigneDemandeServiceRepo($this->pdo);
    }


    /**
     * @param array<string,mixed> $post
     * @param array<string,array<string|int,mixed>> $data
     * @return array<string,mixed>
     */
    public function initEntity(array $post, array $data = []): array
    {
        $post['NO_DS'] = $data['DEMANDE_SERVICE']['NO_DS'];
        $post['RESTE'] = $post['QTE_DEM'];
        $post['ETAT_LIG'] = SoldeDemandeServiceEnum::A->name;
        $post['DTE_SOUH'] = $post['DTE_SOUH'] ?? $data['LIGNES_PLANNING'][count($data['LIGNES_PLANNING']) - 1]['DTE_SOUH'];
        $post['TYPE_DS'] = $data['DEMANDE_SERVICE']['TYPE_DEM'];
        $post['ORIGINE'] = $data['DEMANDE_SERVICE']['SITE'];
        $post['DESTINAT'] = $data['DEMANDE_SERVICE']['TIERS'];
        $post['CODE_STE'] = $data['DEMANDE_SERVICE']['CODE_STE'];

        if (array_key_exists('INIT_NO_LIG_DS', $data) && $data['INIT_NO_LIG_DS']) {
            $post['NO_LIG_DS'] = $this->ligneDemandeServiceRepo->getNouveauNoLigne($post['NO_DS'], 'NO_LIG_DS', 'SDS_LIGN', 'NO_DS');
        }

        return $post;
    }


    /**
     * @param array<string,mixed> $entity
     * @param array<string,mixed> $post
     * @return void
     */
    public function flushData(array $entity, array $post = []): void
    {
        $this->pdo->beginTransaction();

        (new SdsLign($this->pdo))->processAndMerge($entity);
        ChampSupp::updateCS($post, $this->pdo);
        $this->createLignesPlanning($entity, $post);

        $this->pdo->commit();
    }


    /**
     * @param array<string,mixed> $demandeService
     * @param array<string,mixed> $ligneDemandeService
     * @return array<string,mixed>
     */
    public function initLignePlanning(array $demandeService, array $ligneDemandeService): array
    {
        return [
            'NO_DS' => $demandeService['NO_DS'],
            'NO_LIG_DS' => $ligneDemandeService['NO_LIG_DS'],
            'DTE_SOUH' => $ligneDemandeService['DTE_SOUH'],
            'QTE' => $ligneDemandeService['QTE_DEM'],
            'REAPPRO_FAIT' => false
        ];
    }


    /**
     * @param array<string,mixed> $ligneDemandeService
     * @param array<int,array<string,mixed>> $lignesPlanning
     * @return void
     */
    public function setDateSouhaitee(array &$ligneDemandeService, array $lignesPlanning): void
    {
        if (count($lignesPlanning) > 1) {
            $ligneDemandeService['DTE_SOUH'] = "00000000";

            return;
        }

        $ligneDemandeService['DTE_SOUH'] = $lignesPlanning[0]['DTE_SOUH'];
    }


    /**
     * Création / mise à jour du calendrier de la ligne.
     *
     * @param array<string,mixed> $entity
     * @param array<string,mixed> $post
     * @return void
     */
    private function createLignesPlanning(array $entity, array $post): void
    {
        $sdsPlan = new SdsPlan($this->pdo);
        $sdsPlan->delete(['NO_DS' => $entity['NO_DS'], 'NO_LIG_DS' => $entity['NO_LIG_DS']]);

        foreach ($post['LIGNES_PLANNING'] as $lignePlanning) {
            $lignePlanning['NO_DS'] = $entity['NO_DS'];
            $lignePlanning['NO_LIG_DS'] = $entity['NO_LIG_DS'];
            StringHelper::toFloat($lignePlanning, ['QTE']);
            $sdsPlan->insert($lignePlanning);
        }
    }
}
