-- Down migration for 026: revert the partner_units rename.
--
-- Note: paliad.users.dezernat values cannot be perfectly restored. The
-- column is recreated as NULL; structural data (membership rows) is
-- preserved. Per design doc §7.3 — if a true free-text rollback is ever
-- needed, an admin script can reconstruct values from
-- partner_unit_members:
--
--   UPDATE paliad.users u SET dezernat = (
--     SELECT pu.name FROM paliad.partner_units pu
--      JOIN paliad.partner_unit_members pum ON pum.partner_unit_id = pu.id
--      WHERE pum.user_id = u.id LIMIT 1)
--    WHERE u.dezernat IS NULL;
--
-- That step is not auto-run because most rollbacks are recoveries, not
-- data restorations.

-- 1. Drop the audit table.
--
-- DATA LOSS: paliad.partner_unit_events has no pre-027 equivalent, so its
-- rows cannot be migrated forward on a re-up. Any audit history accumulated
-- since 027 was applied will be permanently lost on rollback. This is
-- accepted because audits are append-only telemetry, not authoritative
-- state — losing them does not corrupt the rest of the schema.
DROP TABLE IF EXISTS paliad.partner_unit_events;

-- 2. Rename RLS policies back.
DO $$ BEGIN ALTER POLICY partner_units_select        ON paliad.partner_units        RENAME TO departments_select;        EXCEPTION WHEN undefined_object THEN NULL; END $$;
DO $$ BEGIN ALTER POLICY partner_units_write         ON paliad.partner_units        RENAME TO departments_write;         EXCEPTION WHEN undefined_object THEN NULL; END $$;
DO $$ BEGIN ALTER POLICY partner_unit_members_select ON paliad.partner_unit_members RENAME TO department_members_select; EXCEPTION WHEN undefined_object THEN NULL; END $$;
DO $$ BEGIN ALTER POLICY partner_unit_members_write  ON paliad.partner_unit_members RENAME TO department_members_write;  EXCEPTION WHEN undefined_object THEN NULL; END $$;

-- 3. Rename indexes back.
DO $$ BEGIN ALTER INDEX paliad.partner_units_office_idx        RENAME TO departments_office_idx;        EXCEPTION WHEN undefined_object THEN NULL; END $$;
DO $$ BEGIN ALTER INDEX paliad.partner_units_lead_idx          RENAME TO departments_lead_idx;          EXCEPTION WHEN undefined_object THEN NULL; END $$;
DO $$ BEGIN ALTER INDEX paliad.partner_unit_members_user_idx   RENAME TO department_members_user_idx;   EXCEPTION WHEN undefined_object THEN NULL; END $$;

-- 4. Rename constraints back.
DO $$ BEGIN ALTER TABLE paliad.partner_units        RENAME CONSTRAINT partner_units_pkey                            TO departments_pkey;                       EXCEPTION WHEN undefined_object THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.partner_units        RENAME CONSTRAINT partner_units_lead_user_id_fkey               TO departments_lead_user_id_fkey;          EXCEPTION WHEN undefined_object THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.partner_units        RENAME CONSTRAINT partner_units_office_check                    TO departments_office_check;               EXCEPTION WHEN undefined_object THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.partner_unit_members RENAME CONSTRAINT partner_unit_members_pkey                     TO department_members_pkey;                EXCEPTION WHEN undefined_object THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.partner_unit_members RENAME CONSTRAINT partner_unit_members_partner_unit_id_fkey     TO department_members_department_id_fkey;  EXCEPTION WHEN undefined_object THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.partner_unit_members RENAME CONSTRAINT partner_unit_members_user_id_fkey             TO department_members_user_id_fkey;        EXCEPTION WHEN undefined_object THEN NULL; END $$;

-- 5. Rename junction column back.
ALTER TABLE paliad.partner_unit_members RENAME COLUMN partner_unit_id TO department_id;

-- 6. Rename tables back.
ALTER TABLE paliad.partner_unit_members RENAME TO department_members;
ALTER TABLE paliad.partner_units        RENAME TO departments;

-- 7. Recreate the legacy free-text column. Values are NULL.
ALTER TABLE paliad.users ADD COLUMN IF NOT EXISTS dezernat text;
