-- 157_scenario_builder_foundation — down
--
-- Rolls back mig 157 in reverse order. Down files are reference material
-- (not auto-applied); operator recovery path is:
--
--   psql ... < 157_scenario_builder_foundation.down.sql
--   DELETE FROM paliad.applied_migrations WHERE version = 157;
--
-- This restores the legacy paliad.scenarios shape from mig 145 — the
-- builder columns and the three sibling tables are dropped wholesale.
-- Any builder data in the dropped tables is lost (the tables CASCADE to
-- their children, and DROP TABLE doesn't keep a backup).

BEGIN;

SELECT set_config(
    'paliad.audit_reason',
    'mig 157 rollback: tear down Scenario builder foundation (t-paliad-340)',
    true
);

-- 8. updated_at triggers
DROP TRIGGER IF EXISTS scenario_events_touch_updated_at_trg       ON paliad.scenario_events;
DROP TRIGGER IF EXISTS scenario_proceedings_touch_updated_at_trg  ON paliad.scenario_proceedings;

-- 7. RLS — drop new policies + restore legacy four
DROP POLICY IF EXISTS scenario_shares_mutate        ON paliad.scenario_shares;
DROP POLICY IF EXISTS scenario_shares_select        ON paliad.scenario_shares;
DROP POLICY IF EXISTS scenario_events_mutate        ON paliad.scenario_events;
DROP POLICY IF EXISTS scenario_events_select        ON paliad.scenario_events;
DROP POLICY IF EXISTS scenario_proceedings_mutate   ON paliad.scenario_proceedings;
DROP POLICY IF EXISTS scenario_proceedings_select   ON paliad.scenario_proceedings;
DROP POLICY IF EXISTS scenarios_owner_mutate        ON paliad.scenarios;
DROP POLICY IF EXISTS scenarios_select              ON paliad.scenarios;

-- Restore the four mig-145 policies verbatim.
CREATE POLICY scenarios_project_select ON paliad.scenarios
    FOR SELECT
    USING (project_id IS NOT NULL AND paliad.can_see_project(project_id));

CREATE POLICY scenarios_project_mutate ON paliad.scenarios
    FOR ALL
    USING (project_id IS NOT NULL AND paliad.can_see_project(project_id))
    WITH CHECK (project_id IS NOT NULL AND paliad.can_see_project(project_id));

CREATE POLICY scenarios_abstract_select ON paliad.scenarios
    FOR SELECT
    USING (project_id IS NULL AND created_by = auth.uid());

CREATE POLICY scenarios_abstract_mutate ON paliad.scenarios
    FOR ALL
    USING (project_id IS NULL AND created_by = auth.uid())
    WITH CHECK (project_id IS NULL AND created_by = auth.uid());

-- 6. helper function
DROP FUNCTION IF EXISTS paliad.can_see_scenario(uuid);

-- 5. paliad.projects.origin_scenario_id
DROP INDEX IF EXISTS paliad.projects_origin_scenario_idx;
ALTER TABLE paliad.projects DROP COLUMN IF EXISTS origin_scenario_id;

-- 4. paliad.scenario_shares
DROP TABLE IF EXISTS paliad.scenario_shares;

-- 3. paliad.scenario_events
DROP TABLE IF EXISTS paliad.scenario_events;

-- 2. paliad.scenario_proceedings
DROP TABLE IF EXISTS paliad.scenario_proceedings;

-- 1. paliad.scenarios — restore mig-145 shape
DROP INDEX IF EXISTS paliad.scenarios_updated_idx;
DROP INDEX IF EXISTS paliad.scenarios_owner_status_idx;

-- Restore the unique constraint mig 145 had.
ALTER TABLE paliad.scenarios
    ADD CONSTRAINT scenarios_unique_per_scope
    UNIQUE NULLS NOT DISTINCT (project_id, created_by, name);

-- spec was NOT NULL in mig 145. Restore that — but only after backfilling
-- any NULL specs the builder might have created (none in legacy paths;
-- only builder rows have NULL spec, and those are dropped together with
-- the builder schema if a real rollback is needed).
UPDATE paliad.scenarios SET spec = '{}'::jsonb WHERE spec IS NULL;
ALTER TABLE paliad.scenarios ALTER COLUMN spec SET NOT NULL;

ALTER TABLE paliad.scenarios DROP COLUMN IF EXISTS notes;
ALTER TABLE paliad.scenarios DROP COLUMN IF EXISTS stichtag;
ALTER TABLE paliad.scenarios DROP COLUMN IF EXISTS promoted_project_id;
ALTER TABLE paliad.scenarios DROP COLUMN IF EXISTS origin_project_id;
ALTER TABLE paliad.scenarios DROP COLUMN IF EXISTS status;
ALTER TABLE paliad.scenarios DROP COLUMN IF EXISTS owner_id;

COMMIT;
