-- Idempotent SQL applied after `prisma db push`.
-- Adds Postgres full-text-search columns + triggers + GIN indexes for Ticket and Comment.
-- Prisma can't express tsvector/triggers, so we manage them here.

-- Ticket.searchVector
ALTER TABLE "Ticket" ADD COLUMN IF NOT EXISTS "searchVector" tsvector;

CREATE INDEX IF NOT EXISTS ticket_search_idx ON "Ticket" USING GIN ("searchVector");

CREATE OR REPLACE FUNCTION ticket_search_trigger() RETURNS trigger AS $$
BEGIN
  NEW."searchVector" :=
    setweight(to_tsvector('english', coalesce(NEW."displayId", '')), 'A') ||
    setweight(to_tsvector('english', coalesce(NEW."title", '')), 'A') ||
    setweight(to_tsvector('english', coalesce(NEW."overview", '')), 'B');
  RETURN NEW;
END
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS ticket_search_update ON "Ticket";
CREATE TRIGGER ticket_search_update
  BEFORE INSERT OR UPDATE OF "title", "overview", "displayId" ON "Ticket"
  FOR EACH ROW EXECUTE FUNCTION ticket_search_trigger();

-- Backfill any rows missing the vector (first run or new columns)
UPDATE "Ticket"
SET "searchVector" =
  setweight(to_tsvector('english', coalesce("displayId", '')), 'A') ||
  setweight(to_tsvector('english', coalesce("title", '')), 'A') ||
  setweight(to_tsvector('english', coalesce("overview", '')), 'B')
WHERE "searchVector" IS NULL;

-- Comment.searchVector
ALTER TABLE "Comment" ADD COLUMN IF NOT EXISTS "searchVector" tsvector;

CREATE INDEX IF NOT EXISTS comment_search_idx ON "Comment" USING GIN ("searchVector");

CREATE OR REPLACE FUNCTION comment_search_trigger() RETURNS trigger AS $$
BEGIN
  NEW."searchVector" := to_tsvector('english', coalesce(NEW."body", ''));
  RETURN NEW;
END
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS comment_search_update ON "Comment";
CREATE TRIGGER comment_search_update
  BEFORE INSERT OR UPDATE OF "body" ON "Comment"
  FOR EACH ROW EXECUTE FUNCTION comment_search_trigger();

UPDATE "Comment"
SET "searchVector" = to_tsvector('english', coalesce("body", ''))
WHERE "searchVector" IS NULL;
