-- Parche: Registro de la página "Unidades Ejecutoras" en el menú del Módulo de Presupuesto
-- Ejecutar en la base de datos de producción (PostgreSQL)

BEGIN;

-- 1. Insertar la nueva página en auth.page
-- Se asocia al módulo de Presupuesto (ID: '5b8f8a32-9fc1-4eb9-ea98-05f37f5d6e70')
INSERT INTO auth.page (id, module_id, name_es, name_en, path, icon, order_index, is_active)
VALUES (
  'c2d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e9f', 
  '5b8f8a32-9fc1-4eb9-ea98-05f37f5d6e70', 
  'Unidades Ejecutoras', 
  'Organizational Units', 
  '/budget/org-units', 
  'corporate_fare', 
  15, 
  TRUE
)
ON CONFLICT (id) DO UPDATE
SET module_id = EXCLUDED.module_id,
    name_es = EXCLUDED.name_es,
    name_en = EXCLUDED.name_en,
    path = EXCLUDED.path,
    icon = EXCLUDED.icon,
    order_index = EXCLUDED.order_index,
    is_active = EXCLUDED.is_active,
    updated_at = CURRENT_TIMESTAMP;

-- 2. Asignar permisos completos al Rol Administrador (ID: '8a9b3a32-dfc7-4eb5-aa94-c1f93f1d2e1c')
INSERT INTO auth.profile (role_id, page_id, can_view, can_create, can_edit, can_delete)
VALUES ('8a9b3a32-dfc7-4eb5-aa94-c1f93f1d2e1c', 'c2d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e9f', TRUE, TRUE, TRUE, TRUE)
ON CONFLICT (role_id, page_id) DO UPDATE
SET can_view = EXCLUDED.can_view,
    can_create = EXCLUDED.can_create,
    can_edit = EXCLUDED.can_edit,
    can_delete = EXCLUDED.can_delete;

-- 3. Asignar permisos completos al Rol Gerente (ID: '1b7c4a32-cfc8-4eb6-ba95-d2f04f2d3e2d')
INSERT INTO auth.profile (role_id, page_id, can_view, can_create, can_edit, can_delete)
VALUES ('1b7c4a32-cfc8-4eb6-ba95-d2f04f2d3e2d', 'c2d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e9f', TRUE, TRUE, TRUE, TRUE)
ON CONFLICT (role_id, page_id) DO UPDATE
SET can_view = EXCLUDED.can_view,
    can_create = EXCLUDED.can_create,
    can_edit = EXCLUDED.can_edit,
    can_delete = EXCLUDED.can_delete;

COMMIT;
