"""Add versioned governance knowledge documents and Qwen embeddings."""
from alembic import op

revision = "20260716_40"
down_revision = "20260716_30"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.execute("""
        CREATE EXTENSION IF NOT EXISTS vector;
        CREATE TABLE IF NOT EXISTS public.governance_documents (
            id UUID PRIMARY KEY,
            object_uid UUID NOT NULL,
            object_type VARCHAR(40) NOT NULL,
            object_version INTEGER NOT NULL,
            object_name VARCHAR(300) NOT NULL,
            business_domain_uid UUID,
            permission_scope JSONB NOT NULL DEFAULT '{}'::jsonb,
            content TEXT NOT NULL,
            content_hash CHAR(64) NOT NULL,
            status VARCHAR(20) NOT NULL DEFAULT 'active' CHECK (status IN ('active','superseded','deleted')),
            source_updated_at TIMESTAMPTZ,
            created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
            UNIQUE (object_uid, object_version)
        );
        CREATE UNIQUE INDEX IF NOT EXISTS uq_governance_document_active ON public.governance_documents(object_uid) WHERE status = 'active';
        CREATE TABLE IF NOT EXISTS public.governance_chunks (
            id UUID PRIMARY KEY,
            document_id UUID NOT NULL REFERENCES public.governance_documents(id) ON DELETE CASCADE,
            chunk_no INTEGER NOT NULL,
            content TEXT NOT NULL,
            content_hash CHAR(64) NOT NULL,
            embedding vector(1024),
            embedding_provider VARCHAR(30),
            embedding_model VARCHAR(100),
            embedding_dimension INTEGER,
            UNIQUE (document_id, chunk_no)
        );
        CREATE TABLE IF NOT EXISTS public.governance_sync_jobs (
            id UUID PRIMARY KEY,
            job_type VARCHAR(30) NOT NULL CHECK (job_type IN ('incremental','full_audit')),
            status VARCHAR(20) NOT NULL,
            statistics JSONB NOT NULL DEFAULT '{}'::jsonb,
            last_error VARCHAR(1000),
            started_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
            finished_at TIMESTAMPTZ
        );
    """)


def downgrade() -> None:
    pass
