<?php

namespace Modules\Stock\Repository;

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

    /**
     * @param array<string,mixed> $post
     * @return array<int,array<string,mixed>>
     * @throws Exception
     */
    public function listLotsLocaux(array $post) : array
    {
        $requete = "SELECT DISTINCT
                        SLO.NO_LOT AS NO_LOT,
                        SLO.QTE_LOC AS QTE_LOC,
                        SLO.QTE_INDIS AS QTE_INDIS,
                        SGO.PRIX AS PRIX,
                        SGO.DTE_PEREM AS DTE_PEREM,
                        SA.UNITE_ACH AS UNITE_ACH,
                        SA.UNITE_STK AS UNITE_STK,
                        STO.DEVISE AS DEVISE
                    FROM SLOTLOC SLO
                    LEFT JOIN SLOTGLOB SGO ON SLO.NO_LOT = SGO.NO_LOT
                    LEFT JOIN SARTICLE SA ON SLO.CODE_ART = SA.CODE_ART
                    LEFT JOIN SSITESTO STO ON SLO.SITE = STO.SITE
                    WHERE 1=1 ";

        $arrayReq = [];
        $parameters =  (new RepoService())->buildQuery($post, $requete, $arrayReq, 'SLOTLOC');

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

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