<?php

namespace App\Exports;

use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;

use Maatwebsite\Excel\Concerns\WithTitle;

class MembersExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize, WithTitle
{
    public function title(): string
    {
        return 'Association Sénégalaise Valls Icamp';
    }
    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        return User::where('role', 'membre')->get();
    }

    public function headings(): array
    {
        return [
            'Matricule',
            'Prénom',
            'Nom',
            'Genre',
            'Date Naissance',
            'Lieu Naissance',
            'Nationalité',
            'Résidence',
            'Téléphone',
            'Email',
            'CNI',
            'Statut',
        ];
    }

    public function map($member): array
    {
        return [
            $member->registration_number,
            $member->first_name,
            $member->last_name,
            ucfirst($member->gender),
            $member->birth_date ? $member->birth_date->format('d/m/Y') : '',
            $member->birth_place,
            $member->nationality,
            $member->residence,
            $member->phone,
            $member->email,
            $member->cni,
            $member->status === 'active' ? 'Actif' : ($member->status === 'pending' ? 'En attente' : 'Inactif'),
        ];
    }
}
