-- =============================================================================
-- Migración 002 — Nuevos roles del sistema
-- Inserta los roles necesarios para el ERP multi-módulo.
-- Proyecto: WhatsApp ERP Multi-módulo
-- Fecha:    2026-04-16
-- IMPORTANTE: Usa INSERT IGNORE para no duplicar si ya existen.
-- =============================================================================

INSERT IGNORE INTO roles (name, slug, description, color, is_system, created_at, updated_at) VALUES
    ('Super Admin',   'superadmin',    'Acceso total al sistema incluyendo configuración técnica', '#dc3545', 1, NOW(), NOW()),
    ('Recepcionista', 'recepcionista', 'Turnero, recepción de pacientes y agenda',                 '#198754', 0, NOW(), NOW()),
    ('Bacteriólogo',  'bacteriologo',  'Análisis de muestras y carga de resultados (Oleada 2)',    '#0dcaf0', 0, NOW(), NOW()),
    ('Supervisor',    'supervisor',    'Acceso de solo lectura a reportes y dashboards',           '#fd7e14', 0, NOW(), NOW()),
    ('Operador Bot',  'operador_bot',  'Gestión de conversaciones WhatsApp',                       '#6f42c1', 0, NOW(), NOW()),
    ('Solo Lectura',  'readonly',      'Visualización de dashboards sin modificar datos',          '#6c757d', 0, NOW(), NOW());

-- Asignar permisos básicos al rol recepcionista
-- (se ajustan cuando el módulo Turnero esté disponible — Migración 003)
INSERT IGNORE INTO role_modules (role_id, module_slug, can_view, can_create, can_edit, can_delete, can_export)
SELECT r.id, 'lab_pacientes', 1, 1, 1, 0, 0
FROM roles r WHERE r.slug = 'recepcionista';

INSERT IGNORE INTO role_modules (role_id, module_slug, can_view, can_create, can_edit, can_delete, can_export)
SELECT r.id, 'lab_domicilios', 1, 0, 0, 0, 0
FROM roles r WHERE r.slug = 'recepcionista';

-- Supervisor: solo lectura en reportes y dashboard
INSERT IGNORE INTO role_modules (role_id, module_slug, can_view, can_create, can_edit, can_delete, can_export)
SELECT r.id, 'lab_dashboard', 1, 0, 0, 0, 0
FROM roles r WHERE r.slug = 'supervisor';

INSERT IGNORE INTO role_modules (role_id, module_slug, can_view, can_create, can_edit, can_delete, can_export)
SELECT r.id, 'lab_reportes', 1, 0, 0, 0, 1
FROM roles r WHERE r.slug = 'supervisor';

-- Operador Bot: solo WhatsApp
INSERT IGNORE INTO role_modules (role_id, module_slug, can_view, can_create, can_edit, can_delete, can_export)
SELECT r.id, 'whatsapp', 1, 1, 1, 0, 1
FROM roles r WHERE r.slug = 'operador_bot';

