<?php

namespace Database\Factories;

use App\Enums\UserStatus;
use App\Enums\UserType;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
 */
class UserFactory extends Factory
{
    /**
     * The current password being used by the factory.
     */
    protected static ?string $password;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'user_type' => UserType::Individual,
            'full_name' => fake()->name(),
            'national_id' => fake()->numerify('#########'),
            'email' => fake()->unique()->safeEmail(),
            'phone' => fake()->phoneNumber(),
            'password' => static::$password ??= Hash::make('password'),
            'status' => UserStatus::Active,
            'preferred_language' => fake()->randomElement(['ar', 'en']),
            'email_verified_at' => now(),
            'remember_token' => Str::random(10),
        ];
    }

    /**
     * Indicate that the model's email address should be unverified.
     */
    public function unverified(): static
    {
        return $this->state(fn (array $attributes) => [
            'email_verified_at' => null,
        ]);
    }

    /**
     * Create an admin user.
     */
    public function admin(): static
    {
        return $this->state(fn (array $attributes) => [
            'user_type' => UserType::Admin,
            'company_name' => null,
            'company_cert_number' => null,
            'contact_person_name' => null,
            'contact_person_id' => null,
        ]);
    }

    /**
     * Create an individual client user.
     */
    public function individual(): static
    {
        return $this->state(fn (array $attributes) => [
            'user_type' => UserType::Individual,
            'company_name' => null,
            'company_cert_number' => null,
            'contact_person_name' => null,
            'contact_person_id' => null,
        ]);
    }

    /**
     * Create a company client user.
     */
    public function company(): static
    {
        return $this->state(fn (array $attributes) => [
            'user_type' => UserType::Company,
            'national_id' => null,
            'company_name' => fake()->company(),
            'company_cert_number' => fake()->numerify('CR-######'),
            'contact_person_name' => fake()->name(),
            'contact_person_id' => fake()->numerify('#########'),
        ]);
    }

    /**
     * Create a client user (individual or company).
     */
    public function client(): static
    {
        return fake()->boolean()
            ? $this->individual()
            : $this->company();
    }

    /**
     * Create a deactivated user.
     */
    public function deactivated(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => UserStatus::Deactivated,
        ]);
    }

    /**
     * Indicate that the model has two-factor authentication configured.
     */
    public function withTwoFactor(): static
    {
        return $this->state(fn (array $attributes) => [
            'two_factor_secret' => Str::random(10),
            'two_factor_recovery_codes' => Str::random(10),
            'two_factor_confirmed_at' => now(),
        ]);
    }

    /**
     * Indicate that the model does not have two-factor authentication configured.
     */
    public function withoutTwoFactor(): static
    {
        return $this->state(fn (array $attributes) => [
            'two_factor_secret' => null,
            'two_factor_recovery_codes' => null,
            'two_factor_confirmed_at' => null,
        ]);
    }
}
