-- ROLLBACK: restore the Swedish schema
--
-- Generated by scripts/generate_rename_migration.py from _postgres/rename_map.py.
-- Do not edit by hand; edit the map and regenerate.
--
-- ALTER TABLE ... RENAME is catalog-only in PostgreSQL: no table rewrite, no data
-- movement, no index rebuild. Wall time is milliseconds regardless of row count,
-- and the HNSW vector indexes are not touched.
--
-- Guarded by an existence check, so this is a no-op on a database created from the
-- current schema.sql and does the work on one created before the rename. One file
-- serves both a fresh install and an existing deployment.

BEGIN;

SET LOCAL lock_timeout = '5s';
SET LOCAL statement_timeout = 0;

DO $rename$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.columns
        WHERE table_schema = 'public' AND table_name = 'speeches'
          AND column_name = 'text') THEN
    -- indexes
    ALTER INDEX IF EXISTS speeches_search_idx RENAME TO talks_search_idx;
    ALTER INDEX IF EXISTS speeches_debate_idx RENAME TO talks_debate_idx;
    ALTER INDEX IF EXISTS speeches_party_idx RENAME TO talks_parti_idx;
    ALTER INDEX IF EXISTS speeches_date_idx RENAME TO talks_datum_idx;
    ALTER INDEX IF EXISTS speeches_year_idx RENAME TO talks_year_idx;
    ALTER INDEX IF EXISTS speeches_person_idx RENAME TO talks_intressent_idx;
    ALTER INDEX IF EXISTS speeches_speaker_idx RENAME TO talks_talare_idx;
    ALTER INDEX IF EXISTS speeches_source_doc_idx RENAME TO talks_dok_id_idx;
    ALTER INDEX IF EXISTS speeches_summary_embedding_idx RENAME TO talks_summary_embedding_idx;
    ALTER INDEX IF EXISTS speech_chunks_speech_idx RENAME TO chunks_talk_idx;
    ALTER INDEX IF EXISTS speech_chunks_embedding_idx RENAME TO chunks_embedding_idx;
    ALTER INDEX IF EXISTS debates_date_idx RENAME TO debates_datum_idx;
    ALTER INDEX IF EXISTS documents_search_idx RENAME TO motions_search_idx;
    ALTER INDEX IF EXISTS documents_date_idx RENAME TO motions_datum_idx;
    ALTER INDEX IF EXISTS documents_session_year_idx RENAME TO motions_year_idx;
    ALTER INDEX IF EXISTS documents_committee_idx RENAME TO motions_organ_idx;
    ALTER INDEX IF EXISTS documents_parties_idx RENAME TO motions_parties_idx;
    ALTER INDEX IF EXISTS document_authors_person_idx RENAME TO motion_authors_intressent_idx;
    ALTER INDEX IF EXISTS document_chunks_doc_idx RENAME TO motion_chunks_motion_idx;
    ALTER INDEX IF EXISTS document_chunks_embedding_idx RENAME TO motion_chunks_embedding_idx;
    ALTER INDEX IF EXISTS document_proposals_doc_idx RENAME TO motion_yrkanden_dok_idx;
    ALTER INDEX IF EXISTS document_proposals_embedding_idx RENAME TO motion_yrkanden_embedding_idx;

    -- tables
    ALTER TABLE documents DROP COLUMN IF EXISTS doc_type;
    ALTER TABLE speeches RENAME TO talks;
    ALTER TABLE speech_chunks RENAME TO chunks;
    ALTER TABLE documents RENAME TO motions;
    ALTER TABLE document_chunks RENAME TO motion_chunks;
    ALTER TABLE document_authors RENAME TO motion_authors;
    ALTER TABLE document_proposals RENAME TO motion_yrkanden;

    -- columns
    ALTER TABLE people RENAME COLUMN person_id TO intressent_id;
    ALTER TABLE people RENAME COLUMN source_record_id TO hangar_id;
    ALTER TABLE people RENAME COLUMN source_record_guid TO hangar_guid;
    ALTER TABLE people RENAME COLUMN source_id TO sourceid;
    ALTER TABLE people RENAME COLUMN birth_year TO fodd_ar;
    ALTER TABLE people RENAME COLUMN gender TO kon;
    ALTER TABLE people RENAME COLUMN last_name TO efternamn;
    ALTER TABLE people RENAME COLUMN first_name TO tilltalsnamn;
    ALTER TABLE people RENAME COLUMN sort_name TO sorteringsnamn;
    ALTER TABLE people RENAME COLUMN home_town TO iort;
    ALTER TABLE people RENAME COLUMN party TO parti;
    ALTER TABLE people RENAME COLUMN constituency TO valkrets;
    ALTER TABLE people RENAME COLUMN source_url TO person_url_xml;
    ALTER TABLE people RENAME COLUMN image_url_small TO bild_url_80;
    ALTER TABLE people RENAME COLUMN image_url_medium TO bild_url_192;
    ALTER TABLE people RENAME COLUMN image_url_large TO bild_url_max;
    ALTER TABLE people RENAME COLUMN assignments TO personuppdrag;
    ALTER TABLE people RENAME COLUMN contact_details TO personuppgift;
    ALTER TABLE people RENAME COLUMN name TO namn;
    ALTER TABLE people RENAME COLUMN active TO aktiv;
    ALTER TABLE talks RENAME COLUMN source_speech_id TO anforande_id;
    ALTER TABLE talks RENAME COLUMN text TO anforandetext;
    ALTER TABLE talks RENAME COLUMN section_title TO avsnittsrubrik;
    ALTER TABLE talks RENAME COLUMN sequence TO anforande_nummer;
    ALTER TABLE talks RENAME COLUMN activity_type TO kammaraktivitet;
    ALTER TABLE talks RENAME COLUMN speaker_name TO talare;
    ALTER TABLE talks RENAME COLUMN party TO parti;
    ALTER TABLE talks RENAME COLUMN person_id TO intressent_id;
    ALTER TABLE talks RENAME COLUMN date TO datum;
    ALTER TABLE talks RENAME COLUMN source_datetime TO dok_datum;
    ALTER TABLE talks RENAME COLUMN session_year TO period;
    ALTER TABLE talks RENAME COLUMN related_doc_id TO rel_dok_id;
    ALTER TABLE talks RENAME COLUMN source_doc_number TO dok_nummer;
    ALTER TABLE talks RENAME COLUMN source_record_id TO hangar_id;
    ALTER TABLE talks RENAME COLUMN source_doc_id TO dok_id;
    ALTER TABLE talks RENAME COLUMN title TO titel;
    ALTER TABLE talks RENAME COLUMN debate_id TO debate;
    ALTER TABLE talks RENAME COLUMN is_reply TO replik;
    ALTER TABLE talks RENAME COLUMN url_video TO debateurl;
    ALTER TABLE talks RENAME COLUMN url_audio_file TO audiofileurl;
    ALTER TABLE talks RENAME COLUMN audio_start_seconds TO startpos;
    ALTER TABLE chunks RENAME COLUMN speech_id TO talk_id;
    ALTER TABLE debates RENAME COLUMN id TO debate;
    ALTER TABLE debates RENAME COLUMN date TO datum;
    ALTER TABLE motions RENAME COLUMN doc_id TO dok_id;
    ALTER TABLE motions RENAME COLUMN source_record_id TO hangar_id;
    ALTER TABLE motions RENAME COLUMN session_label TO rm;
    ALTER TABLE motions RENAME COLUMN designation TO beteckning;
    ALTER TABLE motions RENAME COLUMN subtype TO subtyp;
    ALTER TABLE motions RENAME COLUMN committee TO organ;
    ALTER TABLE motions RENAME COLUMN date TO datum;
    ALTER TABLE motions RENAME COLUMN source_updated_at TO systemdatum;
    ALTER TABLE motions RENAME COLUMN published_at TO publicerad;
    ALTER TABLE motions RENAME COLUMN session_year TO year;
    ALTER TABLE motions RENAME COLUMN title TO titel;
    ALTER TABLE motions RENAME COLUMN subtitle TO undertitel;
    ALTER TABLE motions RENAME COLUMN proposals_text TO forslag_text;
    ALTER TABLE motions RENAME COLUMN url_text TO dokument_url_text;
    ALTER TABLE motions RENAME COLUMN url_html TO dokument_url_html;
    ALTER TABLE motions RENAME COLUMN url_pdf TO pdf_url;
    ALTER TABLE motions RENAME COLUMN proposals_raw TO forslag;
    ALTER TABLE motions RENAME COLUMN attachments TO bilagor;
    ALTER TABLE motions RENAME COLUMN num_proposals TO num_yrkanden;
    ALTER TABLE motion_authors RENAME COLUMN doc_id TO dok_id;
    ALTER TABLE motion_authors RENAME COLUMN person_id TO intressent_id;
    ALTER TABLE motion_authors RENAME COLUMN name TO namn;
    ALTER TABLE motion_authors RENAME COLUMN party TO partibet;
    ALTER TABLE motion_authors RENAME COLUMN role TO roll;
    ALTER TABLE motion_chunks RENAME COLUMN doc_id TO motion_id;
    ALTER TABLE motion_yrkanden RENAME COLUMN doc_id TO dok_id;
    ALTER TABLE motion_yrkanden RENAME COLUMN number TO nummer;
    ALTER TABLE motion_yrkanden RENAME COLUMN text TO lydelse;
    ALTER TABLE motion_yrkanden RENAME COLUMN committee_recommendation TO utskottet;
    ALTER TABLE motion_yrkanden RENAME COLUMN chamber_decision TO kammaren;
    ALTER TABLE motion_yrkanden RENAME COLUMN handled_in TO behandlas_i;
    ALTER TABLE chat_sessions RENAME COLUMN person_id TO intressent_id;
    ALTER TABLE chat_sessions RENAME COLUMN initial_speech_id TO initial_talk_id;
    ALTER TABLE chat_snapshots RENAME COLUMN person_id TO intressent_id;
    ALTER TABLE chat_snapshots RENAME COLUMN initial_speech_id TO initial_talk_id;
