<?php

namespace Database\Seeders;

use App\Models\Application;
use Illuminate\Database\Seeder;

class PdfLayoutDemoSeeder extends Seeder
{
    public function run(): void
    {
        $dependents = collect([
            ['Isidora Belén González Peña', 21_100_001, '2012-01-15', 'Hija'],
            ['Benjamín Andrés González Peña', 22_200_002, '2014-03-22', 'Hijo'],
            ['Josefa Antonia González Peña', 23_300_003, '2016-05-09', 'Hija'],
            ['Agustín Matías González Peña', 24_400_004, '2018-07-18', 'Hijo'],
            ['Emilia Sofía González Peña', 25_500_005, '2020-09-27', 'Hija'],
            ['María José Peña Ñanculef', 16_600_006, '1988-11-04', 'Cónyuge'],
        ])->map(fn ($dependent, $index) => [
            'id' => 'pdf-demo-dependent-'.($index + 1),
            'full_name' => $dependent[0],
            'rut' => $this->rut($dependent[1]),
            'birth_date' => $dependent[2],
            'relationship' => $dependent[3],
        ])->all();

        $data = [
            'application_date' => now()->format('Y-m-d'),
            'payment_amount' => '$ 35.000',
            'receipt_number' => 'PDF-DEMO-0001',
            'first_payment_date' => now()->addDays(2)->format('Y-m-d'),
            'full_name' => 'Francisco Javier González Muñoz',
            'rut' => $this->rut(15_345_678),
            'birth_date' => '1985-06-14',
            'email' => 'francisco.gonzalez@example.cl',
            'address' => 'Avenida España 1456, Calama',
            'phone' => '+56 9 8765 4321',
            'delivery_address' => 'Pasaje Los Peumos 742, Villa Ascotán, Calama',
            'profession' => 'Ingeniero en Ejecución',
            'work_area' => 'Mantenimiento y confiabilidad operacional',
            'superintendency' => 'Mantenimiento Mina',
            'work_phone' => '+56 55 234 9876',
            'management' => 'Gerencia de Operaciones',
            'work_email' => 'francisco.gonzalez@empresa.cl',
            'dependents' => $dependents,
            'recommender_name' => 'María Fernanda Núñez Díaz',
            'recommender_rut' => $this->rut(12_987_654),
            'recommender_signature' => '',
            'board_members' => [
                ['id' => 'pdf-demo-board-1', 'name' => 'Alejandra del Carmen Muñoz', 'rut' => $this->rut(9_876_543), 'signature' => ''],
                ['id' => 'pdf-demo-board-2', 'name' => 'Cristóbal Andrés Núñez', 'rut' => $this->rut(10_765_432), 'signature' => ''],
            ],
            'signature_date' => now()->addDays(3)->format('Y-m-d'),
            'signature' => '',
        ];

        Application::updateOrCreate(['folio' => 'CCR-PDF-DEMO-0001'], [
            'type' => 'particular',
            'status' => 'pending',
            'full_name' => $data['full_name'],
            'rut' => $data['rut'],
            'email' => $data['email'],
            'phone' => $data['phone'],
            'application_date' => $data['application_date'],
            'data' => $data,
            'attachments' => [],
            'admin_notes' => 'Registro de demostración para verificar la plantilla PDF completa.',
        ]);
    }

    private function rut(int $number): string
    {
        $sum = 0;
        $multiplier = 2;
        foreach (array_reverse(str_split((string) $number)) as $digit) {
            $sum += ((int) $digit) * $multiplier;
            $multiplier = $multiplier === 7 ? 2 : $multiplier + 1;
        }
        $result = 11 - ($sum % 11);
        $verification = $result === 11 ? '0' : ($result === 10 ? 'K' : (string) $result);
        return number_format($number, 0, ',', '.').'-'.$verification;
    }
}
