-- Parche: Registro del módulo y páginas de Reportes y asignación de permisos a Admin y Gerente
-- Ejecutar con la base de datos correspondiente (PostgreSQL/SQLite)

-- 1. Crear el Módulo de Reportes
INSERT INTO auth.module (id, name_es, name_en, icon, order_index, is_active)
VALUES ('7e0f8a32-9fc1-4eb9-ea98-05f37f5d6e50', 'Reportes', 'Reports', 'assessment', 50, TRUE)
ON CONFLICT (id) DO UPDATE
SET name_es = EXCLUDED.name_es,
    name_en = EXCLUDED.name_en,
    icon = EXCLUDED.icon,
    order_index = EXCLUDED.order_index,
    is_active = EXCLUDED.is_active,
    updated_at = CURRENT_TIMESTAMP;

-- 2. Crear las Páginas del Módulo de Reportes
INSERT INTO auth.page (id, module_id, name_es, name_en, path, icon, order_index, is_active)
VALUES
  ('b3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7a', '7e0f8a32-9fc1-4eb9-ea98-05f37f5d6e50', 'Balance General', 'Balance Sheet', '/reports/balance-sheet', 'account_balance_wallet', 10, TRUE),
  ('c3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7b', '7e0f8a32-9fc1-4eb9-ea98-05f37f5d6e50', 'Pérdidas y Ganancias', 'Profit & Loss', '/reports/profit-and-loss', 'show_chart', 20, TRUE),
  ('d3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7c', '7e0f8a32-9fc1-4eb9-ea98-05f37f5d6e50', 'Balanza de Comprobación', 'Trial Balance', '/reports/trial-balance', 'receipt', 30, TRUE),
  ('e3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7d', '7e0f8a32-9fc1-4eb9-ea98-05f37f5d6e50', 'Antigüedad de Cartera', 'Aging Reports', '/reports/aging', 'query_builder', 40, TRUE),
  ('f3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7e', '7e0f8a32-9fc1-4eb9-ea98-05f37f5d6e50', 'Libros Fiscales', 'Tax Books', '/reports/tax-books', 'menu_book', 50, 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;

-- 3. Asignar Permisos al Administrador (Acceso Total: can_view, can_create, can_edit, can_delete)
INSERT INTO auth.profile (role_id, page_id, can_view, can_create, can_edit, can_delete)
VALUES
  ('8a9b3a32-dfc7-4eb5-aa94-c1f93f1d2e1c', 'b3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7a', TRUE, TRUE, TRUE, TRUE),
  ('8a9b3a32-dfc7-4eb5-aa94-c1f93f1d2e1c', 'c3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7b', TRUE, TRUE, TRUE, TRUE),
  ('8a9b3a32-dfc7-4eb5-aa94-c1f93f1d2e1c', 'd3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7c', TRUE, TRUE, TRUE, TRUE),
  ('8a9b3a32-dfc7-4eb5-aa94-c1f93f1d2e1c', 'e3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7d', TRUE, TRUE, TRUE, TRUE),
  ('8a9b3a32-dfc7-4eb5-aa94-c1f93f1d2e1c', 'f3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7e', 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;

-- 4. Asignar Permisos al Gerente (Solo Lectura/Generación: can_view = TRUE, otros = FALSE para reportes)
INSERT INTO auth.profile (role_id, page_id, can_view, can_create, can_edit, can_delete)
VALUES
  ('1b7c4a32-cfc8-4eb6-ba95-d2f04f2d3e2d', 'b3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7a', TRUE, FALSE, FALSE, FALSE),
  ('1b7c4a32-cfc8-4eb6-ba95-d2f04f2d3e2d', 'c3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7b', TRUE, FALSE, FALSE, FALSE),
  ('1b7c4a32-cfc8-4eb6-ba95-d2f04f2d3e2d', 'd3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7c', TRUE, FALSE, FALSE, FALSE),
  ('1b7c4a32-cfc8-4eb6-ba95-d2f04f2d3e2d', 'e3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7d', TRUE, FALSE, FALSE, FALSE),
  ('1b7c4a32-cfc8-4eb6-ba95-d2f04f2d3e2d', 'f3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7e', TRUE, FALSE, FALSE, FALSE)
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;
