<?php

namespace App\Exports;

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

class GroupMembersExport implements FromCollection, WithHeadings, WithMapping
{
    protected Group $group;

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

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        return $this->group->donateurs()->get();
    }

    /**
     * Define the header row
     */
    public function headings(): array
    {
        return ['ID', 'Nom', 'Prénom', 'Email', 'Téléphone', 'Date d\'ajout'];
    }

    /**
     * Map each donateur to a row
     */
    public function map($donateur): array
    {
        return [
            $donateur->id,
            $donateur->nom,
            $donateur->prenom,
            $donateur->email ?: '-',
            $donateur->telephone ?: '-',
            $donateur->created_at->format('Y-m-d'),
        ];
    }
}