<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Enums\UserRole;
use App\Models\Branch;
use App\Models\Region;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

/**
 * @extends Factory<User>
 */
class UserFactory extends Factory
{
    /**
     * The current password being used by the factory.
     */
    protected static ?string $password;

    public function definition(): array
    {
        return [
            'name' => fake()->name(),
            'email' => fake()->unique()->safeEmail(),
            'email_verified_at' => now(),
            'password' => static::$password ??= Hash::make('password'),
            'remember_token' => Str::random(10),
            'role' => UserRole::BranchAdmin,
            'region_id' => null,
            'branch_id' => Branch::factory(),
            'is_active' => true,
        ];
    }

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

    public function superAdmin(): static
    {
        return $this->state(fn (array $attributes) => [
            'role' => UserRole::SuperAdmin,
            'region_id' => null,
            'branch_id' => null,
        ]);
    }

    public function regionAdmin(): static
    {
        return $this->state(fn (array $attributes) => [
            'role' => UserRole::RegionAdmin,
            'region_id' => Region::factory(),
            'branch_id' => null,
        ]);
    }

    public function branchAdmin(): static
    {
        return $this->state(fn (array $attributes) => [
            'role' => UserRole::BranchAdmin,
            'region_id' => null,
            'branch_id' => Branch::factory(),
        ]);
    }

    public function dataClerk(): static
    {
        return $this->state(fn (array $attributes) => [
            'role' => UserRole::DataClerk,
            'region_id' => null,
            'branch_id' => Branch::factory(),
        ]);
    }

    public function branchViewer(): static
    {
        return $this->state(fn (array $attributes) => [
            'role' => UserRole::Viewer,
            'region_id' => null,
            'branch_id' => Branch::factory(),
        ]);
    }

    public function regionViewer(): static
    {
        return $this->state(fn (array $attributes) => [
            'role' => UserRole::Viewer,
            'region_id' => Region::factory(),
            'branch_id' => null,
        ]);
    }
}
