-- Add periodicidad (period type) and monto_pago (payment amount) to declaraciones.
-- periodicidad replaces the assumption that all declarations are monthly.
-- monto_pago = 0 means the declaration results in $0 to pay (auto-mark as paid).

ALTER TABLE declaraciones_provisionales
  ADD COLUMN IF NOT EXISTS periodicidad VARCHAR(15) NOT NULL DEFAULT 'mensual'
    CHECK (periodicidad IN ('mensual', 'bimestral', 'trimestral', 'semestral', 'anual'));

ALTER TABLE declaraciones_provisionales
  ADD COLUMN IF NOT EXISTS monto_pago NUMERIC(15,2);

-- For existing rows that already have a payment proof, backfill pagado_at if null
UPDATE declaraciones_provisionales
SET pagado_at = updated_at
WHERE pdf_pago IS NOT NULL AND pagado_at IS NULL;
