<?php

declare(strict_types=1);

namespace Database\Factories;

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

/**
 * @extends Factory<Vacation>
 */
class VacationFactory extends Factory
{
    protected $model = Vacation::class;

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

        return [
            'employee_id' => Employee::factory(),
            'type' => fake()->randomElement(VacationType::cases()),
            'start_date' => $startDate,
            'end_date' => $endDate,
            'return_date' => (clone $endDate)->modify('+1 day'),
            'days' => $days,
            'status' => $status,
            'reason' => fake()->optional()->sentence(),
            'attachment_path' => null,
            '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,
        ]);
    }

    public function approved(): static
    {
        return $this->state(fn (array $attributes): array => [
            'status' => ApprovalStatus::Approved,
            'approved_by' => User::factory(),
            'approved_at' => now(),
        ]);
    }
}
