-- =====================================================
-- SCRIPT DE PERMISOS PARA MÓDULO DE NOVEDADES
-- Sistema: SmartPOS-Cloud v3 - HRM
-- =====================================================

-- 1. INSERTAR PERMISOS
INSERT INTO permissions (name, display_name, description, created_at, updated_at) VALUES
-- Service Centers
('service_centers_view', 'Ver Centros de Servicio', 'Permite visualizar centros de servicio', NOW(), NOW()),
('service_centers_create', 'Crear Centros de Servicio', 'Permite crear nuevos centros de servicio', NOW(), NOW()),
('service_centers_update', 'Editar Centros de Servicio', 'Permite modificar centros de servicio', NOW(), NOW()),
('service_centers_delete', 'Eliminar Centros de Servicio', 'Permite eliminar centros de servicio', NOW(), NOW()),

-- Novelty Types
('novelty_types_view', 'Ver Tipos de Novedad', 'Permite visualizar tipos de novedad', NOW(), NOW()),
('novelty_types_create', 'Crear Tipos de Novedad', 'Permite crear nuevos tipos de novedad', NOW(), NOW()),
('novelty_types_update', 'Editar Tipos de Novedad', 'Permite modificar tipos de novedad', NOW(), NOW()),
('novelty_types_delete', 'Eliminar Tipos de Novedad', 'Permite eliminar tipos de novedad', NOW(), NOW()),

-- Novelties
('novelties_view', 'Ver Novedades', 'Permite visualizar novedades', NOW(), NOW()),
('novelties_create', 'Crear Novedades', 'Permite registrar nuevas novedades', NOW(), NOW()),
('novelties_update', 'Editar Novedades', 'Permite modificar novedades', NOW(), NOW()),
('novelties_delete', 'Eliminar Novedades', 'Permite eliminar novedades', NOW(), NOW()),
('novelties_validate', 'Validar Novedades', 'Permite validar novedades (Jefe de Operación)', NOW(), NOW()),
('novelties_approve', 'Aprobar Novedades', 'Permite aprobar novedades (Director de Talento)', NOW(), NOW()),
('novelties_export', 'Exportar Novedades', 'Permite exportar novedades a SIESA', NOW(), NOW());

-- =====================================================
-- 2. ASIGNAR PERMISOS A ROL ADMINISTRADOR (Opcional)
-- =====================================================

-- Obtener ID del rol Admin (ajustar según tu base de datos)
SET @admin_role_id = (SELECT id FROM roles WHERE name = 'Admin' LIMIT 1);

-- Asignar todos los permisos al rol Admin
INSERT INTO permission_role (permission_id, role_id)
SELECT id, @admin_role_id 
FROM permissions 
WHERE name LIKE 'service_centers_%' 
   OR name LIKE 'novelty_types_%' 
   OR name LIKE 'novelties_%';

-- =====================================================
-- 3. CREAR ROLES ESPECÍFICOS (Opcional)
-- =====================================================

-- Rol: Líder de Servicio
INSERT INTO roles (name, description, created_at, updated_at) 
VALUES ('Líder de Servicio', 'Puede registrar y ver novedades de su CDS', NOW(), NOW());

SET @leader_role_id = LAST_INSERT_ID();

INSERT INTO permission_role (permission_id, role_id)
SELECT id, @leader_role_id FROM permissions 
WHERE name IN (
    'service_centers_view',
    'novelties_view',
    'novelties_create',
    'novelties_update'
);

-- Rol: Jefe de Operación
INSERT INTO roles (name, description, created_at, updated_at) 
VALUES ('Jefe de Operación', 'Puede validar novedades de sus CDS', NOW(), NOW());

SET @manager_role_id = LAST_INSERT_ID();

INSERT INTO permission_role (permission_id, role_id)
SELECT id, @manager_role_id FROM permissions 
WHERE name IN (
    'service_centers_view',
    'novelties_view',
    'novelties_validate'
);

-- Rol: Director de Talento
INSERT INTO roles (name, description, created_at, updated_at) 
VALUES ('Director de Talento', 'Puede aprobar novedades y gestionar configuración', NOW(), NOW());

SET @director_role_id = LAST_INSERT_ID();

INSERT INTO permission_role (permission_id, role_id)
SELECT id, @director_role_id FROM permissions 
WHERE name LIKE 'service_centers_%' 
   OR name LIKE 'novelty_types_%' 
   OR name IN ('novelties_view', 'novelties_approve');

-- Rol: Tesorería
INSERT INTO roles (name, description, created_at, updated_at) 
VALUES ('Tesorería', 'Puede exportar novedades aprobadas', NOW(), NOW());

SET @treasury_role_id = LAST_INSERT_ID();

INSERT INTO permission_role (permission_id, role_id)
SELECT id, @treasury_role_id FROM permissions 
WHERE name IN (
    'novelties_view',
    'novelties_export'
);

-- =====================================================
-- 4. VERIFICAR PERMISOS CREADOS
-- =====================================================

SELECT * FROM permissions 
WHERE name LIKE 'service_centers_%' 
   OR name LIKE 'novelty_types_%' 
   OR name LIKE 'novelties_%'
ORDER BY name;

-- =====================================================
-- FIN DEL SCRIPT
-- =====================================================
