<?php
namespace API\InseeSiret;

use API\InseeSiret\SiretWsService;
use API\Partenaire\IDataHinter;
use API\Partenaire\IFournissSearch;
use API\Partenaire\IPartenaire;
use Entity\Model\Fourniss;
use Psr\Container\ContainerInterface;

class InseeSiretPartenaire implements IPartenaire, IDataHinter, IFournissSearch
{

    private ContainerInterface $container;
    private ?SiretWsService $webservice = null;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

//_____________________________________________________________________________
// Accessor

    public function getCode() :string
    {
        return 'WS_SIRET';
    }

    public function getLabel(bool $short = false) :string
    {
        return 'Insee - Siret';
    }

    private function getWebService() :SiretWsService
    {
        if ($this->webservice == null) {
            $this->webservice = SiretWsService::createFromConfig($this->container);
        }
        return $this->webservice;
    }

//_____________________________________________________________________________
// IDataHinter

    public function suggest(string $entityClass, array $data, array $prompter) :?array
    {
        // Case : wrong entity or not enough data to hint
        if ($entityClass != Fourniss::class || ($data['CODE_PAYS'] ?? '') != 'FR'
            // Only respond to changes on RS or NO_SIRET
            || (! in_array('RS', $prompter) && ! in_array('NO_SIRET', $prompter))
            // Require non-blank value for search query
            || (empty($data['RS']) && empty($data['NO_SIRET']))
        ) {
            return null;
        }

        $listEtab = $this
            ->getWebService()
            ->search($data['NO_SIRET'] ?? '', $data['RS'] ?? '');

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

        $res = [];
        foreach ($listEtab as $etab) {
            $adr = $etab->adresseEtablissement;
            
            $res['NO_SIRET'][] = $etab->siret;
            $res['RS'][] = $etab->uniteLegale->denominationUniteLegale;
            $res['ADR'][] = implode(' ', array_filter([
                $adr->complementAdresseEtablissement,
                $adr->numeroVoieEtablissement,
                $adr->typeVoieEtablissement,
                $adr->libelleVoieEtablissement,
            ]));
            $res['CPOSTAL'][] = $adr->codePostalEtablissement;
            $res['VILLE1'][] = $adr->libelleCommuneEtablissement;
        }
        return $res;
    }

//_____________________________________________________________________________
// IFournissSearch

    public function search(array $data) :array
    {
        // Require non-blank value for search query
        if (($data['CODE_PAYS'] ?? '') != 'FR'
            || (empty($data['RS']) && empty($data['NO_SIRET']))
        ) {
            return [];
        }

        $listEtab = $this
            ->getWebService()
            ->search($data['NO_SIRET'] ?? '', $data['RS'] ?? '');

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

        $res = [];
        foreach ($listEtab as $etab) {
            $adr = $etab->adresseEtablissement;
            
            $res[] = [
                'NO_SIRET' => $etab->siret ?? '',
                'RS' => $etab->uniteLegale->denominationUniteLegale ?? '',
                'ADR' => implode(' ', array_filter([
                    $adr->complementAdresseEtablissement,
                    $adr->numeroVoieEtablissement,
                    $adr->typeVoieEtablissement,
                    $adr->libelleVoieEtablissement,
                ])),
                'CPOSTAL' => $adr->codePostalEtablissement ?? '',
                'VILLE1' => $adr->libelleCommuneEtablissement ?? '',
                'CODE_PAYS' => 'FR',
            ];
        }
        return $res;
    }

//_____________________________________________________________________________
// Sub-routine
}
