<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\LoginHistory;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/** @extends Factory<LoginHistory> */
class LoginHistoryFactory extends Factory
{
    /** @return array<string, mixed> */
    public function definition(): array
    {
        $userAgent = fake()->userAgent();
        $ip = fake()->ipv4();

        return [
            'user_id' => User::factory(),
            'ip_address' => $ip,
            'user_agent' => $userAgent,
            'device_type' => fake()->randomElement(['Desktop', 'Phone', 'Tablet']),
            'browser' => fake()->randomElement(['Chrome', 'Firefox', 'Safari', 'Edge']),
            'os' => fake()->randomElement(['Windows 10', 'macOS', 'Ubuntu', 'iOS', 'Android']),
            'location_country' => fake()->country(),
            'location_city' => fake()->city(),
            'success' => true,
            'two_factor_used' => false,
            'is_new_device' => false,
            'device_hash' => LoginHistory::generateDeviceHash($userAgent, $ip),
        ];
    }

    public function failed(): static
    {
        return $this->state(fn (array $attributes): array => [
            'success' => false,
        ]);
    }

    public function newDevice(): static
    {
        return $this->state(fn (array $attributes): array => [
            'is_new_device' => true,
        ]);
    }
}
