-- 027_afir_tip_fond.sql
-- Augment fonduri.afir_plati with tip_fond discriminator to host both FEADR
-- (project-based development funds) and FEGA (per-hectare direct payments
-- to farmers) in the same fact table. Schema is near-identical between the
-- two; only specific columns are populated per fund (e.g. fega_op vs feadr_op).
--
-- Backwards compatible: existing 1.04M rows (2023+2024 FEADR) get tip_fond='FEADR'.

ALTER TABLE fonduri.afir_plati
  ADD COLUMN IF NOT EXISTS tip_fond text NOT NULL DEFAULT 'FEADR';

-- Backfill any rows that were inserted before column existed
UPDATE fonduri.afir_plati SET tip_fond = 'FEADR' WHERE tip_fond IS NULL;

CREATE INDEX IF NOT EXISTS idx_afir_plati_tip_fond ON fonduri.afir_plati(tip_fond, source_year);
