-- Rename German names to English (t-paliad-025 follow-up).
--
-- Idempotent: every rename is wrapped in a DO block that swallows
-- "undefined_table" / "undefined_column" / "undefined_function" so the
-- migration succeeds whether the database is:
--   * production (manually patched to English already, so most renames
--     are no-ops; only checklisten_feedback / gerichte_feedback /
--     reminder_log.frist_id are still German)
--   * a fresh dev DB (where rewritten migration 018 created/renamed
--     everything with English names except the feedback tables that
--     migration 011 still creates with German names).
--
-- Without this idempotency, a fresh deploy would fail at server startup.

-- ---------------------------------------------------------------------------
-- Tables
-- ---------------------------------------------------------------------------
DO $$ BEGIN ALTER TABLE paliad.projekte            RENAME TO projects;            EXCEPTION WHEN undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.projekt_teams       RENAME TO project_teams;       EXCEPTION WHEN undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.projekt_events      RENAME TO project_events;      EXCEPTION WHEN undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.fristen             RENAME TO deadlines;           EXCEPTION WHEN undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.termine             RENAME TO appointments;        EXCEPTION WHEN undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.parteien            RENAME TO parties;             EXCEPTION WHEN undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.notizen             RENAME TO notes;               EXCEPTION WHEN undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.dezernate           RENAME TO departments;         EXCEPTION WHEN undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.dezernat_mitglieder RENAME TO department_members;  EXCEPTION WHEN undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.dokumente           RENAME TO documents;           EXCEPTION WHEN undefined_table THEN NULL; END $$;

-- ---------------------------------------------------------------------------
-- RLS / visibility functions
-- ---------------------------------------------------------------------------
DO $$ BEGIN ALTER FUNCTION paliad.can_see_projekt(uuid) RENAME TO can_see_project; EXCEPTION WHEN undefined_function THEN NULL; END $$;
DO $$ BEGIN ALTER FUNCTION paliad.notiz_is_visible(uuid, uuid, uuid, uuid) RENAME TO note_is_visible; EXCEPTION WHEN undefined_function THEN NULL; END $$;

-- ---------------------------------------------------------------------------
-- Columns: projekt_id → project_id
-- ---------------------------------------------------------------------------
DO $$ BEGIN ALTER TABLE paliad.deadlines           RENAME COLUMN projekt_id TO project_id; EXCEPTION WHEN undefined_column OR undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.appointments        RENAME COLUMN projekt_id TO project_id; EXCEPTION WHEN undefined_column OR undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.parties             RENAME COLUMN projekt_id TO project_id; EXCEPTION WHEN undefined_column OR undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.notes               RENAME COLUMN projekt_id TO project_id; EXCEPTION WHEN undefined_column OR undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.documents           RENAME COLUMN projekt_id TO project_id; EXCEPTION WHEN undefined_column OR undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.project_events      RENAME COLUMN projekt_id TO project_id; EXCEPTION WHEN undefined_column OR undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.checklist_instances RENAME COLUMN projekt_id TO project_id; EXCEPTION WHEN undefined_column OR undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.project_teams       RENAME COLUMN projekt_id TO project_id; EXCEPTION WHEN undefined_column OR undefined_table THEN NULL; END $$;

-- ---------------------------------------------------------------------------
-- Columns: remaining German names
-- ---------------------------------------------------------------------------
DO $$ BEGIN ALTER TABLE paliad.reminder_log RENAME COLUMN frist_id        TO deadline_id;       EXCEPTION WHEN undefined_column OR undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.appointments RENAME COLUMN termin_type     TO appointment_type;  EXCEPTION WHEN undefined_column OR undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.notes        RENAME COLUMN frist_id        TO deadline_id;       EXCEPTION WHEN undefined_column OR undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.notes        RENAME COLUMN termin_id       TO appointment_id;    EXCEPTION WHEN undefined_column OR undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.notes        RENAME COLUMN akten_event_id  TO project_event_id;  EXCEPTION WHEN undefined_column OR undefined_table THEN NULL; END $$;

-- ---------------------------------------------------------------------------
-- Feedback tables (still German on both production and fresh DBs after 011)
-- ---------------------------------------------------------------------------
DO $$ BEGIN ALTER TABLE paliad.checklisten_feedback RENAME TO checklist_feedback; EXCEPTION WHEN undefined_table THEN NULL; END $$;
DO $$ BEGIN ALTER TABLE paliad.gerichte_feedback    RENAME TO court_feedback;     EXCEPTION WHEN undefined_table THEN NULL; END $$;
