<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\PermissionRegistrar;

class PermissionCatalogSeeder extends Seeder
{
    /**
     * Full permission catalogue, grouped by section.
     * Idempotent: updates group/description on existing permissions and
     * creates the missing ones. Does NOT change role assignments.
     */
    public function run(): void
    {
        $guard = config('auth.defaults.guard', 'web');

        $catalog = [
            'Proyectos' => [
                'view projects' => 'Ver listado y fichas de proyectos',
                'create projects' => 'Crear proyectos',
                'edit projects' => 'Editar datos del proyecto',
                'delete projects' => 'Eliminar proyectos',
                'export projects' => 'Exportar proyectos (Excel/PDF)',
            ],
            'Fases y progreso' => [
                'view phases' => 'Ver fases del proyecto',
                'manage phases' => 'Crear, editar, ordenar y eliminar fases',
                'update progress' => 'Actualizar el porcentaje de progreso',
            ],
            'Capas y elementos' => [
                'view layers' => 'Ver capas y elementos en el mapa',
                'upload layers' => 'Subir/importar capas',
                'edit layers' => 'Editar capas y elementos',
                'delete layers' => 'Eliminar capas/elementos',
            ],
            'Inspecciones' => [
                'view inspections' => 'Ver inspecciones e historial',
                'create inspections' => 'Registrar inspecciones',
                'edit inspections' => 'Editar inspecciones existentes',
                'delete inspections' => 'Eliminar inspecciones',
                'manage templates' => 'Gestionar plantillas de inspección',
            ],
            'Incidencias' => [
                'view issues' => 'Ver incidencias',
                'create issues' => 'Crear incidencias',
                'edit issues' => 'Editar, resolver y cerrar incidencias',
                'delete issues' => 'Eliminar incidencias',
            ],
            'Empresas' => [
                'view companies' => 'Ver empresas',
                'create companies' => 'Crear empresas',
                'edit companies' => 'Editar empresas',
                'delete companies' => 'Eliminar empresas',
                'assign companies' => 'Asignar empresas/roles a proyectos',
            ],
            'Usuarios' => [
                'view users' => 'Ver usuarios',
                'create users' => 'Crear usuarios',
                'edit users' => 'Editar usuarios',
                'delete users' => 'Eliminar usuarios',
                'assign users' => 'Asignar usuarios/roles a proyectos',
            ],
            'Roles' => [
                'manage roles' => 'Crear/editar/borrar roles y asignar permisos',
            ],
            'Informes' => [
                'view reports' => 'Ver panel de informes',
                'export reports' => 'Exportar informes',
            ],
            'Archivos' => [
                'view media' => 'Ver archivos/galería',
                'upload media' => 'Subir archivos',
                'delete media' => 'Eliminar archivos',
            ],
            'General' => [
                'manage all' => 'Súper-admin: acceso total al sistema',
            ],
        ];

        foreach ($catalog as $group => $permissions) {
            foreach ($permissions as $name => $description) {
                Permission::updateOrCreate(
                    ['name' => $name, 'guard_name' => $guard],
                    ['group' => $group, 'description' => $description]
                );
            }
        }

        app(PermissionRegistrar::class)->forgetCachedPermissions();
    }
}
