<?php

namespace Modules\Additional\Specifique\Client95\Modules\EtatStatistique\Service;

use Helpers\Fichier\Fichier;
use Helpers\Outil\Date;
use Helpers\Outil\Roles;
use Helpers\Outil\Sql;
use Helpers\Trad\Trad;
use Modules\Additional\Specifique\Client95\Modules\EtatStatistique\Repository\Reporting;
use OpenApi\LinkExample\Repository;
use PDO;
use Skeleton\Router;

class EtatStatistiqueService
{


    /**
     * pdo
     *
     * @var PDO
     */
    private $pdo;

    /**
     * pdo
     *
     * @var Reporting
     */
    private Reporting $repository;


    public function __construct(PDO $pdo)
    {
        $this->pdo = $pdo;
        $this->repository = new Reporting($this->pdo);
    }

    /**
     * checkDate
     *
     * @param  string $period
     * @param  string $endDate
     * @return bool
     */
    public function checkDate(string $period, string $endDate)
    {
        // on commence par aller chercher les dates de début et de fin de la période choisie
        $dates = $this->repository->checkDate($period, $endDate);

        if ($dates && $endDate >= $dates['DATE_DEB'] && $endDate <= $dates['DATE_FIN']) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * orderOnInvoicesAnalysis
     *
     * @param  string $period
     * @param  string $endDate
     * @param  string $type
     * @param  string $perimeter
     * @return array<mixed>
     */
    public function orderOnInvoicesAnalysis(string $period, string $endDate, string $type, string $perimeter): array
    {

        // on commence par récupérer les cumuls par mois de facturé et engagé
        $invoicesPerMonth = $this->repository->invoicesPerMonth($period, $endDate, $type, $perimeter);

        $ordersPerMonth = $this->repository->ordersPerMonth($period, $endDate, $type, $perimeter);

        // puis on regroupe ces données dans un tableau allant de 1 à 12 pour chaque mois de l'année
        $months = range(1, 12);
        $data = [];

        foreach ($months as $month) {
            $data[$month]['mois'] = $month;
            $data[$month]['FACTURE'] = 0.00;
            $data[$month]['ENGAGE'] = 0.00;
            foreach ($invoicesPerMonth as $invoice) {
                if ($invoice['mois'] == $month) {
                    $data[$month]['FACTURE'] = round($invoice['FACTURE'], 2);
                }
            }

            foreach ($ordersPerMonth as $order) {
                if ($order['mois'] == $month) {
                    $data[$month]['ENGAGE'] = round($order['ENGAGE'], 2);
                }
            }
        }

        return $data;
    }
    
    /**
     * getZoomList
     *
     * @param  string $type
     * @param  array<mixed> $post
     * @return array<mixed>
     */
    public function getZoomList(string $type, array $post = []): array
    {
        $userAdmin = Roles::isAdminCentral($this->pdo, $_SESSION['Session_Login']);
        $userUpperHierarchy = Roles::isUpperHierarchy($this->pdo, $_SESSION['Session_Login']);
        
        if ($type == 'centre') {
            $centersList = $this->repository->getCentersList($_SESSION['Session_Login'], $userAdmin, $userUpperHierarchy, $post, true);
            return $centersList;
        } else {
            $centersList = $this->repository->getCentersList($_SESSION['Session_Login'], $userAdmin, $userUpperHierarchy, []);
            $list = $this->repository->getAccountsList($centersList, $userAdmin, $post);

            return $list;
        }
    }
    
    /**
     * calculationOfTotals
     *
     * @param  array<mixed> $data
     * @return array<mixed>
     */
    public function calculationOfTotals(array $data): array
    {
        // $total = [];
        // $total['BUDGET'] = 0;
        // $total['BUDGET_N_1'] = 0;
        // $total['ENGAGE'] = 0;
        // $total['ENGAGE_N_1'] = 0;
        // $total['ECART_ENGAGE_EUROS'] = 0;
        // $total['ECART_ENGAGE_PRCT'] = 0;
        // $total['BUDGET_RESTANT_EURO'] = 0;
        // $total['BUDGET_RESTANT_PRCT'] = 0;
        // $total['FACTURE'] = 0;
        // $total['FACTURE_N_1'] = 0;
        // $total['ECART_FACTURE_EUROS'] = 0;
        // $total['ECART_FACTURE_PRCT'] = 0;
        // $total['BUDGET_RESTANT_FACTURE_EURO'] = 0;
        // $total['BUDGET_RESTANT_FACTURE_PRCT'] = 0;
        // $total['INDICATEUR_ALERTE'] = 0;
        // $total['PRCT_CONSO'] = 0;

        // foreach ($data as $ligne) {
        //     $total['BUDGET'] += intval($ligne['BUDGET']);
        //     $total['BUDGET_N_1'] += intval($ligne['BUDGET_N_1']);
        //     $total['ENGAGE'] += intval($ligne['ENGAGE']);
        //     $total['ENGAGE_N_1'] += intval($ligne['ENGAGE_N_1']);
        //     $total['ECART_ENGAGE_EUROS'] += intval($ligne['ECART_ENGAGE_EUROS']);
        //     $total['BUDGET_RESTANT_EURO'] += intval($ligne['BUDGET_RESTANT_EURO']);
        //     $total['FACTURE'] += intval($ligne['FACTURE']);
        //     $total['FACTURE_N_1'] += intval($ligne['FACTURE_N_1']);
        //     $total['ECART_FACTURE_EUROS'] += intval($ligne['ECART_FACTURE_EUROS']);
        //     $total['BUDGET_RESTANT_FACTURE_EURO'] += intval($ligne['BUDGET_RESTANT_FACTURE_EURO']);
        // }
        // $total['ECART_ENGAGE_PRCT'] = round(($total['ENGAGE'] - $total['ENGAGE_N_1']) / $total['ENGAGE_N_1'] * 100, 2);
        // $total['BUDGET_RESTANT_PRCT'] = round(($total['BUDGET'] - $total['ENGAGE']) / $total['BUDGET'] * 100, 2);
        // $total['ECART_FACTURE_PRCT'] = round(($total['FACTURE'] - $total['FACTURE_N_1']) / $total['FACTURE_N_1'] * 100, 2);
        // $total['BUDGET_RESTANT_FACTURE_PRCT'] = round(($total['BUDGET'] - $total['FACTURE']) / $total['BUDGET'] * 100, 2);
        // $total['INDICATEUR_ALERTE'] = round($total['FACTURE'] / $total['BUDGET'] * 100, 2);
        // $total['PRCT_CONSO'] = round($total['ENGAGE'] / $total['BUDGET'] * 100, 2);

        // return $total;

        return [0 => 0];
    }
    
    /**
     * formatDataForExport
     *
     * @param  array<mixed> $data
     * @return array<mixed>
     */
    public function formatDataForExport(array $data): array
    {
        $return = [];
        // on remplace les séparateurs "." par des séparateurs "," pour les valeurs numériques de l'export
        foreach ($data as $key1 => $value1) {
            foreach ($value1 as $key => $value) {
                if ($key != 'CENTRE_ANALYSE' && $key != 'COMPTE' && $key != 'NAT_DEPENSE' && $key != 'RESPONSABLE_CTA') {
                    if ($value) {
                        $return[$key1][$key] = number_format($value, 2, ',', '');
                    } else {
                        $return[$key1][$key] = '0,00';
                    }
                } else {
                    $return[$key1][$key] = $value;
                }
            }
        }

        return $return;
    }
}
