-- Falukant Schema-Drift Hotfix (idempotent)
-- Ziel: Kompatibilitaet zwischen aktuellem App-Schema und Legacy/Daemon-Queries.
--
-- Behebt:
-- 1) falukant_data.character.highest_church_hierarchy_ever
-- 2) falukant_predefine.firstname.label / lastname.label
-- 3) falukant_predefine.political_office_benefit.political_office_type_id
-- 4) falukant_type.title.tr (Alias zu label_tr)
--
-- Ausfuehrung:
--   psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f backend/sql/fix_schema_drift_falukant_compat.sql

BEGIN;

-- 1) character.highest_church_hierarchy_ever
ALTER TABLE IF EXISTS falukant_data."character"
  ADD COLUMN IF NOT EXISTS highest_church_hierarchy_ever INTEGER NOT NULL DEFAULT 0;

-- 2) firstname/lastname: label <-> name kompatibel halten
ALTER TABLE IF EXISTS falukant_predefine.firstname
  ADD COLUMN IF NOT EXISTS label TEXT;

ALTER TABLE IF EXISTS falukant_predefine.lastname
  ADD COLUMN IF NOT EXISTS label TEXT;

UPDATE falukant_predefine.firstname
SET label = COALESCE(label, name),
    name  = COALESCE(name, label)
WHERE label IS NULL OR name IS NULL;

UPDATE falukant_predefine.lastname
SET label = COALESCE(label, name),
    name  = COALESCE(name, label)
WHERE label IS NULL OR name IS NULL;

-- 3) political_office_benefit: political_office_type_id <-> office_type_id
ALTER TABLE IF EXISTS falukant_predefine.political_office_benefit
  ADD COLUMN IF NOT EXISTS political_office_type_id INTEGER;

UPDATE falukant_predefine.political_office_benefit
SET political_office_type_id = COALESCE(political_office_type_id, office_type_id),
    office_type_id = COALESCE(office_type_id, political_office_type_id)
WHERE political_office_type_id IS NULL OR office_type_id IS NULL;

-- FKs fuer beide Spalten absichern (nur falls fehlend)
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1
    FROM pg_constraint
    WHERE conname = 'political_office_benefit_office_type_id_fkey'
      AND connamespace = 'falukant_predefine'::regnamespace
  ) THEN
    ALTER TABLE falukant_predefine.political_office_benefit
      ADD CONSTRAINT political_office_benefit_office_type_id_fkey
      FOREIGN KEY (office_type_id)
      REFERENCES falukant_type.political_office_type(id)
      ON DELETE CASCADE;
  END IF;

  IF NOT EXISTS (
    SELECT 1
    FROM pg_constraint
    WHERE conname = 'political_office_benefit_political_office_type_id_fkey'
      AND connamespace = 'falukant_predefine'::regnamespace
  ) THEN
    ALTER TABLE falukant_predefine.political_office_benefit
      ADD CONSTRAINT political_office_benefit_political_office_type_id_fkey
      FOREIGN KEY (political_office_type_id)
      REFERENCES falukant_type.political_office_type(id)
      ON DELETE CASCADE;
  END IF;
END $$;

CREATE OR REPLACE FUNCTION falukant_predefine.sync_political_office_benefit_type_ids()
RETURNS TRIGGER AS $$
BEGIN
  IF NEW.office_type_id IS NULL THEN
    NEW.office_type_id := NEW.political_office_type_id;
  END IF;
  IF NEW.political_office_type_id IS NULL THEN
    NEW.political_office_type_id := NEW.office_type_id;
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS trg_sync_political_office_benefit_type_ids
  ON falukant_predefine.political_office_benefit;

CREATE TRIGGER trg_sync_political_office_benefit_type_ids
BEFORE INSERT OR UPDATE ON falukant_predefine.political_office_benefit
FOR EACH ROW
EXECUTE FUNCTION falukant_predefine.sync_political_office_benefit_type_ids();

-- 4) title.tr <-> label_tr
ALTER TABLE IF EXISTS falukant_type.title
  ADD COLUMN IF NOT EXISTS tr TEXT;

UPDATE falukant_type.title
SET tr = COALESCE(tr, label_tr),
    label_tr = COALESCE(label_tr, tr)
WHERE tr IS NULL OR label_tr IS NULL;

CREATE OR REPLACE FUNCTION falukant_type.sync_title_tr_label()
RETURNS TRIGGER AS $$
BEGIN
  IF NEW.tr IS NULL THEN
    NEW.tr := NEW.label_tr;
  END IF;
  IF NEW.label_tr IS NULL THEN
    NEW.label_tr := NEW.tr;
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS trg_sync_title_tr_label
  ON falukant_type.title;

CREATE TRIGGER trg_sync_title_tr_label
BEFORE INSERT OR UPDATE ON falukant_type.title
FOR EACH ROW
EXECUTE FUNCTION falukant_type.sync_title_tr_label();

COMMIT;
