<?php

namespace Database\Factories;

use App\Models\Cliente;
use App\Models\CredencialCliente;
use App\Models\Persona;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Cliente>
 */
class ClienteFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Cliente::class;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'dni' => (string) $this->faker->unique()->numberBetween(20000000, 45000000),
            'correo' => $this->faker->unique()->safeEmail(),
            'persona_id' => Persona::factory(),
            'baja_id' => 1,
            'credencialcliente_id' => CredencialCliente::factory(),
        ];
    }

    public function configure(): static
    {
        return $this->afterCreating(function (Cliente $cliente) {
            CredencialCliente::where('id', $cliente->credencialcliente_id)
                ->update([
                    'correo' => $cliente->correo,
                    'contra' => Hash::make($cliente->dni),
                ]);
        });
    }
}
