<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Enums\EmploymentStatus;
use App\Enums\Gender;
use App\Models\Department;
use App\Models\Employee;
use App\Models\Position;
use App\Models\Shift;
use App\Support\Countries;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<Employee>
 */
class EmployeeFactory extends Factory
{
    protected $model = Employee::class;

    /**
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $hireDate = fake()->dateTimeBetween('-10 years', '-1 month');
        $birthDate = fake()->dateTimeBetween('-55 years', '-20 years');
        $employmentStatus = fake()->randomElement(EmploymentStatus::cases());
        $gender = fake()->randomElement(Gender::cases());

        return [
            'employee_number' => '',
            'full_name' => fake()->name($gender === Gender::Female ? 'female' : 'male'),
            'national_id' => fake()->optional(0.8)->randomElement(Countries::codes()),
            'gender' => $gender,
            'birth_date' => $birthDate,
            'phone' => '+993 61 92 92 48',
            'address' => fake()->optional()->address(),
            'department_id' => Department::factory(),
            'position_id' => Position::factory(),
            'shift_id' => Shift::factory(),
            'employment_status' => $employmentStatus,
            'hire_date' => $hireDate,
            'termination_date' => $employmentStatus === EmploymentStatus::Terminated
                ? fake()->dateTimeBetween($hireDate, 'now')
                : null,
            'notes' => fake()->optional(0.3)->sentence(),
            'profile_photo_path' => null,
        ];
    }

    public function active(): static
    {
        return $this->state(fn (array $attributes): array => [
            'employment_status' => EmploymentStatus::Active,
            'termination_date' => null,
        ]);
    }

    public function terminated(): static
    {
        return $this->state(function (array $attributes): array {
            $hireDate = $attributes['hire_date'] ?? fake()->dateTimeBetween('-5 years', '-1 year');

            return [
                'employment_status' => EmploymentStatus::Terminated,
                'termination_date' => fake()->dateTimeBetween($hireDate, 'now'),
            ];
        });
    }
}
