-- Add profissional_id column to financial_transactions
ALTER TABLE financial_transactions ADD COLUMN IF NOT EXISTS profissional_id UUID REFERENCES cadastro_profissionais(id);

-- Optional: Try to link existing transactions by CPF (removes non-digits for comparison)
UPDATE financial_transactions ft
SET profissional_id = cp.id
FROM cadastro_profissionais cp
WHERE ft.profissional_id IS NULL
  AND REGEXP_REPLACE(ft.cpf, '\D', '', 'g') = REGEXP_REPLACE(cp.cpf_cnpj_titular, '\D', '', 'g')
  AND ft.cpf IS NOT NULL 
  AND ft.cpf != '';
