<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Enums\ApprovalStatus;
use App\Models\Employee;
use App\Models\UnpaidLeave;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<UnpaidLeave>
 */
class UnpaidLeaveFactory extends Factory
{
    protected $model = UnpaidLeave::class;

    /**
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $startDate = fake()->dateTimeBetween('-3 months', '+1 month');
        $days = fake()->numberBetween(1, 30);
        $endDate = (clone $startDate)->modify("+{$days} days");
        $status = fake()->randomElement(ApprovalStatus::cases());

        return [
            'employee_id' => Employee::factory(),
            'start_date' => $startDate,
            'end_date' => $endDate,
            'days' => $days,
            'reason' => fake()->optional()->sentence(),
            'status' => $status,
            'approved_by' => $status === ApprovalStatus::Pending ? null : User::factory(),
            'approved_at' => $status === ApprovalStatus::Pending ? null : fake()->dateTimeBetween($startDate, 'now'),
        ];
    }

    public function pending(): static
    {
        return $this->state(fn (array $attributes): array => [
            'status' => ApprovalStatus::Pending,
            'approved_by' => null,
            'approved_at' => null,
        ]);
    }
}
