<?php

namespace Database\Factories;

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 [
            'name' => fake()->name(),
            'email' => fake()->unique()->safeEmail(),
            'email_verified_at' => now(),
            'password' => static::$password ??= Hash::make('password'),
            'remember_token' => Str::random(10),
            'is_admin' => false,
            'auto_approve_apps' => fake()->boolean(30), // 30% chance of auto-approval
            'preferred_username' => null,
            'avatar' => null,
        ];
    }

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

    /**
     * Indicate that the user should be an admin.
     */
    public function admin(): static
    {
        return $this->state(fn (array $attributes) => [
            'is_admin' => true,
        ]);
    }

    /**
     * Indicate that the user has auto-approval enabled.
     */
    public function autoApprove(): static
    {
        return $this->state(fn (array $attributes) => [
            'auto_approve_apps' => true,
        ]);
    }

    /**
     * Indicate that the user has auto-approval disabled.
     */
    public function noAutoApprove(): static
    {
        return $this->state(fn (array $attributes) => [
            'auto_approve_apps' => false,
        ]);
    }

    /**
     * Indicate that the user has a preferred username.
     */
    public function withPreferredUsername(string $username = null): static
    {
        return $this->state(fn (array $attributes) => [
            'preferred_username' => $username ?? fake()->userName(),
        ]);
    }

    /**
     * Indicate that the user has an avatar.
     */
    public function withAvatar(string $avatar = null): static
    {
        return $this->state(fn (array $attributes) => [
            'avatar' => $avatar ?? 'avatars/' . fake()->uuid() . '.jpg',
        ]);
    }

    /**
     * Create a complete user profile with all optional fields.
     */
    public function complete(): static
    {
        return $this->state(fn (array $attributes) => [
            'preferred_username' => fake()->userName(),
            'avatar' => 'avatars/' . fake()->uuid() . '.jpg',
            'auto_approve_apps' => true,
        ]);
    }

    /**
     * Create a user with a specific password for testing.
     */
    public function withPassword(string $password): static
    {
        return $this->state(fn (array $attributes) => [
            'password' => Hash::make($password),
        ]);
    }

    /**
     * Create a user that was recently created (useful for testing verification flows).
     */
    public function recent(): static
    {
        return $this->state(fn (array $attributes) => [
            'created_at' => now()->subMinutes(5),
            'updated_at' => now()->subMinutes(5),
        ]);
    }

    /**
     * Create a user that's been around for a while.
     */
    public function established(): static
    {
        return $this->state(fn (array $attributes) => [
            'created_at' => now()->subDays(fake()->numberBetween(30, 365)),
            'updated_at' => now()->subDays(fake()->numberBetween(1, 30)),
        ]);
    }
}
