<?php

namespace App\Exports;

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

class TrainingsExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize
{
    public function collection()
    {
        return Training::orderByDesc('start_date')->get();
    }

    public function map($training): array
    {
        return [
            $training->id,
            $training->name,
            optional($training->creation_date)->format('Y-m-d'),
            optional($training->start_date)->format('Y-m-d'),
            optional($training->end_date)->format('Y-m-d'),
            $training->location,
            $training->trainer_name,
            $training->supervisor_name,
            $training->expected_participants,
            count($training->participants ?? []),
            $training->remarks,
        ];
    }

    public function headings(): array
    {
        return [
            'Training ID',
            'Name',
            'Creation Date',
            'Start Date',
            'End Date',
            'Location',
            'Trainer',
            'Supervisor',
            'Expected Participants',
            'Registered Participants',
            'Remarks',
        ];
    }
}


