<?php

namespace Modules\Stock\Repository;

use Helpers\QueryBuilder\AbsRepository;

class CahierInventaireRepo extends AbsRepository
{
    /**
     * @param string $noInv
     * @param string $site
     * @return array<int,array<string,string>>
     */
    public function getExportData(string $noInv, string $site): array
    {
        $req = "SELECT STO.EMPLAC AS EMPLAC, SCI.CODE_ART AS CODE_ART, SA.DESIGN AS DESIGN, SLO.NO_LOT AS NO_LOT FROM SCINVENT SC 
                LEFT JOIN SCIINVLI SCI ON SC.NO_INV = SCI.NO_INV AND SC.SITE = SCI.SITE
                LEFT JOIN SSTOCKLO STO ON SCI.CODE_ART = STO.CODE_ART AND SC.SITE = STO.SITE
                LEFT JOIN SLOTLOC SLO ON SCI.CODE_ART = SLO.CODE_ART AND SCI.SITE = SLO.SITE
                LEFT JOIN SARTICLE SA ON SCI.CODE_ART = SA.CODE_ART
                WHERE SC.NO_INV=:noInv AND (SLO.QTE_LOC is NULL OR SLO.QTE_LOC > 0) AND SCI.SITE=:site;";

        return $this->fetchAssocAll($req, [':noInv' => $noInv, ':site' => $site]);
    }

    /**
     * @param string|int $noInv
     * @return int
     */
    public function countCahiersInventairesForInv(string|int $noInv): int
    {
        $req = "SELECT COUNT(*) AS NB FROM SCINVENT with (nolock) WHERE NO_INV=:noInv";

        $arrayReq = [':noInv' => $noInv];

        return (int) $this->fetchAssocAll($req, $arrayReq)[0]['NB'];
    }

    /**
     * @param string|int $noInv
     * @return array<int,array<string,string>>
     */
    public function findFromNoInv(string|int $noInv): array
    {
        $req = "SELECT 
                    SC.*,
                    STO.DESIGN AS SITE_DESIGN
                FROM SCINVENT SC
                LEFT JOIN SSITESTO STO ON SC.SITE = STO.SITE
                WHERE SC.NO_INV=:noInv";

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