<?php

declare(strict_types=1);

namespace App\Exports;

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

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

    public function query(): Builder
    {
        $query = Bonus::query()->with('employee');

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

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

    /**
     * @return array<int, string>
     */
    public function headings(): array
    {
        return [
            'Employee Number',
            'Employee Name',
            'Bonus Date',
            'Bonus Type',
            'Amount',
            'Reason',
        ];
    }

    /**
     * @param  Bonus  $bonus
     * @return array<int, mixed>
     */
    public function map($bonus): array
    {
        return [
            $bonus->employee?->employee_number,
            $bonus->employee?->full_name,
            $bonus->bonus_date?->toDateString(),
            $bonus->bonus_type?->value,
            $bonus->amount,
            $bonus->reason,
        ];
    }
}
