-- ============================================================================
-- SEED DATA: Catálogos de Clínica
-- FASE-8 ERP-Core - ERP Clínicas
-- ============================================================================
-- NOTA: Ejecutar después de SET app.current_tenant_id = 'UUID-DEL-TENANT';
-- ============================================================================

-- Categorías de almacén para clínicas
INSERT INTO inventory.storage_categories (tenant_id, name, max_weight, allow_new_product,
    requiere_refrigeracion, temperatura_min, temperatura_max, es_controlado, requiere_receta)
SELECT current_setting('app.current_tenant_id', true)::UUID, name, max_weight, allow_new_product,
    requiere_refrigeracion, temperatura_min, temperatura_max, es_controlado, requiere_receta
FROM (VALUES
    ('Farmacia General', 5000.0, 'mixed', false, NULL, NULL, false, false),
    ('Refrigerados', 500.0, 'same', true, 2.0, 8.0, false, true),
    ('Medicamentos Controlados', 100.0, 'same', false, NULL, NULL, true, true),
    ('Material Quirúrgico', 1000.0, 'mixed', false, NULL, NULL, false, false),
    ('Insumos Laboratorio', 500.0, 'mixed', false, NULL, NULL, false, false),
    ('Vacunas', 200.0, 'same', true, 2.0, 8.0, false, true)
) AS t(name, max_weight, allow_new_product, requiere_refrigeracion, temperatura_min, temperatura_max, es_controlado, requiere_receta)
WHERE current_setting('app.current_tenant_id', true) IS NOT NULL
  AND current_setting('app.current_tenant_id', true) != ''
ON CONFLICT DO NOTHING;

-- Tipos de paquete para medicamentos
INSERT INTO inventory.package_types (tenant_id, name, height, width, length, base_weight, max_weight, sequence)
SELECT current_setting('app.current_tenant_id', true)::UUID, name, height, width, length, base_weight, max_weight, seq
FROM (VALUES
    ('Caja Medicamentos', 20.0, 15.0, 10.0, 0.1, 2.0, 10),
    ('Blister', 15.0, 10.0, 1.0, 0.01, 0.1, 20),
    ('Frasco', 10.0, 5.0, 5.0, 0.05, 0.5, 30),
    ('Ampolleta', 8.0, 2.0, 2.0, 0.01, 0.05, 40),
    ('Bolsa Suero', 30.0, 20.0, 5.0, 0.1, 1.0, 50),
    ('Jeringa', 15.0, 3.0, 3.0, 0.02, 0.1, 60)
) AS t(name, height, width, length, base_weight, max_weight, seq)
WHERE current_setting('app.current_tenant_id', true) IS NOT NULL
  AND current_setting('app.current_tenant_id', true) != ''
ON CONFLICT DO NOTHING;

-- Métodos de pago para clínicas
INSERT INTO financial.payment_methods (tenant_id, name, code, payment_type, aplica_seguro, requiere_factura, porcentaje_seguro)
SELECT current_setting('app.current_tenant_id', true)::UUID, name, code, payment_type::financial.payment_method_type, aplica_seguro, requiere_factura, porcentaje_seguro
FROM (VALUES
    ('Efectivo', 'efectivo', 'inbound', false, false, 0),
    ('Tarjeta Débito', 'td', 'inbound', false, false, 0),
    ('Tarjeta Crédito', 'tc', 'inbound', false, true, 0),
    ('Transferencia', 'transfer', 'inbound', false, true, 0),
    ('Seguro GMM', 'seguro_gmm', 'inbound', true, true, 80),
    ('Seguro GMA', 'seguro_gma', 'inbound', true, true, 70),
    ('Convenio Empresa', 'convenio', 'inbound', false, true, 0)
) AS t(name, code, payment_type, aplica_seguro, requiere_factura, porcentaje_seguro)
WHERE current_setting('app.current_tenant_id', true) IS NOT NULL
  AND current_setting('app.current_tenant_id', true) != ''
ON CONFLICT (tenant_id, code) DO NOTHING;

-- Estructuras de nómina para clínicas
INSERT INTO hr.payslip_structures (tenant_id, name, code, tipo_pago)
SELECT current_setting('app.current_tenant_id', true)::UUID, name, code, tipo_pago
FROM (VALUES
    ('Nómina Quincenal', 'NOM-QUIN', 'quincenal'),
    ('Nómina Semanal', 'NOM-SEM', 'semanal'),
    ('Honorarios Médicos', 'HON-MED', 'honorarios'),
    ('Pago por Guardia', 'PAG-GUAR', 'guardia'),
    ('Comisión por Consulta', 'COM-CONS', 'comision')
) AS t(name, code, tipo_pago)
WHERE current_setting('app.current_tenant_id', true) IS NOT NULL
  AND current_setting('app.current_tenant_id', true) != ''
ON CONFLICT (tenant_id, code) DO NOTHING;

-- Tipos de consultorio
INSERT INTO hr.work_locations (tenant_id, name, tipo_consultorio, capacidad)
SELECT current_setting('app.current_tenant_id', true)::UUID, name, tipo, capacidad
FROM (VALUES
    ('Consultorio 1', 'general', 1),
    ('Consultorio 2', 'general', 1),
    ('Consultorio Especialidad', 'especialidad', 1),
    ('Área de Urgencias', 'urgencias', 3),
    ('Laboratorio', 'laboratorio', 5),
    ('Farmacia', 'farmacia', 2)
) AS t(name, tipo, capacidad)
WHERE current_setting('app.current_tenant_id', true) IS NOT NULL
  AND current_setting('app.current_tenant_id', true) != ''
ON CONFLICT DO NOTHING;
