<?php
namespace API\Partenaire\Repository;

use API\Partenaire\IPartenaire;
use Helpers\QueryBuilder\AbsRepository;
use Psr\SimpleCache\CacheInterface;

class PartenaireRepo extends AbsRepository
{


//_____________________________________________________________________________
// Accessor

    /**
     * @return array<string> list of partenaire
     */
    public function getListActivated() :array
    {
        // TODO : use licence instead ?
        $req ="SELECT TYPEDICO
            FROM DILISPEC with (nolock) 
            WHERE TYPEDICO IN (
                'INFOLG', 'BVD', 'HIVEO', 
                'EAT', 'PROVIGIS', 'ALTARES'
            ) GROUP BY TYPEDICO";

        $list = $this->fetchAssocAll($req, []);
        return array_column($list, 'TYPEDICO');
    }


    
    /**
     * 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]
        );
    }
}
