-- ============================================================================
-- SEED DATA: Habilidades/Especialidades Médicas
-- FASE-8 ERP-Core - ERP Clínicas
-- ============================================================================
-- NOTA: Ejecutar después de SET app.current_tenant_id = 'UUID-DEL-TENANT';
-- ============================================================================

-- Tipos de habilidad médica
INSERT INTO hr.skill_types (tenant_id, name)
SELECT current_setting('app.current_tenant_id', true)::UUID, name
FROM (VALUES
    ('Especialidad Médica'),
    ('Subespecialidad'),
    ('Certificación'),
    ('Curso/Diplomado'),
    ('Idioma')
) AS t(name)
WHERE current_setting('app.current_tenant_id', true) IS NOT NULL
  AND current_setting('app.current_tenant_id', true) != ''
ON CONFLICT DO NOTHING;

-- Niveles de habilidad (para cada tipo)
INSERT INTO hr.skill_levels (tenant_id, skill_type_id, name, level)
SELECT
    current_setting('app.current_tenant_id', true)::UUID,
    st.id,
    l.name,
    l.level
FROM hr.skill_types st
CROSS JOIN (VALUES
    ('Residente', 1),
    ('Especialista', 2),
    ('Subespecialista', 3),
    ('Fellow', 4)
) AS l(name, level)
WHERE st.tenant_id = current_setting('app.current_tenant_id', true)::UUID
  AND st.name IN ('Especialidad Médica', 'Subespecialidad')
ON CONFLICT DO NOTHING;

-- Niveles para certificaciones
INSERT INTO hr.skill_levels (tenant_id, skill_type_id, name, level)
SELECT
    current_setting('app.current_tenant_id', true)::UUID,
    st.id,
    l.name,
    l.level
FROM hr.skill_types st
CROSS JOIN (VALUES
    ('En trámite', 1),
    ('Vigente', 2),
    ('Recertificado', 3)
) AS l(name, level)
WHERE st.tenant_id = current_setting('app.current_tenant_id', true)::UUID
  AND st.name = 'Certificación'
ON CONFLICT DO NOTHING;

-- Especialidades médicas comunes
INSERT INTO hr.skills (tenant_id, skill_type_id, name, codigo_ssa, requiere_cedula)
SELECT
    current_setting('app.current_tenant_id', true)::UUID,
    id,
    unnest(ARRAY[
        'Medicina General',
        'Medicina Familiar',
        'Pediatría',
        'Ginecología y Obstetricia',
        'Medicina Interna',
        'Cardiología',
        'Dermatología',
        'Oftalmología',
        'Otorrinolaringología',
        'Traumatología y Ortopedia',
        'Neurología',
        'Psiquiatría',
        'Urología',
        'Gastroenterología',
        'Neumología'
    ]),
    NULL,
    true
FROM hr.skill_types
WHERE name = 'Especialidad Médica'
  AND tenant_id = current_setting('app.current_tenant_id', true)::UUID
ON CONFLICT DO NOTHING;

-- Certificaciones comunes
INSERT INTO hr.skills (tenant_id, skill_type_id, name, requiere_cedula)
SELECT
    current_setting('app.current_tenant_id', true)::UUID,
    id,
    unnest(ARRAY[
        'Consejo de Especialidad',
        'COFEPRIS',
        'BLS (Basic Life Support)',
        'ACLS (Advanced Cardiac Life Support)',
        'PALS (Pediatric Advanced Life Support)',
        'NOM-024-SSA3 Expediente Clínico'
    ]),
    false
FROM hr.skill_types
WHERE name = 'Certificación'
  AND tenant_id = current_setting('app.current_tenant_id', true)::UUID
ON CONFLICT DO NOTHING;
