-- Unified Search: pgvector extension, FTS triggers, provider registry

-- pgvector extension
CREATE EXTENSION IF NOT EXISTS vector;

-- ─── Provider Registry Table ───
CREATE TABLE IF NOT EXISTS unified_search_providers (
    id          UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id   UUID NOT NULL,
    entity_type VARCHAR(50) NOT NULL,
    plugin_name VARCHAR(80) NOT NULL,
    is_active   BOOLEAN NOT NULL DEFAULT TRUE,
    config      JSONB NOT NULL DEFAULT '{}'::jsonb,
    deleted_at  TIMESTAMPTZ,
    created_at  TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at  TIMESTAMPTZ NOT NULL DEFAULT now(),
    UNIQUE(tenant_id, entity_type)
);

CREATE INDEX IF NOT EXISTS ix_usp_tenant ON unified_search_providers(tenant_id);

-- ─── Index Log Table ───
CREATE TABLE IF NOT EXISTS unified_search_index_log (
    id           UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id    UUID NOT NULL,
    entity_type  VARCHAR(50) NOT NULL,
    entity_id    UUID NOT NULL,
    action       VARCHAR(20) NOT NULL,
    status       VARCHAR(20) NOT NULL DEFAULT 'pending',
    error_message TEXT,
    deleted_at   TIMESTAMPTZ,
    created_at   TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS ix_usil_tenant ON unified_search_index_log(tenant_id);
CREATE INDEX IF NOT EXISTS ix_usil_entity ON unified_search_index_log(entity_type, entity_id);

-- ─── FTS: Mails ───
ALTER TABLE mails ADD COLUMN IF NOT EXISTS body_tsv tsvector;

CREATE OR REPLACE FUNCTION mails_tsv_trigger() RETURNS trigger AS $$
BEGIN
    NEW.body_tsv :=
        setweight(to_tsvector('pg_catalog.german', coalesce(NEW.subject, '')), 'A') ||
        setweight(to_tsvector('pg_catalog.german', coalesce(NEW.from_address, '')), 'B') ||
        setweight(to_tsvector('pg_catalog.german', coalesce(NEW.to_addresses, '')), 'C') ||
        setweight(to_tsvector('pg_catalog.german', coalesce(NEW.body_text, '')), 'D');
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS mails_tsv_update ON mails;
CREATE TRIGGER mails_tsv_update
    BEFORE INSERT OR UPDATE ON mails
    FOR EACH ROW EXECUTE FUNCTION mails_tsv_trigger();

CREATE INDEX IF NOT EXISTS ix_mails_body_tsv ON mails USING gin(body_tsv);

-- ─── FTS: Contacts ───
ALTER TABLE contacts ADD COLUMN IF NOT EXISTS search_tsv tsvector;

CREATE OR REPLACE FUNCTION contacts_tsv_trigger() RETURNS trigger AS $$
BEGIN
    NEW.search_tsv :=
        setweight(to_tsvector('pg_catalog.german', coalesce(NEW.first_name, '') || ' ' || coalesce(NEW.last_name, '')), 'A') ||
        setweight(to_tsvector('pg_catalog.german', coalesce(NEW.email, '')), 'B') ||
        setweight(to_tsvector('pg_catalog.german', coalesce(NEW.phone, '') || ' ' || coalesce(NEW.mobile, '')), 'C') ||
        setweight(to_tsvector('pg_catalog.german', coalesce(NEW.notes, '')), 'D');
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS contacts_tsv_update ON contacts;
CREATE TRIGGER contacts_tsv_update
    BEFORE INSERT OR UPDATE ON contacts
    FOR EACH ROW EXECUTE FUNCTION contacts_tsv_trigger();

CREATE INDEX IF NOT EXISTS ix_contacts_search_tsv ON contacts USING gin(search_tsv);

-- ─── FTS: Calendar Entries ───
ALTER TABLE calendar_entries ADD COLUMN IF NOT EXISTS search_tsv tsvector;

CREATE OR REPLACE FUNCTION calendar_entries_tsv_trigger() RETURNS trigger AS $$
BEGIN
    NEW.search_tsv :=
        setweight(to_tsvector('pg_catalog.german', coalesce(NEW.title, '')), 'A') ||
        setweight(to_tsvector('pg_catalog.german', coalesce(NEW.description, '')), 'B') ||
        setweight(to_tsvector('pg_catalog.german', coalesce(NEW.location, '')), 'C');
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS calendar_entries_tsv_update ON calendar_entries;
CREATE TRIGGER calendar_entries_tsv_update
    BEFORE INSERT OR UPDATE ON calendar_entries
    FOR EACH ROW EXECUTE FUNCTION calendar_entries_tsv_trigger();

CREATE INDEX IF NOT EXISTS ix_cal_entries_search_tsv ON calendar_entries USING gin(search_tsv);

-- ─── FTS: Files (content_text added here, not in 0002, to avoid trigger dependency) ───
ALTER TABLE files ADD COLUMN IF NOT EXISTS content_text text;
ALTER TABLE files ADD COLUMN IF NOT EXISTS content_tsv tsvector;

CREATE OR REPLACE FUNCTION files_tsv_trigger() RETURNS trigger AS $$
BEGIN
    NEW.content_tsv :=
        setweight(to_tsvector('pg_catalog.german', coalesce(NEW.name, '')), 'A') ||
        setweight(to_tsvector('pg_catalog.german', coalesce(NEW.content_text, '')), 'D');
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS files_tsv_update ON files;
CREATE TRIGGER files_tsv_update
    BEFORE INSERT OR UPDATE ON files
    FOR EACH ROW EXECUTE FUNCTION files_tsv_trigger();

CREATE INDEX IF NOT EXISTS ix_files_content_tsv ON files USING gin(content_tsv);

-- ─── FTS: Tags ───
ALTER TABLE tags ADD COLUMN IF NOT EXISTS search_tsv tsvector;

CREATE OR REPLACE FUNCTION tags_tsv_trigger() RETURNS trigger AS $$
BEGIN
    NEW.search_tsv := to_tsvector('pg_catalog.german', coalesce(NEW.name, ''));
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS tags_tsv_update ON tags;
CREATE TRIGGER tags_tsv_update
    BEFORE INSERT OR UPDATE ON tags
    FOR EACH ROW EXECUTE FUNCTION tags_tsv_trigger();

CREATE INDEX IF NOT EXISTS ix_tags_search_tsv ON tags USING gin(search_tsv);

-- ─── FTS: Audit Log ───
ALTER TABLE audit_log ADD COLUMN IF NOT EXISTS search_tsv tsvector;

CREATE OR REPLACE FUNCTION audit_log_tsv_trigger() RETURNS trigger AS $$
BEGIN
    NEW.search_tsv :=
        to_tsvector('pg_catalog.german',
            coalesce(NEW.entity_type, '') || ' ' ||
            coalesce(NEW.action, '') || ' ' ||
            coalesce(NEW.user_id::text, ''));
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS audit_log_tsv_update ON audit_log;
CREATE TRIGGER audit_log_tsv_update
    BEFORE INSERT OR UPDATE ON audit_log
    FOR EACH ROW EXECUTE FUNCTION audit_log_tsv_trigger();

CREATE INDEX IF NOT EXISTS ix_audit_log_search_tsv ON audit_log USING gin(search_tsv);
