-- Migration to support multiple functions per professional

-- 1. Create Junction Table
CREATE TABLE IF NOT EXISTS profissionais_funcoes_junction (
    profissional_id UUID NOT NULL REFERENCES cadastro_profissionais(id) ON DELETE CASCADE,
    funcao_id UUID NOT NULL REFERENCES funcoes_profissionais(id) ON DELETE CASCADE,
    PRIMARY KEY (profissional_id, funcao_id)
);

-- 2. Migrate existing data (assuming column exists)
INSERT INTO profissionais_funcoes_junction (profissional_id, funcao_id)
SELECT id, funcao_profissional_id 
FROM cadastro_profissionais 
WHERE funcao_profissional_id IS NOT NULL
ON CONFLICT DO NOTHING;

-- 3. (Optional) Drop the old column later. keeping it for backward compat for a moment, or handle it in Go.
-- ALTER TABLE cadastro_profissionais DROP COLUMN funcao_profissional_id;
