<?php

namespace Database\Factories;

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

/**
 * @extends Factory<User>
 */
class UserFactory extends Factory
{
    /**
     * @var list<string>
     */
    private static array $nombres = [
        'Carlos', 'Juan', 'María', 'Ana', 'Luis', 'Rosa', 'Pedro', 'Carmen',
        'José', 'Patricia', 'Miguel', 'Sandra', 'Jorge', 'Gloria', 'Roberto',
    ];

    /**
     * @var list<string>
     */
    private static array $apellidos = [
        'García', 'López', 'Martínez', 'Rodríguez', 'González', 'Pérez',
        'Sánchez', 'Ramírez', 'Torres', 'Flores', 'Rivera', 'Díaz',
        'Morales', 'Reyes', 'Cruz',
    ];

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'document' => $this->faker->unique()->numerify('########'),
            'email' => $this->faker->unique()->safeEmail(),
            'name' => $this->faker->randomElement(self::$nombres),
            'paternal_surname' => $this->faker->randomElement(self::$apellidos),
            'maternal_surname' => $this->faker->randomElement(self::$apellidos),
            'phone' => $this->faker->numerify('9########'),
            'password' => Hash::make('password'),
            'status' => true,
            'two_factor_secret' => null,
            'two_factor_recovery_codes' => null,
            'two_factor_confirmed_at' => null,
        ];
    }

    public function withTwoFactor(): static
    {
        return $this->state(function (array $attributes) {
            $encrypter = Fortify::currentEncrypter();

            return [
                'two_factor_secret' => $encrypter->encrypt('JBSWY3DPEHPK3PXP'),
                'two_factor_recovery_codes' => $encrypter->encrypt(json_encode([
                    'aaaaaaaa',
                    'bbbbbbbb',
                    'cccccccc',
                    'dddddddd',
                    'eeeeeeee',
                    'ffffffff',
                    'gggggggg',
                    'hhhhhhhh',
                ])),
                'two_factor_confirmed_at' => now(),
            ];
        });
    }
}
