-- Seed para Fase 3: Contactos e Impuestos
-- Crea módulos de Ventas y Compras, registra nuevas páginas del menú y otorga permisos al Administrador.

-- 1. Crear Módulos del Menú (Ventas y Compras)
INSERT INTO auth.module (id, name_es, name_en, icon, order_index, is_active)
VALUES
  ('5f3b7b4d-ef81-4eb2-8c9f-3f626c7e6c4e', 'Ventas', 'Sales', 'trending_up', 30, TRUE),
  ('6f4c8b5e-df92-4eb3-9d0f-4f737d8e7d5f', 'Compras', 'Purchases', 'shopping_cart', 40, 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 Páginas de Menú
INSERT INTO auth.page (id, module_id, name_es, name_en, path, icon, order_index, is_active)
VALUES
  -- Clientes (Ventas)
  ('a4b5c6d7-e8f9-4a0b-1c2d-3e4f5a6b7c8d', '5f3b7b4d-ef81-4eb2-8c9f-3f626c7e6c4e', 'Clientes', 'Customers', '/sales/customers', 'people_alt', 1, TRUE),
  -- Proveedores (Compras)
  ('b5c6d7e8-f9a0-4b1c-2d3e-4f5a6b7c8d9e', '6f4c8b5e-df92-4eb3-9d0f-4f737d8e7d5f', 'Proveedores', 'Vendors', '/purchases/vendors', 'local_shipping', 1, TRUE),
  -- Impuestos (Contabilidad)
  ('c6d7e8f9-a0b1-4c2d-3e4f-5a6b7c8d9e0f', '4e0f7a32-9fc1-4eb9-ea98-05f37f5d6e50', 'Impuestos', 'Taxes', '/accounting/taxes', 'percent', 3, 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. Configurar Permisos del Administrador (Acceso Total a las nuevas páginas)
INSERT INTO auth.profile (role_id, page_id, can_view, can_create, can_edit, can_delete)
VALUES
  ('8a9b3a32-dfc7-4eb5-aa94-c1f93f1d2e1c', 'a4b5c6d7-e8f9-4a0b-1c2d-3e4f5a6b7c8d', TRUE, TRUE, TRUE, TRUE),
  ('8a9b3a32-dfc7-4eb5-aa94-c1f93f1d2e1c', 'b5c6d7e8-f9a0-4b1c-2d3e-4f5a6b7c8d9e', TRUE, TRUE, TRUE, TRUE),
  ('8a9b3a32-dfc7-4eb5-aa94-c1f93f1d2e1c', 'c6d7e8f9-a0b1-4c2d-3e4f-5a6b7c8d9e0f', 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. Datos semilla opcionales para la empresa demo (Clientes, Proveedores e Impuestos base)
-- ID Empresa Demo: d1e2f3a4-b5c6-4d7e-8f9a-0b1c2d3e4f5a
INSERT INTO contacts (id, company_id, type, name, tax_id, email, phone, address, is_active)
VALUES
  ('f1e2d3c4-b5a6-4f7e-8d9c-0b1a2c3d4e5f', 'd1e2f3a4-b5c6-4d7e-8f9a-0b1c2d3e4f5a', 'customer', 'Cliente Ejemplo S.A.', '123456789-0', 'cliente@ejemplo.com', '555-0199', 'Av. Central 456', TRUE),
  ('e2d3c4b5-a6f7-4d8e-9c0b-1a2c3d4e5f6a', 'd1e2f3a4-b5c6-4d7e-8f9a-0b1c2d3e4f5a', 'vendor', 'Proveedor Ejemplo Ltda.', '987654321-9', 'ventas@proveedor.com', '555-0100', 'Zona Industrial Lote 12', TRUE)
ON CONFLICT (company_id, tax_id) DO NOTHING;

INSERT INTO tax_rates (id, company_id, name, rate, type, is_active)
VALUES
  ('a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5e', 'd1e2f3a4-b5c6-4d7e-8f9a-0b1c2d3e4f5a', 'IVA 19%', 19.00, 'vat', TRUE),
  ('b2c3d4e5-f6a7-4b8c-9d0e-1f2a3b4c5d6f', 'd1e2f3a4-b5c6-4d7e-8f9a-0b1c2d3e4f5a', 'Retención Fuente 2.5%', 2.50, 'withholding', TRUE),
  ('c3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7a', 'd1e2f3a4-b5c6-4d7e-8f9a-0b1c2d3e4f5a', 'Exento de IVA', 0.00, 'exempt', TRUE)
ON CONFLICT (company_id, name) DO NOTHING;
