<?php

namespace Modules\Stock\Repository;

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

class SiteStockRepo extends AbsRepository
{
    use ImputationRepoTrait;

    /**
     * @param array<string,mixed> $post
     * @return array<int,array<string,mixed>>
     * @throws Exception
     */
    public function listSitesStock(array $post): array
    {
        $siSectorisationJoinValue = $this->getImputationSectoJoinValue('SISECTOR', "STO.CODE_STE");

        $arrayReq = [];
        $requete = "SELECT 
                        STO.*,
                        SI.LIBELLE AS SI_LIBELLE,
                        CC.LIBELLE AS CDC_LIBELLE,
                        SCA.LIBELLE AS SCA_LIBELLE
                    FROM SSITESTO STO with (nolock)
                    LEFT JOIN SECTIMPU SI ON STO.SI = SI.CODE_SI AND SI.CODE_STE = ".$siSectorisationJoinValue."
                    LEFT JOIN CODECOMP CC ON STO.CDC = CC.CODE_CDC AND CC.CODE_STE = ".$this->getImputationSectoJoinValue('CDCSECTO', "STO.CODE_STE")."
                    LEFT JOIN SSCPTANA SCA ON STO.SCA = SCA.CODE_SCA AND SCA.CODE_STE = ".$this->getImputationSectoJoinValue('SCASECTO', "STO.CODE_STE")."
                    WHERE 1=1 "
            . Roles::getClausePerimetreSociete('STO.CODE_STE', $arrayReq);

        if (isset($post['ETAT']) && $post['ETAT'] !== '') {
            $today = date("Ymd");
            match ($post['ETAT']) {
                'V' => $requete .= " AND (STO.SIST_DTE_FERM = '00000000' OR STO.SIST_DTE_FERM >= :today)",
                'N' => $requete .= " AND STO.SIST_DTE_FERM < :today AND STO.SIST_DTE_FERM != '00000000'",
                default => null
            };

            $arrayReq[':today'] = $today;
        }

        $parameters =  (new RepoService())->buildQuery($post, $requete, $arrayReq, 'SSITESTO');
        (new PaginatorGestion())->injecterSqlCount($requete, $parameters, $arrayReq, $this->pdo);

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

    /**
     * @return array<string,mixed>
     */
    public function findSitesSocieteOrigine(string $siteStock): array
    {
        $requete = "SELECT DISTINCT S1.SITE FROM SSITESTO S1 with (nolock) 
                    INNER JOIN SSITESTO S2 ON S1.CODE_STE = S2.CODE_STE
                    WHERE S2.SITE=:siteOrigine1 AND S1.SITE != :siteOrigine2";

        $arrayReq = [':siteOrigine1' => $siteStock, ':siteOrigine2' => $siteStock];

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

    /**
     * @param string $code
     * @return array<int,array<string,mixed>>
     */
    public function findFromMagasinier(string $code): array
    {
        $requete = "SELECT * FROM SSITESTO STO LEFT JOIN HASTUTIL HT ON STO.SITE = HT.SIT_STO WHERE HT.CODE=:code";

        $arrayReq = [':code' => $code];
        return $this->fetchAssocAll($requete, $arrayReq);
    }
}
