<?php

namespace Modules\Stock\Repository;

use Exception;
use Helpers\Outil\Roles;
use Helpers\Pagination\PaginatorGestion;
use Helpers\QueryBuilder\AbsRepository;
use Modules\Stock\Interface\EnteteRepoInterface;
use Modules\Stock\Service\RepoService;
use Modules\Stock\Trait\Repo\EnteteRepoTrait;

class BonTransfertRepo extends AbsRepository implements EnteteRepoInterface
{
    use EnteteRepoTrait;

    /**
     * @param array<string,mixed> $post
     * @return array<int,array<string,mixed>>
     * @throws Exception
     */
    public function listBonsTransfert(array $post) : array
    {
        $repoService = new RepoService();
        $statsCte = $repoService->getEnteteStatsCte('NO_BT', 'SBT_ENTE', 'BT_ENTET');

        $requete = $statsCte . "SELECT DISTINCT 
                                    SE.*,
                                    STATS.NB_DOC,
                                    HA.NOM AS MAG_LIBELLE,
                                    STO1.DESIGN AS SITE_DESIGN,
                                    STO2.DESIGN AS SITE_DEST_DESIGN
                                FROM SBT_ENTE SE with (nolock)
                                LEFT JOIN SBT_LIGN SL with (nolock) ON SE.NO_BT = SL.NO_BT
                                LEFT JOIN STATS ON STATS.NO_BT = SE.NO_BT
                                LEFT JOIN HASTUTIL HA ON SE.MAG = HA.CODE
                                LEFT JOIN SSITESTO STO1 ON SE.SITE = STO1.SITE
                                LEFT JOIN SSITESTO STO2 ON SE.SITE_DEST = STO2.SITE
                                ";

        $arrayReq = [];
        $repoService->addMotCleQueryShard($post, $requete, $arrayReq, 'NO_BT', 'SBT_LIGN', 'CODE_ART');
        $requete .= " WHERE 1=1 ";
        $this->addEtatQueryShard($post, $requete);
        $requete .= Roles::getClausePerimetreSociete('SE.CODE_STE', $arrayReq);
        $parameters =  $repoService->buildQuery($post, $requete, $arrayReq, 'SBT_ENTE');

        $paginatorGestion = new PaginatorGestion();
        $paginatorGestion->injecterSqlCount($requete, $parameters, $arrayReq, $this->pdo);

        return $this->fetchAssocAll($requete, $arrayReq);
    }

    /**
     * @param array<string,string> $post
     * @return array<int,array<string,string|int>>
     * @throws Exception
     */
    public function listBonsTransfertExport(array $post, string $listeSelect): array
    {
        $req = "SELECT DISTINCT " . $listeSelect
            . " FROM SBT_ENTE SE with (nolock) "
            . " LEFT JOIN SBT_LIGN SL ON SE.NO_BT = SL.NO_BT"
            . " LEFT JOIN SARTICLE SA ON SL.CODE_ART = SA.CODE_ART "
        ;

        $arrayReq = [];
        $repoService = new RepoService();
        $repoService->addMotCleQueryShard($post, $req, $arrayReq, 'NO_BT', 'SBT_LIGN', 'CODE_ART');
        $req .= " WHERE 1=1 ";
        $this->addEtatQueryShard($post, $req);
        $req .= Roles::getClausePerimetreSociete('SE.CODE_STE', $arrayReq);
        $repoService->buildQuery($post, $req, $arrayReq, 'SBT_ENTE');

        return $this->fetchAssocAll($req, $arrayReq);
    }

    /**
     * @param array<string,mixed> $post
     * @param string $requete
     * @return void
     */
    private function addEtatQueryShard(array $post, string &$requete): void
    {
        if ($etat = $post['ETAT']) {
            $etat === 'A' ? $requete .= " AND SE.DTE_VAL_S = 00000000 AND SE.DTE_VAL_E = 00000000 ": $requete .= " AND SE.DTE_VAL_S != 00000000 AND SE.DTE_VAL_E != 00000000 ";
        }
    }
}
