<?php

declare(strict_types=1);

namespace Database\Factories;

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

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

        return [
            'user_id' => User::factory(),
            'device_hash' => LoginHistory::generateDeviceHash($userAgent, $ip),
            'device_name' => fake()->randomElement(['Chrome on Windows', 'Safari on macOS', 'Firefox on Linux', 'Chrome on Android']),
            'ip_address' => $ip,
            'last_used_at' => now(),
            'expires_at' => now()->addDays(30),
        ];
    }

    public function expired(): static
    {
        return $this->state(fn (array $attributes): array => [
            'expires_at' => now()->subDay(),
        ]);
    }
}
