<?php

namespace App\Exports;

use App\Models\Project;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class ProjectsExport implements FromCollection, WithHeadings, WithMapping
{
    protected $projects;

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

    public function collection()
    {
        return $this->projects;
    }

    public function headings(): array
    {
        return [
            'ID',
            'Nom du Projet',
            'Description',
            'Type de Cotisation',
            'Montant',
            'Fréquence',
            'Date de Début',
            'Date de Fin',
            'Nombre de Membres',
            'Statut',
            'Créé le',
        ];
    }

    public function map($project): array
    {
        return [
            $project->id,
            $project->name,
            $project->description,
            $project->contribution_type === 'fixe' ? 'Fixe' : 'Variable',
            $project->amount,
            ucfirst($project->frequency),
            $project->start_date->format('d/m/Y'),
            $project->end_date ? $project->end_date->format('d/m/Y') : '-',
            $project->members->count(),
            $project->status === 'actif' ? 'Actif' : 'Terminé',
            $project->created_at->format('d/m/Y H:i'),
        ];
    }
}
