<?php
namespace API\Partenaire\Repository;

use API\Partenaire\IPartenaire;
use Entity\Model\ParamDico;
use Helpers\Log\LogSysteme;
use Helpers\QueryBuilder\AbsRepository;
use Modules\GlobalApplication\Repository\LicenceOxalys;
use Psr\SimpleCache\CacheInterface;

class PartenaireRepo extends AbsRepository
{


//_____________________________________________________________________________
// Accessor

    /**
     * @return array<string> list of partenaire
     */
    public function getListActivated() :array
    {
        $list = array_column(
            (new LicenceOxalys($this->pdo))->getPartenaireList(),
            'VAL_LIC',
        );

        // Case : demo licence
        if ((string) $_SESSION['Session_NumClient'] === '0') {
            foreach ((new ParamDico($this->pdo))->findAll([
                'TYPEDICO' => 'CLIENT0_PARTFOU'
            ]) as $demo) {
                $list[] = $demo['ID1'];
            }
        }
        return $list;
    }


    
    /**
     * listePartenaireFrs
     * @param array<string> $listeFrs
     * @return array<array<mixed>>
     */
    public function listePartenaireFrs(array $listeFrs) :array
    {
        $param = [];
        return $this->fetchAssocAll(
            "SELECT CODE_FOU, CODEPARTENAIRE 
            FROM FOUR_PARTENAIRE_DATA with(nolock)
            WHERE ".$this->makeWhereIn('CODE_FOU', $listeFrs, $param)." 
                AND CODE_FOU not in (
				    SELECT ID3 FROM PARAMDICO with(nolock) 
                    WHERE TYPEDICO = 'INVIT_PARTFOU' 
                        AND ALPHA2 != 'A' 
                        AND ID1 = CODEPARTENAIRE
                )",
            $param
        );
    }

    /**
     * @return array<array<mixed>>
     */
    public function listFournissByPartn(string $partnId) :array
    {
        return $this->fetchAssocAll(
            'SELECT
                FOURNISS.*,
                PARAMDICO.ID4 as ExternalId
            FROM FOURNISS with (nolock)
            JOIN FOUR_PARTENAIRE_DATA with (nolock) 
                ON FOUR_PARTENAIRE_DATA.CODE_FOU = FOURNISS.CODE
            LEFT JOIN PARAMDICO with (nolock)
                ON PARAMDICO.ID1 = FOUR_PARTENAIRE_DATA.CODEPARTENAIRE
                AND PARAMDICO.ID3 = FOUR_PARTENAIRE_DATA.CODE_FOU
                AND PARAMDICO.TYPEDICO = CONCAT(FOUR_PARTENAIRE_DATA.CODEPARTENAIRE, \'_KEY\')
            WHERE FOUR_PARTENAIRE_DATA.CODEPARTENAIRE = :partnId',
            ['partnId' => $partnId]
        );
    }

    /**
     * @param array<mixed> $listRegisterNumber
     * @param array<string> $listField
     * @return string[][]
     */
    public function searchFourniss(array $listRegisterNumber, array $listField)
    {
        if (empty($listField)) {
            LogSysteme::prefixErrorLog('Invalid $fields');
            return [];
        }

        if (empty($listRegisterNumber)) {
            return [];
        }

        $param = [];
        return $this->fetchAssocAll(
            "SELECT * FROM FOURNISS with(nolock)
            JOIN (".$this->generateTableConst(array_map(function ($item) {
                return ['regnumber' => $item];
            }, $listRegisterNumber), $param).") as Ref 
                ON " . implode(
                ' OR ',
                array_map(function ($field) {
                        return "Ref.regnumber = UPPER(FOURNISS." . $field . ")";
                }, $listField)
            ),
            $param
        );
    }
}
