<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        if (!Schema::hasTable('menus')) {
            return;
        }

        // Si ya existen menus, no tocar.
        if (DB::table('menus')->count() > 0) {
            return;
        }

        // Inserta un set minimo para que el sidebar no quede vacio.
        $now = now();

        // Dashboard
        DB::table('menus')->insert([
            'nombre' => 'Dashboard',
            'route_name' => 'dashboard',
            'url' => null,
            'route_pattern' => 'dashboard',
            'icon' => 'fas fa-home',
            'parent_id' => null,
            'orden' => 1,
            'estado' => 'AC',
            'created_at' => $now,
            'updated_at' => $now,
        ]);

        // Contenedor: Administrar
        $adminId = DB::table('menus')->insertGetId([
            'nombre' => 'Administrar',
            'route_name' => null,
            'url' => null,
            'route_pattern' => null,
            'icon' => 'fas fa-cog',
            'parent_id' => null,
            'orden' => 2,
            'estado' => 'AC',
            'created_at' => $now,
            'updated_at' => $now,
        ]);

        DB::table('menus')->insert([
            [
                'nombre' => 'Sede',
                'route_name' => 'admin.sede.index',
                'url' => null,
                'route_pattern' => 'admin.sede.*',
                'icon' => 'fas fa-building',
                'parent_id' => $adminId,
                'orden' => 0,
                'estado' => 'AC',
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'nombre' => 'Tipo Usuario',
                'route_name' => 'admin.tipo-usuario.index',
                'url' => null,
                'route_pattern' => 'admin.tipo-usuario.*',
                'icon' => 'fas fa-tag',
                'parent_id' => $adminId,
                'orden' => 1,
                'estado' => 'AC',
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'nombre' => 'Usuarios',
                'route_name' => 'admin.usuario.index',
                'url' => null,
                'route_pattern' => 'admin.usuario.*',
                'icon' => 'fas fa-users',
                'parent_id' => $adminId,
                'orden' => 2,
                'estado' => 'AC',
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'nombre' => 'Menus',
                'route_name' => 'admin.menu.index',
                'url' => null,
                'route_pattern' => 'admin.menu.*',
                'icon' => 'fas fa-sitemap',
                'parent_id' => $adminId,
                'orden' => 3,
                'estado' => 'AC',
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'nombre' => 'Privilegios',
                'route_name' => 'admin.privilegios.index',
                'url' => null,
                'route_pattern' => 'admin.privilegios.*',
                'icon' => 'fas fa-key',
                'parent_id' => $adminId,
                'orden' => 4,
                'estado' => 'AC',
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'nombre' => 'Auditoría',
                'route_name' => 'admin.auditoria.index',
                'url' => null,
                'route_pattern' => 'admin.auditoria.*',
                'icon' => 'fas fa-clipboard-list',
                'parent_id' => $adminId,
                'orden' => 99,
                'estado' => 'AC',
                'created_at' => $now,
                'updated_at' => $now,
            ],
        ]);

        // Perfil
        DB::table('menus')->insert([
            'nombre' => 'Perfil',
            'route_name' => 'profile.edit',
            'url' => null,
            'route_pattern' => 'profile.*',
            'icon' => 'fas fa-user',
            'parent_id' => null,
            'orden' => 3,
            'estado' => 'AC',
            'created_at' => $now,
            'updated_at' => $now,
        ]);
    }

    public function down(): void
    {
        // No se revierte automaticamente: los menus suelen ser datos de negocio.
    }
};
