<?php

namespace Modules\Stock\MenuContextuel;

use Helpers\Outil\MenuContextuel;
use Helpers\Outil\Roles;
use Helpers\Trad\ITrad;
use Helpers\Trad\Trad;
use Modules\Stock\Enum\DemandeServiceTypeEnum;
use Modules\Stock\Enum\SoldeDemandeServiceEnum;
use Modules\Stock\Helper\RulesHelper;
use Modules\Stock\Interface\MenuContextuelInterface;
use Modules\Stock\Trait\MenuContextuelTrait;
use PDO;
use Skeleton\Router;

class DemandeServiceContextuel extends MenuContextuel implements MenuContextuelInterface
{
    use MenuContextuelTrait;

    /**
     * @var array<string,mixed>
     */
    public array $droit = [];

    public function __construct(PDO $pdo, Router $router, ITrad $trad)
    {
        parent::__construct($pdo, $router, $trad);
    }

    /**
     * @param array<string,string|int> $entity
     * @return array<string, array<int,array<string, mixed>>>
     */
    public function generate(array $entity): array
    {
        $menuContextuel = [
            'listePrincipal' => [],
            'listeSecondaire' => [
                [
                    'libelle' => $this->trad->t('Dupliquer'),
                    'lien' => $this->router->generateUri('Stock.DemandeService.dupliquerForm', ['noDs' => $entity['NO_DS']]),
                ]
            ]
        ];

        $isDsASatisfaire = $entity['SOLDE_DS'] === SoldeDemandeServiceEnum::E->name;
        $isDsAValider = $entity['SOLDE_DS'] === SoldeDemandeServiceEnum::A->name;
        $aSatisfaireListe = $isDsASatisfaire ? 'listePrincipal': 'listeSecondaire';
        $aValiderListe = $isDsAValider ? 'listePrincipal': 'listeSecondaire';

        $menuContextuel[$aSatisfaireListe][] = [
            'icone' => 'visualiser_v2',
            'libelle' => $this->trad->t('Visualiser'),
            'lien' => $this->router->generateUri('Stock.DemandeService.visualiser', ['noDs' => $entity['NO_DS']]),
        ];

        if (RulesHelper::hasBonSortieAccessRights($this->pdo) && $isDsASatisfaire && $entity['TYPE_DEM'] === DemandeServiceTypeEnum::S->name) {
            $menuContextuel[$aSatisfaireListe][] = [
                'icone' => 'icosq-mg-good-issue-slip',
                'libelle' => $this->trad->t('Créer un bon de sortie'),
                'lien' => false,
                'lienJs' => 'nouveauBon(' . join(',', [$entity['NO_DS'], json_encode($this->router->generateUri('Stock.BonSortie.nouveauForm'))]) . ');',
            ];
        }

        if (RulesHelper::hasBonTransfertAccessRights($this->pdo) && $isDsASatisfaire && $entity['TYPE_DEM'] === DemandeServiceTypeEnum::T->name) {
            $menuContextuel[$aSatisfaireListe][] = [
                'icone' => 'icosq-mg-transfer-order',
                'libelle' => $this->trad->t('Créer un bon de transfert'),
                'lien' => false,
                'lienJs' => 'nouveauBon(' . join(',', [$entity['NO_DS'], json_encode($this->router->generateUri('Stock.BonTransfert.nouveauForm')),]) . ');',
            ];
        }

        if (Roles::droitsUtilisateur($this->pdo, 'ST_DS_VAL') && $isDsAValider) {
            $menuContextuel[$aValiderListe][] = [
                'icone' => 'pictoValidationManagement',
                'libelle' => $this->trad->t('Valider'),
                'lien' => false,
                'lienJs' => 'popupConfirmValiderEntete('
                    . join(',', [
                        json_encode(Trad::strBuild($this->trad->t('Validation de la demande de service N° @1@'), $entity['NO_DS'])),
                        json_encode(Trad::strBuild($this->trad->t('Etes-vous sûr de vouloir valider la demande de service N°@1@ ?'), $entity['NO_DS'])),
                        json_encode($this->router->generateUri('Stock.DemandeService.valider', ['noDs' => $entity['NO_DS']])),
                    ]) . ');',
            ];
        }

        if ($isDsAValider) {
            $menuContextuel[$aValiderListe][] = [
                'libelle' => $this->trad->t('Modifier'),
                'icone' => 'modifier_v2',
                'lien' => $this->router->generateUri('Stock.DemandeService.modifier', ['noDs' => $entity['NO_DS']]),
            ];
        }

        if (Roles::droitsUtilisateur($this->pdo, 'ST_DS-SUP') && $entity['SOLDE_DS'] === SoldeDemandeServiceEnum::A->name) {
            $menuContextuel['listeSecondaire'][] = [
                'libelle' => $this->trad->t('Supprimer'),
                'lienJs' => 'popupConfirmSimple('
                    . join(',', [
                        json_encode(Trad::strBuild($this->trad->t('Suppression de la demande de service N° @1@'), $entity['NO_DS'])),
                        json_encode(Trad::strBuild($this->trad->t('Etes-vous sûr de vouloir supprimer la demande de service N°@1@ ?'), $entity['NO_DS'])),
                        json_encode($this->router->generateUri('Stock.DemandeService.supprimer', ['noDs' => $entity['NO_DS']])),
                        '{}',
                        '() => {window.action.arianeReload()}'
                    ]) . ');',
            ];
        }

        if (Roles::droitsUtilisateur($this->pdo, 'ST_DS_DEV') && $entity['SOLDE_DS'] === SoldeDemandeServiceEnum::E->name) {
            $menuContextuel['listeSecondaire'][] = [
                'libelle' => $this->trad->t('Dévalider'),
                'lien' => false,
                'lienJs' => "devaliderDemandeService('".($this->router->generateUri('Stock.DemandeService.devalider', ['noDs' => $entity['NO_DS']]))."')",
            ];
        }

        if (Roles::droitsUtilisateur($this->pdo, 'ST_DS_SM') && $entity['SOLDE_DS'] === SoldeDemandeServiceEnum::E->name) {
            $menuContextuel['listeSecondaire'][] = [
                'libelle' => $this->trad->t('Solde manuel'),
                'lien' => false,
                'lienJs' => 'popupConfirmSimple('
                    . join(',', [
                        json_encode($this->trad->t('Solde manuel de la demande de service N° ' . $entity['NO_DS'])),
                        json_encode(Trad::strBuild($this->trad->t('Etes-vous sûr de vouloir solder la demande de service N°@1@ ?'), $entity['NO_DS'])),
                        json_encode($this->router->generateUri('Stock.DemandeService.soldeManuel', ['noDs' => $entity['NO_DS']])),
                        '{}',
                        '() => {window.action.arianeReload()}'
                    ]) . ');',
            ];
        }
        if (Roles::droitsUtilisateur($this->pdo, 'ST_DS_DM') && $entity['SOLDE_DS'] === SoldeDemandeServiceEnum::S->name) {
            $menuContextuel['listeSecondaire'][] = [
                'libelle' => $this->trad->t('Dé-solde manuel'),
                'lien' => false,
                'lienJs' => 'popupConfirmSimple('
                    . join(',', [
                        json_encode($this->trad->t('Dé-solde manuel de la demande de service N° ' . $entity['NO_DS'])),
                        json_encode(Trad::strBuild($this->trad->t('Etes-vous sûr de vouloir dé-solder la demande de service N°@1@ ?'), $entity['NO_DS'])),
                        json_encode($this->router->generateUri('Stock.DemandeService.desoldeManuel', ['noDs' => $entity['NO_DS']])),
                        '{}',
                        '() => {window.action.arianeReload()}'
                    ]) . ');',
            ];
        }
        if (Roles::droitsUtilisateur($this->pdo, 'ST_DS_IMP') && $entity['DTE_VALID'] !== '00000000') {
            $menuContextuel['listeSecondaire'][] = [
                'target' => '_blank',
                'libelle' => $this->trad->t('Imprimer'),
                'lien' => $this->router->generateUri('Stock.DemandeService.imprimer', ['noDs' => $entity['NO_DS']]),
            ];
        }

        return $menuContextuel;
    }

    /**
     * @param array<string,mixed> $droits
     * @return $this
     */
    public function setDroits(array $droits): self
    {
        $this->droit = $droits;

        return $this;
    }
}
