-- t-paliad-186 down — reverses 087_project_proceeding_type_remap.up.sql.
--
-- "Revert" here means: NULL every project that the up-migration remapped
-- AND drop the 'proceeding_type_remap_null' project_events rows it
-- wrote. We cannot perfectly recover the litigation→fristenrechner
-- remap because the up-migration moved INF→UPC_INF (etc.) without
-- preserving the original code in a side column. Resetting to NULL is
-- the safe rollback — the operator can hand-remap a project if needed.
--
-- Today this is a no-op on production data (0 live remaps).

SELECT set_config(
    'paliad.audit_reason',
    'rollback 087: NULL projects.proceeding_type_id remapped by mig 087',
    true);

DELETE FROM paliad.project_events
 WHERE event_type = 'proceeding_type_remap_null'
   AND metadata->>'migration' = '087';

UPDATE paliad.projects
   SET proceeding_type_id = NULL
 WHERE proceeding_type_id IS NOT NULL
   AND proceeding_type_id IN (
       SELECT id FROM paliad.proceeding_types
        WHERE category = 'fristenrechner'
          AND code IN ('UPC_INF', 'UPC_REV', 'UPC_APP')
   );
