<?php

declare(strict_types=1);

namespace App\Exports;

use App\Models\DisciplinaryReport;
use Illuminate\Database\Eloquent\Builder;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class ReportsExport implements FromQuery, WithHeadings, WithMapping
{
    /**
     * @param  array<int, int>|null  $ids
     */
    public function __construct(
        private readonly ?array $ids = null,
    ) {}

    public function query(): Builder
    {
        $query = DisciplinaryReport::query()->with(['employee', 'creator']);

        if ($this->ids !== null) {
            $query->whereIn('id', $this->ids);
        }

        return $query->orderByDesc('report_date');
    }

    /**
     * @return array<int, string>
     */
    public function headings(): array
    {
        return [
            'Employee Number',
            'Employee Name',
            'Report Date',
            'Title',
            'Description',
            'Severity',
            'Created By',
        ];
    }

    /**
     * @param  DisciplinaryReport  $report
     * @return array<int, mixed>
     */
    public function map($report): array
    {
        return [
            $report->employee?->employee_number,
            $report->employee?->full_name,
            $report->report_date?->toDateString(),
            $report->title,
            $report->description,
            $report->severity?->value,
            $report->creator?->name,
        ];
    }
}
