\c cler_admin;

-- Limpiar permisos antiguos
DELETE FROM role_permissions;
DELETE FROM permissions;

-- Insertar permisos actualizados
INSERT INTO permissions (name, display_name, description, module) VALUES
-- Dashboard
('view_dashboard', 'Ver Dashboard', 'Acceso al panel principal', 'dashboard'),

-- Usuarios Admin
('view_admin_users', 'Ver Usuarios Admin', 'Listar usuarios administradores', 'admin'),
('create_admin_users', 'Crear Usuarios Admin', 'Crear nuevos administradores', 'admin'),
('edit_admin_users', 'Editar Usuarios Admin', 'Modificar administradores', 'admin'),
('delete_admin_users', 'Eliminar Usuarios Admin', 'Eliminar administradores', 'admin'),

-- Landing Page
('view_landing', 'Ver Landing Page', 'Ver contenido de landing', 'landing'),
('edit_landing', 'Editar Landing Page', 'Modificar contenido de landing', 'landing'),

-- Roles y Permisos
('view_roles', 'Ver Roles', 'Listar roles del sistema', 'admin'),
('manage_roles', 'Gestionar Roles', 'Crear y modificar roles y permisos', 'admin');

-- Asignar permisos a Super Admin (todos)
INSERT INTO role_permissions (role_id, permission_id)
SELECT 1, id FROM permissions;

-- Asignar permisos a Admin (todos excepto gestión de roles)
INSERT INTO role_permissions (role_id, permission_id)
SELECT 2, id FROM permissions WHERE name != 'manage_roles';

-- Asignar permisos a Moderador
INSERT INTO role_permissions (role_id, permission_id)
SELECT 3, id FROM permissions WHERE module IN ('dashboard', 'admin', 'landing') 
AND name NOT LIKE '%delete%' AND name != 'manage_roles';

-- Asignar permisos a Analista (solo lectura)
INSERT INTO role_permissions (role_id, permission_id)
SELECT 4, id FROM permissions WHERE name LIKE 'view_%';

-- Asignar permisos a Soporte
INSERT INTO role_permissions (role_id, permission_id)
SELECT 5, id FROM permissions WHERE name LIKE 'view_%';
