<?php

namespace Modules\Stock\Service\Form;

use Entity\Model\Memos;
use Entity\Model\SdsEnte;
use Entity\Model\Snoreser;
use Exception;
use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Outil\Sql;
use Modules\Stock\Enum\Operation;
use Modules\Stock\Enum\SoldeDemandeServiceEnum;
use Modules\Stock\Enum\TypeNumerotationEnum;
use Modules\Stock\Interface\FormServiceInterface;
use Modules\Stock\Repository\DemandeServiceRepo;
use Modules\Stock\Trait\FormTrait;
use PDO;

readonly class DemandeServiceFormService implements FormServiceInterface
{
    use FormTrait;

    public function __construct(private PDO $pdo)
    {
    }

    /**
     * @param array<string,mixed> $post
     * @param array<string,mixed> $data
     * @return array<string,mixed>
     * @throws Exception
     */
    public function initEntity(array $post, array $data = []): array
    {
        $demandeService = [
            'NO_DS' => $post['NO_DS'],
            'DTE_CREAT' => $post['DTE_CREAT'],
            'TYPE_DEM' => $post['TYPE_DEM'],
            'TIERS' => $post['TIERS'],
            'SITE' => $post['SITE'],
            'CODE_STE' => $post['CODE_STE'] ?? $data['SITE_ORIGINE']['CODE_STE'],
            'SOLDE_DS' => SoldeDemandeServiceEnum::A->name,
            'DTE_VALID' => '00000000',
            'NM_COMME' => $post['NM_COMME'] ?? 0
        ];

        $this->initNoDs($data['PARAMETRE_NUMEROTATION'], $demandeService);

        return $demandeService;
    }

    /**
     * @throws Exception
     * @param array<string,mixed> $entity
     */
    public function flushData(array $entity, array $post = []): void
    {
        if ($post['operation'] === Operation::CREATE) {
            $champSupp = new ChampSupp($this->pdo);
            $champSupp->copyDefault('SDS_ENTE', [$entity['NO_DS'], null], 'SOCIETE', $entity['CODE_STE']);
            $champSupp->copyDefault('SDS_ENTE', [$entity['NO_DS'], null], 'SSITESTO', $entity['SITE']);
        }

        (new SdsEnte($this->pdo))->insert($entity);
        (new Snoreser($this->pdo))->delete(['NO_DS' => $entity['NO_DS']]);
    }

    /**
     * @param array<string,mixed> $parametreNumerotation
     * @param array<string,mixed> $demandeService
     * @return void
     * @throws Exception
     */
    public function initNoDs(array $parametreNumerotation, array &$demandeService): void
    {
        if ($parametreNumerotation['TYPE_NUM'] === TypeNumerotationEnum::A->name
            || ($parametreNumerotation['TYPE_NUM'] === TypeNumerotationEnum::I->name && $demandeService['NO_DS'] === '')
        ) {
            $demandeService['NO_DS'] = (new DemandeServiceRepo($this->pdo))->getAutomatiqueNo($parametreNumerotation);
        }
    }

    /**
     * @param array<string,mixed> $demandeService
     * @throws Exception
     */
    public function duplicateMemos(array &$demandeService): void
    {
        if (!$nmComme = $demandeService['NM_COMME']) {
            return;
        }

        $memos = new Memos($this->pdo);
        $nodeMemo = (new Sql())->generateNum($this->pdo);
        $commentaire = ['NODEMEMO' => $nodeMemo, 'CONTMEMO' => $memos->find($nmComme, ['CONTMEMO'])['CONTMEMO']];
        $demandeService['NM_COMME'] = $nodeMemo;

        $memos->insert($commentaire);
    }
}
