-- Script Final Corrigido - Usa IDs reais do banco
-- Insere dados faltantes em todas as tabelas

DO $$
DECLARE
    v_min_001 UUID := '6e32517f-5dbc-416a-9632-d920175b8cdd'; -- ID real do MIN-001
    v_min_002 UUID;
    v_min_003 UUID;
    v_ou_001 UUID;
    v_ou_002 UUID;
    v_ou_003 UUID;
    v_ou_004 UUID;
    v_pos_001 UUID;
    v_pos_002 UUID;
    v_pos_003 UUID;
    v_pos_004 UUID;
BEGIN
    -- Inserir ministerios
    INSERT INTO ministry (id, code, name, acronym, is_active, created_at, updated_at) VALUES
    ('22222222-2222-2222-2222-222222222222', 'MIN-002', 'Ministerio da Saude Publica', 'MINSAUDE', true, now(), now())
    ON CONFLICT (code) DO UPDATE SET name = EXCLUDED.name
    RETURNING id INTO v_min_002;
    
    IF v_min_002 IS NULL THEN SELECT id INTO v_min_002 FROM ministry WHERE code = 'MIN-002'; END IF;
    
    INSERT INTO ministry (id, code, name, acronym, is_active, created_at, updated_at) VALUES
    ('33333333-3333-3333-3333-333333333333', 'MIN-003', 'Ministerio da Educacao Nacional', 'MINEDU', true, now(), now())
    ON CONFLICT (code) DO UPDATE SET name = EXCLUDED.name
    RETURNING id INTO v_min_003;
    
    IF v_min_003 IS NULL THEN SELECT id INTO v_min_003 FROM ministry WHERE code = 'MIN-003'; END IF;
    
    RAISE NOTICE 'Ministerios: MIN-001=%, MIN-002=%, MIN-003=%', v_min_001, v_min_002, v_min_003;
    
    -- Inserir unidades organicas
    INSERT INTO org_unit (id, code, name, ministry_id, unit_type, is_active, created_at, updated_at) VALUES
    ('44444444-4444-4444-4444-444444444444', 'UO-001', 'Direcao-Geral do Tesouro', v_min_001, 'DIRECTORATE', true, now(), now())
    ON CONFLICT (code) DO UPDATE SET name = EXCLUDED.name
    RETURNING id INTO v_ou_001;
    
    IF v_ou_001 IS NULL THEN SELECT id INTO v_ou_001 FROM org_unit WHERE code = 'UO-001'; END IF;
    
    INSERT INTO org_unit (id, code, name, ministry_id, unit_type, is_active, created_at, updated_at) VALUES
    ('55555555-5555-5555-5555-555555555555', 'UO-002', 'Direcao-Geral do Orcamento', v_min_001, 'DIRECTORATE', true, now(), now())
    ON CONFLICT (code) DO UPDATE SET name = EXCLUDED.name
    RETURNING id INTO v_ou_002;
    
    IF v_ou_002 IS NULL THEN SELECT id INTO v_ou_002 FROM org_unit WHERE code = 'UO-002'; END IF;
    
    INSERT INTO org_unit (id, code, name, ministry_id, unit_type, is_active, created_at, updated_at) VALUES
    ('66666666-6666-6666-6666-666666666666', 'UO-003', 'Hospital Nacional Simao Mendes', v_min_002, 'HOSPITAL', true, now(), now())
    ON CONFLICT (code) DO UPDATE SET name = EXCLUDED.name
    RETURNING id INTO v_ou_003;
    
    IF v_ou_003 IS NULL THEN SELECT id INTO v_ou_003 FROM org_unit WHERE code = 'UO-003'; END IF;
    
    INSERT INTO org_unit (id, code, name, ministry_id, unit_type, is_active, created_at, updated_at) VALUES
    ('77777777-7777-7777-7777-777777777777', 'UO-004', 'Escola Secundaria de Bissau', v_min_003, 'SCHOOL', true, now(), now())
    ON CONFLICT (code) DO UPDATE SET name = EXCLUDED.name
    RETURNING id INTO v_ou_004;
    
    IF v_ou_004 IS NULL THEN SELECT id INTO v_ou_004 FROM org_unit WHERE code = 'UO-004'; END IF;
    
    RAISE NOTICE 'Unidades Organicas criadas: %, %, %, %', v_ou_001, v_ou_002, v_ou_003, v_ou_004;
    
    -- Inserir cargos
    INSERT INTO position (id, code, title, org_unit_id, is_active, created_at, updated_at) VALUES
    ('88888888-8888-8888-8888-888888888888', 'POS-001', 'Diretor-Geral', v_ou_001, true, now(), now())
    ON CONFLICT (code) DO UPDATE SET title = EXCLUDED.title;
    
    INSERT INTO position (id, code, title, org_unit_id, is_active, created_at, updated_at) VALUES
    ('99999999-9999-9999-9999-999999999999', 'POS-002', 'Tecnico Superior', v_ou_002, true, now(), now())
    ON CONFLICT (code) DO UPDATE SET title = EXCLUDED.title;
    
    INSERT INTO position (id, code, title, org_unit_id, is_active, created_at, updated_at) VALUES
    ('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'POS-003', 'Medico Especialista', v_ou_003, true, now(), now())
    ON CONFLICT (code) DO UPDATE SET title = EXCLUDED.title;
    
    INSERT INTO position (id, code, title, org_unit_id, is_active, created_at, updated_at) VALUES
    ('bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'POS-004', 'Professor', v_ou_004, true, now(), now())
    ON CONFLICT (code) DO UPDATE SET title = EXCLUDED.title;
    
    RAISE NOTICE 'Cargos criados com sucesso';
    RAISE NOTICE '=== DADOS INSERIDOS COM SUCESSO ===';
END $$;

-- Verificar
SELECT 'MINISTERIOS' as tabela, COUNT(*)::text FROM ministry
UNION ALL SELECT 'UNIDADES ORGANICAS', COUNT(*)::text FROM org_unit
UNION ALL SELECT 'CARGOS', COUNT(*)::text FROM position
UNION ALL SELECT 'AGENTES', COUNT(*)::text FROM agents
UNION ALL SELECT 'BANCOS', COUNT(*)::text FROM bank;
