<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class UserFactory extends Factory
{
    protected static ?string $password;

    public function definition(): array
    {
        return [
            'name' => fake()->name(),
            'username' => fake()->userName(),
            'email' => fake()->safeEmail(),
            'mobile' => fake()->phoneNumber(),
            'role' => 'employee',
            'birth_date' => $this->faker->dateTimeBetween('-70 years', '-18 years')->format('Y-m-d'),
            'address' => fake()->optional()->address(),
            'email_verified_at' => fake()->optional(0.8)->dateTimeThisYear(),
            'mobile_verified_at' => fake()->optional(0.5)->dateTimeThisYear(),
            'password' => static::$password ??= Hash::make('password'),
            'password_changed_at' => now(),
            'remember_token' => Str::random(10),
            'is_active' => true, // 90% chance of being active
            'locale' => fake()->randomElement(['en', 'ar']),
            'created_at' => now(),
            'updated_at' => now(),
        ];
    }

    public function unverified(): static
    {
        return $this->state(fn (array $attributes) => [
            'email_verified_at' => null,
        ]);
    }

    public function active(): static
    {
        return $this->state(fn (array $attributes) => [
            'is_active' => true,
        ]);
    }

    public function inactive(): static
    {
        return $this->state(fn (array $attributes) => [
            'is_active' => false,
        ]);
    }

    public function admin(): static
    {
        return $this->state(fn (array $attributes) => [
            // Add admin-specific attributes if needed
        ]);
    }

    public function withPassword(string $password): static
    {
        return $this->state(fn (array $attributes) => [
            'password' => Hash::make($password),
        ]);
    }

    public function country(string $countryCode): static
    {
        return $this->state(fn (array $attributes) => [
            'country' => strtoupper($countryCode),
            'city' => fake()->city(),
        ]);
    }

    public function completeProfile(): static
    {
        return $this->state(fn (array $attributes) => [
            'birth_date' => $this->faker->dateTimeBetween('-70 years', '-18 years')->format('Y-m-d'),
            'address' => fake()->address(),
            'email_verified_at' => now(),
            'mobile_verified_at' => now(),
        ]);
    }
}
