<?php

namespace Modules\Panier\Service;

use Entity\Model\Articles;
use Entity\Model\Centre;
use Entity\Model\Country;
use Entity\Model\Fourniss;
use Entity\Model\SousFamille;
use Helpers\Outil\Sql;
use Modules\Panier\Repository\PanierEntete;

class CalculTauxTvaService
{
    /**
     *
     * @var \PDO
     */
    private $pdo;

    /**
     * @var array<mixed>
     */
    private $parametrage;

    /**
     * @var array<mixed>
     */
    private $cacheCentre;

    /**
     * @var array<mixed>
     */
    private $cachePays;

    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;

        $this->parametrage = [
            'PAYSINSTAL' => sql::paravrac($this->pdo, 'PARAMGEN', 'PAYSINSTAL'),
        ];
    }

    /**
     * @param mixed $tauxTva
     * @return float
     */
    public function getTauxTva(string $codeArticle = '', string $codeSousFamille = '', string $codFourn = '', string $ctrLivraison = '', $tauxTva = null)
    {
        $centre = new Centre($this->pdo);
        $country = new Country($this->pdo);
        $article = new Articles($this->pdo);
        $sousFamille = new SousFamille($this->pdo);
        $fourniss = new Fourniss($this->pdo);

        if (!$ctrLivraison) {
            $panierEneteRepo = new PanierEntete($this->pdo);
            $entete = $panierEneteRepo->getEntetePanier($_SESSION['id_panier'], $_SESSION['id_user']);
            $ctrLivraison = $entete['LIV_CTR'];
        }
        if (isset($this->cacheCentre[$ctrLivraison])) {
            $centreLivr = $this->cacheCentre[$ctrLivraison];
        } else {
            $centreLivr = $centre->find($ctrLivraison);
            $this->cacheCentre[$ctrLivraison] = $centreLivr;
        }
        
        if (isset($this->cachePays[$centreLivr['PAYS']])) {
            $recPaysCentre = $this->cachePays[$centreLivr['PAYS']];
        } else {
            $recPaysCentre = $country->find($centreLivr['PAYS']);
            $this->cachePays[$centreLivr['PAYS']] = $recPaysCentre;
        }

        if ($centreLivr && $recPaysCentre) {
            $taxeLocalCentre = sql::boolSql($recPaysCentre['AV_TAXLOC']);
        } else {
            $taxeLocalCentre = false;
        }
        
        $param['PAYSINSTAL'] = $this->parametrage['PAYSINSTAL'];

        $regle1 = false;

        if ($taxeLocalCentre) {
            $taxeLocalArticle = false;
            if ($codeArticle) {
                $infoArt = $article->find($codeArticle);

                if (sql::boolSql($infoArt['NVIBOXAP']) == 0) {
                    $taxeLocalArticle = sql::boolSql($infoArt['AV_TAXLOC']);
                }
            }

            $taxeLocalSfam = false;
            if ($codeSousFamille) {
                $infoSfm = $sousFamille->find($codeSousFamille);

                if (Sql::boolSql($infoSfm['NVISIPROC']) == 0) {
                    $taxeLocalSfam = sql::boolSql($infoSfm['AV_TAXLOC']);
                }
            }

            if ($taxeLocalArticle || $taxeLocalSfam) {
                $tauxTva = $centreLivr['TX_TAXLOC'];
                $regle1 = true;
            }
        }

        if (!$regle1) {
            $param['REGINIT_TVA'] = Sql::paravrac($this->pdo, 'PARAMGEN', 'REGINIT_TVA');

            if ($param['REGINIT_TVA'] == 'O') {
                $tauxTva = $recPaysCentre['TVA']??0;
            } else {
                if (!$codFourn) {
                    $paysAControle = $param['PAYSINSTAL'];
                } else {
                    $infoFRS = $fourniss->find($codFourn);
                    if ($infoFRS) {
                        $paysAControle = $infoFRS['CODE_PAYS'];
                    } else {
                        $paysAControle = $param['PAYSINSTAL'];
                    }
                    
                    if (isset($this->cachePays[$paysAControle])) {
                        $recPaysAControle = $this->cachePays[$paysAControle];
                    } else {
                        $recPaysAControle = $country->find($paysAControle);
                        $this->cachePays[$paysAControle] = $recPaysAControle;
                    }

                    $paysDuCentreLivr = $centreLivr['PAYS']??'';

                    if (!$paysDuCentreLivr) {
                        $paysDuCentreLivr = $param['PAYSINSTAL'];
                    }

                    if ($paysAControle == $paysDuCentreLivr || $recPaysAControle['PAYSCEE'] == ($recPaysCentre['PAYSCEE']??null)) {
                        if (is_null($tauxTva) && $codeArticle != '') {
                            $infoArt = $article->find($codeArticle);
                            if ($infoArt) {
                                $tauxTva = $infoArt['TXTVA'];
                            }
                        }
                    } else {
                        $tauxTva = 0;
                    }
                }
            }
        }
        
        if (is_null($tauxTva)) {
            (float) $tauxTva = Sql::parametr($this->pdo, 'TX_TVA');
        }
        
        return (float) number_format($tauxTva, 2, '.', '');
    }
}
