<?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 BonSortieRepo extends AbsRepository implements EnteteRepoInterface
{
    use EnteteRepoTrait;

    /**
     * @param array<string,mixed> $post
     * @return array<int,array<string,mixed>>
     * @throws Exception
     */
    public function listBonsSortie(array $post) : array
    {
        $repoService = new RepoService();
        $statsCte = $repoService->getEnteteStatsCte('NO_BS', 'SBS_ENTE', 'BS_ENTET');

        $requete = $statsCte . " SELECT DISTINCT
                        SE.NO_BS AS NO_BS,
                        SE.CODE_STE AS CODE_STE,
                        SE.DTE_CREAT AS DTE_CREAT,
                        SE.DTE_VALID AS DTE_VALID,
                        SE.MAG AS MAG,
                        HA.NOM AS MAG_LIBELLE,
                        SE.SITE AS SITE,
                        STO.DESIGN AS SITE_DESIGN,
                        SE.TOT_BS AS TOT_BS,
                        SE.DEVISE AS DEVISE,
                        STATS.NB_DOC
                    FROM SBS_ENTE SE with (nolock)
                    LEFT JOIN STATS ON STATS.NO_BS = SE.NO_BS
                    LEFT JOIN SBS_LIGN SL ON SE.NO_BS = SL.NO_BS
                    LEFT JOIN HASTUTIL HA ON SE.MAG = HA.CODE
                    LEFT JOIN SSITESTO STO ON SE.SITE = STO.SITE
        ";

        $arrayReq = [];
        $repoService->addMotCleQueryShard($post, $requete, $arrayReq, 'NO_BS', 'SBS_LIGN');
        $requete .= " WHERE 1=1 ";
        $repoService->addEmptyTiersQueryShard($post, $requete, 'SE.DEST');
        $repoService->addEtatQueryShard($post, $requete, $arrayReq);
        $requete .= Roles::getClausePerimetreSociete('SE.CODE_STE', $arrayReq);
        $parameters =  $repoService->buildQuery($post, $requete, $arrayReq, 'SBS_ENTE');

        (new 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 listBonsSortieExport(array $post, string $listeSelect): array
    {
        $req = "SELECT DISTINCT " . $listeSelect . " FROM SBS_ENTE SE with (nolock) " . "LEFT JOIN SBS_LIGN SL ON SE.NO_BS = SL.NO_BS";

        $arrayReq = [];
        $repoService = new RepoService();
        $repoService->addMotCleQueryShard($post, $req, $arrayReq, 'NO_BS', 'SBS_LIGN');
        $req .= " WHERE 1=1 ";
        $repoService->addEmptyTiersQueryShard($post, $req, 'SE.DEST');
        $repoService->addEtatQueryShard($post, $req, $arrayReq);
        $req .= Roles::getClausePerimetreSociete('SE.CODE_STE', $arrayReq);
        $repoService->buildQuery($post, $req, $arrayReq, 'SBS_ENTE');

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