<?php

namespace App\Exports;

use App\Models\Projet;
use App\Models\User;
use App\Models\Structure;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use TYPE_ACHAT_INVESTISSEMENT;
use TYPE_CALCUL_GAINS;

class RespArsPaatsExport implements FromCollection, WithHeadings, WithMapping
{
    private $annee_id;
    private $structure_id;

    public function __construct($annee_id, $structure_id)
    {
        $this->annee_id = $annee_id;
        $this->structure_id = $structure_id;
    }

    public function collection()
    {
        return Projet::getByPaatAndStructure($this->annee_id, $this->structure_id);
    }

    public function map($row): array
    {
        return [
            /* 1 */ $row->paat->annee_reference,
            /* 2 */ $row->reference,
            /* 3 */ $row->acheteur->nom_complet(),
            /* 4 */ $row->structure->nom,
            /* 5 */ $row->direction->nom,
            /* 6 */ $row->service->nom,
            /* 7 */ $row->description,
            /* 8 */ $row->objet,
            /* 9 */ $row->niveau_mutualisation->nom,
            /* 10 */ $row->operateur_mutualisation->nom,
            /* 11 */ $row->impact_budgetaire,
            /* 12 */ $row->imputation_budgetaire->nom,
            /* 13 */ $row->programmation ? 'Action programmée' : 'Action non programmée',
            /* 14 */ TYPE_CALCUL_GAINS::TYPE_CALCUL_GAINS_FORMATTED[$row->type_calcul_gains],
            /* 15 */ $row->ondam ? 'ONDAM' : 'Non ONDAM',
            /* 16 */ $row->referentiel,
            /* 17 */ $row->famille->nom ?? '',
            /* 18 */ $row->domaine->nom ?? '',
            /* 19 */ $row->categorie->nom ?? '',
            /* 20 */ $row->segment->nom ?? '',
            /* 21 */ $row->libelle_marche_a_renouveler,
            /* 22 */ $row->numero_marche_a_renouveler,
            /* 23 */ $row->libelle_nouveau_marche,
            /* 24 */ $row->numero_nouveau_marche,
            /* 25 */ $row->date_debut,
            /* 26 */ $row->date_fin,
            /* 27 */ implode(', ', $row->leviers->map(function ($levier) {
                return $levier->nom;
            })->toArray()),
            /* 28 */ implode(', ', $row->actions->map(function ($action) {
                return $action->thematique->nom . ' - ' . $action->nom;
            })->toArray()),
            /* 29 */ sizeof($row->statut) > 0 ? $row->statut->first()->nom : '',
            /* 30 */ implode(', ', $row->membres->map(function ($membre) {
                return $membre->nom_complet();
            })->toArray()),
            /* 31 */ implode(', ', $row->taches->map(function ($tache) {
                return $tache->nom . ' (' . $tache->nb_jours . ' jours)';
            })->toArray()),
            /* 32 */ $row->niveau_priorite,
            /* 33 */ $row->montant_reference,
            /* 34 */ $row->montant_notifie,
            /* 35 */ $row->montant_realise,
            /* 36 */ $row->pourcentage_gains_cible,
            /* 37 */ $row->gains_cible,
            /* 38 */ $row->gains_notifie,
            /* 39 */ $row->gains_securise,
            /* 40 */ TYPE_ACHAT_INVESTISSEMENT::TYPE_ACHAT_INVESTISSEMENT_FORMATTED[$row->type_achat_investissement],
            /* 41 */ $row->moyenne_offre,
            /* 42 */ $row->duree_mois,
            /* 43 */ $row->commentaire,
            /* 44 */ $row->prochaine_echeance,
            /* 45 */ $row->prochaine_etape,
            /* 46 */ $row->etat_avancement,
            /* 47 */ $row->risque
        ];
    }

    public function headings(): array
    {
        $champ_referentiel = Structure::find(Auth::user()->structure_id)->champ_referentiel;
        return [
            /* 1 */ 'Année PAAT',
            /* 2 */ 'Référence',
            /* 3 */ 'Acheteur',
            /* 4 */ 'Structure',
            /* 5 */ 'Etablissement',
            /* 6 */ 'Service',
            /* 7 */ 'Description',
            /* 8 */ 'Objet',
            /* 9 */ 'Mutualisation',
            /* 10 */ 'Centrale d\'achat',
            /* 11 */ 'Impact budgétaire',
            /* 12 */ 'Imputation budgétaire',
            /* 13 */ 'Programmation',
            /* 14 */ 'Calcul des gains',
            /* 15 */ 'Objectifs de l\'Assurance Maladie',
            /* 16 */ $champ_referentiel,
            /* 17 */ 'Famille',
            /* 18 */ 'Domaine',
            /* 19 */ 'Catégorie',
            /* 20 */ 'Segment',
            /* 21 */ 'Ancien libellé marché',
            /* 22 */ 'Ancien numéro marché',
            /* 23 */ 'Nouveau libellé marché',
            /* 24 */ 'Nouveau numéro marché',
            /* 25 */ 'Date de début',
            /* 26 */ 'Date de fin',
            /* 27 */ 'Leviers',
            /* 28 */ 'RSE',
            /* 29 */ 'Statut',
            /* 30 */ 'Membres',
            /* 31 */ 'Tâches',
            /* 32 */ 'Niveau priorité',
            /* 33 */ 'Montant référence',
            /* 34 */ 'Montant notifié',
            /* 35 */ 'Montant réalisé',
            /* 36 */ 'Pourcentage gains cible',
            /* 37 */ 'Gains cible',
            /* 38 */ 'Gains notifié',
            /* 39 */ 'Gains sécurisé',
            /* 40 */ 'Type achat investissement',
            /* 41 */ 'Moyenne offre',
            /* 42 */ 'Durée (en mois)',
            /* 43 */ 'Commentaire',
            /* 44 */ 'Prochaine échéance',
            /* 45 */ 'Prochaine étape',
            /* 46 */ 'Etat avancement',
            /* 47 */ 'Risque'
        ];
    }
}
