<?php

namespace Database\Seeders;

use App\Models\Account;
use App\Models\User;
use App\Models\Zone;
use App\Models\AccessPoint;
use App\Models\ParkingSpace;
use App\Models\Visitor;
use App\Models\Vehicle;
use App\Models\AccessLog;
use App\Models\ParkingAssignment;
use App\Models\Incident;
use App\Models\AuthorizationRequest;
use App\Enums\UserRole;
use App\Enums\AccessType;
use App\Enums\AccessStatus;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // 1. Cuentas de Recinto
        $account1 = Account::create([
            'name' => 'Condominio Alto Las Condes',
            'address' => 'Av. Las Condes 12300',
            'city' => 'Santiago',
            'plan_type' => 'premium',
            'is_active' => true,
        ]);

        $account2 = Account::create([
            'name' => 'Logística Pudahuel Bodegas',
            'address' => 'Ruta 68, Km 12',
            'city' => 'Pudahuel',
            'plan_type' => 'enterprise',
            'is_active' => true,
        ]);

        // 2. Usuarios de Prueba
        // SuperAdmin
        User::create([
            'name' => 'Admin SaaS Global',
            'email' => 'superadmin@ojodeaguila.cl',
            'password' => Hash::make('admin123'),
            'role' => UserRole::SUPER_ADMIN,
            'is_active' => true,
            'rut' => '9.999.999-9',
            'phone' => '+56 9 9999 9999',
        ]);

        // Administrador de Recinto (Condominio Alto Las Condes)
        User::create([
            'name' => 'Carlos Valenzuela',
            'email' => 'admin@ojodeaguila.cl',
            'password' => Hash::make('admin123'),
            'role' => UserRole::CLIENT_ADMIN,
            'account_id' => $account1->id,
            'is_active' => true,
            'rut' => '12.876.543-2',
            'phone' => '+56 9 8888 8888',
        ]);

        // Guardia de Seguridad 1 (Condominio Alto Las Condes)
        $guard1 = User::create([
            'name' => 'Juan Pérez',
            'email' => 'guardia@ojodeaguila.cl',
            'password' => Hash::make('admin123'),
            'role' => UserRole::GUARD,
            'account_id' => $account1->id,
            'is_active' => true,
            'rut' => '15.432.109-8',
            'phone' => '+56 9 7777 7777',
        ]);

        // Guardia de Seguridad 2 (Condominio Alto Las Condes)
        User::create([
            'name' => 'María González',
            'email' => 'guardia2@ojodeaguila.cl',
            'password' => Hash::make('admin123'),
            'role' => UserRole::GUARD,
            'account_id' => $account1->id,
            'is_active' => true,
            'rut' => '16.765.432-K',
            'phone' => '+56 9 6666 6666',
        ]);

        // 3. Zonas del Recinto 1
        $zone1 = Zone::create(['account_id' => $account1->id, 'name' => 'Oficina 3B']);
        $zone2 = Zone::create(['account_id' => $account1->id, 'name' => 'Recepción']);
        $zone3 = Zone::create(['account_id' => $account1->id, 'name' => 'Departamento 12B']);
        $zone4 = Zone::create(['account_id' => $account1->id, 'name' => 'Bodega Norte']);
        $zone5 = Zone::create(['account_id' => $account1->id, 'name' => 'Estacionamiento 15']);

        // 4. Puntos de Acceso del Recinto 1
        $ap1 = AccessPoint::create(['account_id' => $account1->id, 'name' => 'Entrada Principal', 'type' => 'entry_exit']);
        $ap2 = AccessPoint::create(['account_id' => $account1->id, 'name' => 'Acceso Vehicular', 'type' => 'entry_exit']);

        // 5. Estacionamientos del Recinto 1
        for ($i = 1; $i <= 10; $i++) {
            ParkingSpace::create([
                'account_id' => $account1->id,
                'number' => 'Estacionamiento ' . $i,
                'type' => 'visitor',
                'is_occupied' => ($i === 5 || $i === 8) ? true : false,
            ]);
        }

        // 6. Visitantes
        $visitor1 = Visitor::create([
            'rut' => '12.345.678-9',
            'first_name' => 'Carlos',
            'last_name' => 'Muñoz Andrade',
            'birth_date' => '1985-05-12',
            'nationality' => 'Chilena',
        ]);

        $visitor2 = Visitor::create([
            'rut' => '15.654.321-K',
            'first_name' => 'María',
            'last_name' => 'López',
            'birth_date' => '1990-10-25',
            'nationality' => 'Chilena',
        ]);

        // 7. Vehículos
        $vehicle1 = Vehicle::create([
            'plate' => 'AB-CD-12',
            'type' => 'Auto',
            'brand' => 'Hyundai',
            'model' => 'Accent',
            'color' => 'Gris',
        ]);

        // 8. Registros de Acceso
        // Acceso Peatonal de hoy
        $log1 = AccessLog::create([
            'account_id' => $account1->id,
            'visitor_id' => $visitor1->id,
            'zone_id' => $zone1->id,
            'access_point_id' => $ap1->id,
            'guard_id' => $guard1->id,
            'type' => AccessType::PERSON,
            'status' => AccessStatus::AUTHORIZED,
            'entered_at' => now()->subMinutes(10),
            'observations' => 'Visita programada a oficina.',
        ]);

        // Acceso Vehicular de hoy
        $log2 = AccessLog::create([
            'account_id' => $account1->id,
            'visitor_id' => $visitor2->id,
            'vehicle_id' => $vehicle1->id,
            'zone_id' => $zone5->id,
            'access_point_id' => $ap2->id,
            'guard_id' => $guard1->id,
            'type' => AccessType::VEHICLE,
            'status' => AccessStatus::AUTHORIZED,
            'entered_at' => now()->subMinutes(20),
            'observations' => 'Ingreso con vehículo de visita.',
        ]);

        // Asignación de Estacionamiento 5
        $pSpace = ParkingSpace::where('account_id', $account1->id)->where('number', 'Estacionamiento 5')->first();
        if ($pSpace) {
            ParkingAssignment::create([
                'access_log_id' => $log2->id,
                'parking_space_id' => $pSpace->id,
                'assigned_at' => now()->subMinutes(20),
            ]);
            $pSpace->update(['is_occupied' => true]);
        }

        // 9. Incidencias
        Incident::create([
            'account_id' => $account1->id,
            'reporter_id' => $guard1->id,
            'title' => 'Portón Vehicular Principal Atascado',
            'description' => 'El motor del portón eléctrico vehicular principal se quedó pegado a medio abrir. Se llamó al técnico de mantenimiento de turno.',
            'severity' => 'high',
            'status' => 'open',
        ]);
    }
}
