<?php

namespace Database\Factories;

use App\enum\ProviderType;
use App\enum\UsernameType;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Username>
 */
class UsernameFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'username' => $this->faker->unique()->userName(),
            'is_active' => $this->faker->boolean(80), // 80% chance of being active
            'daily_mailbox_limit' => $this->faker->numberBetween(50, 500),
            'username_type' => $this->faker->randomElement(UsernameType::class),
            'provider_type' => $this->faker->randomElement(ProviderType::class),
            'starts_at' => $this->faker->optional(0.3)->dateTimeBetween('-1 year', 'now'), // 30% chance of having start date
            'ends_at' => $this->faker->optional(0.2)->dateTimeBetween('now', '+2 years'), // 20% chance of having end date
            'last_used_at' => $this->faker->optional(0.7)->dateTimeBetween('-1 month', 'now'), // 70% chance of being used
            'checked_at' => $this->faker->optional(0.8)->dateTimeBetween('-1 week', 'now'), // 80% chance of being checked
        ];
    }

    /**
     * Indicate that the username is active.
     */
    public function active(): static
    {
        return $this->state(fn (array $attributes) => [
            'is_active' => true,
        ]);
    }

    /**
     * Indicate that the username is inactive.
     */
    public function inactive(): static
    {
        return $this->state(fn (array $attributes) => [
            'is_active' => false,
        ]);
    }

    /**
     * Indicate that the username is public.
     */
    public function public(): static
    {
        return $this->state(fn (array $attributes) => [
            'username_type' => UsernameType::PUBLIC,
        ]);
    }

    /**
     * Indicate that the username is premium.
     */
    public function premium(): static
    {
        return $this->state(fn (array $attributes) => [
            'username_type' => UsernameType::PREMIUM,
        ]);
    }

    /**
     * Indicate that the username is for Gmail.
     */
    public function gmail(): static
    {
        return $this->state(fn (array $attributes) => [
            'provider_type' => ProviderType::GMAIL,
        ]);
    }

    /**
     * Indicate that the username is for Yahoo.
     */
    public function yahoo(): static
    {
        return $this->state(fn (array $attributes) => [
            'provider_type' => ProviderType::YAHOO,
        ]);
    }

    /**
     * Indicate that the username is for Outlook.
     */
    public function outlook(): static
    {
        return $this->state(fn (array $attributes) => [
            'provider_type' => ProviderType::OUTLOOK,
        ]);
    }

    /**
     * Indicate that the username is for custom provider.
     */
    public function custom(): static
    {
        return $this->state(fn (array $attributes) => [
            'provider_type' => ProviderType::CUSTOM,
        ]);
    }

    /**
     * Indicate that the username has expiration dates.
     */
    public function withExpiration(): static
    {
        return $this->state(fn (array $attributes) => [
            'starts_at' => $this->faker->dateTimeBetween('-1 month', 'now'),
            'ends_at' => $this->faker->dateTimeBetween('now', '+1 year'),
        ]);
    }

    /**
     * Indicate that the username has been recently used.
     */
    public function recentlyUsed(): static
    {
        return $this->state(fn (array $attributes) => [
            'last_used_at' => $this->faker->dateTimeBetween('-1 day', 'now'),
            'checked_at' => $this->faker->dateTimeBetween('-1 day', 'now'),
        ]);
    }
}
