<?php

declare(strict_types=1);

namespace Database\Factories;

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

/**
 * Test data only. Factories are never used to create production accounts.
 *
 * @extends Factory<User>
 */
class UserFactory extends Factory
{
    /**
     * Hashing is the slowest part of creating a user, so the factory hashes the
     * shared test password once per process.
     */
    protected static ?string $password = null;

    // The return shape is inherited from Factory<User>::definition().
    public function definition(): array
    {
        return [
            'name' => fake()->name(),
            'email' => fake()->unique()->safeEmail(),
            'password' => static::$password ??= Hash::make('password'),
            'is_master' => false,
            'is_active' => true,
            'must_change_password' => false,
            'password_changed_at' => now(),
            'remember_token' => Str::random(10),
        ];
    }

    /**
     * A user who can also manage users and application settings.
     */
    public function master(): static
    {
        return $this->state(fn (array $attributes): array => [
            'is_master' => true,
        ]);
    }

    /**
     * A disabled account: cannot sign in, and is signed out mid-session.
     */
    public function disabled(): static
    {
        return $this->state(fn (array $attributes): array => [
            'is_active' => false,
        ]);
    }

    /**
     * An account whose password was reset by a master and not yet changed.
     */
    public function mustChangePassword(): static
    {
        return $this->state(fn (array $attributes): array => [
            'must_change_password' => true,
        ]);
    }
}
