-- ============================================================
-- Seed: módulo Integraciones (Hostinger + Cloudflare + VCard API)
-- Ejecutar en la base de datos PostgreSQL de producción:
--   psql -h HOST -U usite -d usite -f seed_integraciones.sql
-- ============================================================

BEGIN;

-- 1. Insertar módulo "Integraciones" si no existe
INSERT INTO modules (title, description, modified_at, created_at, updated_at)
SELECT 'Integraciones', 'Conexión con servicios externos: Hostinger, Cloudflare y más', NOW(), NOW(), NOW()
WHERE NOT EXISTS (
    SELECT 1 FROM modules WHERE title = 'Integraciones' AND deleted_at IS NULL
);

-- 2. Insertar submódulo Hostinger si no existe
INSERT INTO submodules (title, description, url, module_id, modified_at, created_at, updated_at)
SELECT 'Hostinger',
       'Panel de VPS, dominios, hosting y DNS de Hostinger',
       '/app/hostinger',
       (SELECT id FROM modules WHERE title = 'Integraciones' AND deleted_at IS NULL LIMIT 1),
       NOW(), NOW(), NOW()
WHERE NOT EXISTS (
    SELECT 1 FROM submodules WHERE url = '/app/hostinger' AND deleted_at IS NULL
);

-- 3. Insertar submódulo Cloudflare si no existe
INSERT INTO submodules (title, description, url, module_id, modified_at, created_at, updated_at)
SELECT 'Cloudflare',
       'Gestión de zonas, DNS, SSL y firewall en Cloudflare',
       '/app/cloudflare',
       (SELECT id FROM modules WHERE title = 'Integraciones' AND deleted_at IS NULL LIMIT 1),
       NOW(), NOW(), NOW()
WHERE NOT EXISTS (
    SELECT 1 FROM submodules WHERE url = '/app/cloudflare' AND deleted_at IS NULL
);

-- 4. Insertar submódulo VCard API si no existe
INSERT INTO submodules (title, description, url, module_id, modified_at, created_at, updated_at)
SELECT 'VCard API',
       'Integración con el sistema VCard externo (Laravel + Sanctum): usuarios, membresías, vcards, pagos y más',
       '/app/vcard-api',
       (SELECT id FROM modules WHERE title = 'Integraciones' AND deleted_at IS NULL LIMIT 1),
       NOW(), NOW(), NOW()
WHERE NOT EXISTS (
    SELECT 1 FROM submodules WHERE url = '/app/vcard-api' AND deleted_at IS NULL
);

-- 5. Insertar submódulo Alibaba Cloud OSS si no existe
INSERT INTO submodules (title, description, url, module_id, modified_at, created_at, updated_at)
SELECT 'Alibaba Cloud OSS',
       'Gestión de configuraciones de acceso a Alibaba Cloud Object Storage Service (OSS): credenciales, buckets y regiones',
       '/app/oss-api',
       (SELECT id FROM modules WHERE title = 'Integraciones' AND deleted_at IS NULL LIMIT 1),
       NOW(), NOW(), NOW()
WHERE NOT EXISTS (
    SELECT 1 FROM submodules WHERE url = '/app/oss-api' AND deleted_at IS NULL
);

-- 6. Asignar todos los submódulos de Integraciones a TODOS los roles (ignorar duplicados)
INSERT INTO roles_submodules (role_id, submodule_id)
SELECT r.id, s.id
FROM roles r
CROSS JOIN submodules s
WHERE s.url IN ('/app/hostinger', '/app/cloudflare', '/app/vcard-api', '/app/oss-api')
  AND r.deleted_at IS NULL
  AND s.deleted_at IS NULL
ON CONFLICT DO NOTHING;

COMMIT;

-- Verificación rápida
SELECT m.title AS modulo, s.title AS submodulo, s.url
FROM modules m
JOIN submodules s ON s.module_id = m.id
WHERE m.title = 'Integraciones'
  AND m.deleted_at IS NULL
  AND s.deleted_at IS NULL;
