-- ============================================================
-- NICC Admin Menu - Migracion 0006
-- Agregar plugin de Aprobacion Masiva (publishadmin)
-- ============================================================

-- Paso 1: Obtener la categoria de Modulos NICC o Documentos
-- Si no existe, usar la primera categoria disponible

-- Agregar el item publishadmin a la categoria de documentos
INSERT OR IGNORE INTO admin_menu_items (
    category_id,
    code,
    plugin_name,
    name,
    description,
    icon,
    url,
    sort_order,
    is_active,
    is_dangerous,
    requires_confirmation
)
SELECT
    (SELECT id FROM admin_menu_categories WHERE code IN ('documents', 'nicc_modules', 'tools') ORDER BY
        CASE code
            WHEN 'documents' THEN 1
            WHEN 'nicc_modules' THEN 2
            ELSE 3
        END
    LIMIT 1),
    'publishadmin',
    'publishadmin',
    'Aprobacion Masiva',
    'Aprobar documentos de forma masiva',
    'file-text',
    '?do=admin&page=publishadmin',
    50,
    1,
    0,
    0
WHERE NOT EXISTS (SELECT 1 FROM admin_menu_items WHERE code = 'publishadmin');

-- Paso 2: Agregar permisos para manager (acceso total)
INSERT OR REPLACE INTO admin_menu_permissions (item_id, role, can_view, can_execute)
SELECT id, 'manager', 1, 1 FROM admin_menu_items WHERE code = 'publishadmin';

-- Paso 3: Agregar permisos para admin (acceso total - es herramienta de documentos)
INSERT OR REPLACE INTO admin_menu_permissions (item_id, role, can_view, can_execute)
SELECT id, 'admin', 1, 1 FROM admin_menu_items WHERE code = 'publishadmin';

-- Paso 4: Agregar permisos para superuser
INSERT OR REPLACE INTO admin_menu_permissions (item_id, role, can_view, can_execute)
SELECT id, 'superuser', 1, 1 FROM admin_menu_items WHERE code = 'publishadmin';

-- Fin de migracion
SELECT 1;
