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

    /**
     * @param array<string,string> $post
     * @return array<int,array<string,string|int>>
     * @throws Exception
     */
    public function listInventaires(array $post): array
    {
        $arrayReq = [];
        $req = "WITH SITES_STATS AS (SELECT SCI.NO_INV, COUNT(SCI.SITE) AS NB_SITES FROM SCINVENT SCI GROUP BY SCI.NO_INV) 
                SELECT DISTINCT 
                    SI.NO_INV,
                    SI.DESIGN,
                    SI.DTE_CREAT,
                    SI.DTE_PREV,
                    SI.ORD_TRI,
                    SI.DTE_PRET,
                    SI.MVT_BLOQ,
                    SI.DTE_VAL,
                    (CASE
                        WHEN SST.NB_SITES = 1 THEN SCI.SITE
                        ELSE NULL
                    END) AS UNIQUE_SITE
                FROM SINVENTA SI with (nolock)
                LEFT JOIN SITES_STATS SST ON SI.NO_INV = SST.NO_INV
                LEFT JOIN SCINVENT SCI ON SI.NO_INV = SCI.NO_INV 
                WHERE 1 = 1 "
            . Roles::getClausePerimetreSociete('SI.CODE_STE', $arrayReq);

        $this->addStatutFilter($req, $post);

        $parameters = (new RepoService())->buildQuery($post, $req, $arrayReq, 'SINVENTA');
        $paginatorGestion = new PaginatorGestion();
        $paginatorGestion->injecterSqlCount($req, $parameters, $arrayReq, $this->pdo);

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

    /**
     * @param string $noInv
     * @return array<string,mixed>
     */
    public function findFromNoInv(string $noInv): array
    {
        $requete = "SELECT
                        SI.CODE_STE AS CODE_STE,
                        S.RAIS_SOC AS RAIS_SOC,
                        SI.MAG AS MAG,
                        H.CTR_ADR AS MAG_LIBELLE,
                        SI.DTE_VAL AS DTE_VAL,
                        SI.DTE_PRET AS DTE_PRET,
                        M.CONTMEMO AS DESCRIPTION
                    FROM SINVENTA SI
                    LEFT JOIN SOCIETE S ON SI.CODE_STE = S.CODE_STE
                    LEFT JOIN HASTUTIL H ON SI.MAG = H.CODE
                    LEFT JOIN MEMOS M ON SI.NM_DESCR = M.NODEMEMO
                    WHERE SI.NO_INV = :noInv";

        return $this->fetchAssocOne($requete, [':noInv' => $noInv]);
    }

    /**
     * @param array<string,mixed> $post
     * @param string $listeSelect
     * @return array<int,array<string,mixed>>
     * @throws Exception
     */
    public function listInventairesExport(array $post, string $listeSelect): array
    {
        $arrayReq = [];
        $req = "SELECT $listeSelect
                FROM SINVENTA SI with (nolock)
                LEFT JOIN SCIINVLI SCI ON SI.NO_INV = SCI.NO_INV
                WHERE (DATEFERM = '00000000' OR DATEFERM > :dateJour)"
            . Roles::getClausePerimetreSociete('SINVENTA.CODE_STE', $arrayReq)
        ;

        $arrayReq[':dateJour'] = date('Ymd');
        $this->addStatutFilter($req, $post);
        (new RepoService())->buildQuery($post, $req, $arrayReq, 'SINVENTA');

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

    /**
     * @param string $req
     * @param array<string,mixed> $post
     * @return void
     */
    private function addStatutFilter(string &$req, array $post): void
    {
        if (array_key_exists('STATUT', $post) && $post['STATUT'] !== "") {
            if ($post['STATUT'] === "C") {
                $req .= " AND SI.DTE_PRET = '00000000' AND SI.DTE_VAL = '00000000'";
            }
            if ($post['STATUT'] === "A") {
                $req .= " AND SI.DTE_VAL = '00000000' AND SI.DTE_PRET != '00000000' ";
            }
            if ($post['STATUT'] === "V") {
                $req .= " AND SI.DTE_VAL != '00000000' AND SI.DTE_PRET != '00000000'";
            }
        }
    }
}