END IF;
END
$rename$;

-- Restore the original trigger functions.
DROP TRIGGER IF EXISTS speeches_search_vector_trigger ON talks;
DROP FUNCTION IF EXISTS speeches_search_vector_update();

CREATE OR REPLACE FUNCTION talks_search_vector_update() RETURNS TRIGGER AS $fn$
BEGIN
    NEW.search_vector := to_tsvector('swedish', coalesce(NEW.anforandetext, ''));
    RETURN NEW;
END;
$fn$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS talks_search_vector_trigger ON talks;
CREATE TRIGGER talks_search_vector_trigger
    BEFORE INSERT OR UPDATE OF anforandetext ON talks
    FOR EACH ROW EXECUTE FUNCTION talks_search_vector_update();

DROP TRIGGER IF EXISTS documents_search_vector_trigger ON motions;
DROP FUNCTION IF EXISTS documents_search_vector_update();

CREATE OR REPLACE FUNCTION motions_search_vector_update() RETURNS TRIGGER AS $fn$
BEGIN
    NEW.search_vector :=
        setweight(to_tsvector('swedish', coalesce(NEW.titel, '')), 'A') ||
        setweight(to_tsvector('swedish', coalesce(NEW.forslag_text, '')), 'B') ||
        setweight(to_tsvector('swedish', coalesce(NEW.undertitel, '')), 'C') ||
        setweight(to_tsvector('swedish', coalesce(NEW.text, '')), 'D');
    RETURN NEW;
END;
$fn$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS motions_search_vector_trigger ON motions;
CREATE TRIGGER motions_search_vector_trigger
    BEFORE INSERT OR UPDATE OF titel, undertitel, text, forslag_text ON motions
    FOR EACH ROW EXECUTE FUNCTION motions_search_vector_update();

COMMIT;
