from __future__ import annotations


def base_table_statements() -> tuple[str, ...]:
    return (
        "CREATE EXTENSION IF NOT EXISTS vector",
        """
        CREATE TABLE IF NOT EXISTS rag_sessions (
            rag_session_id VARCHAR(64) PRIMARY KEY,
            project_id VARCHAR(512) NOT NULL,
            created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
        )
        """,
        """
        CREATE TABLE IF NOT EXISTS rag_index_jobs (
            index_job_id VARCHAR(64) PRIMARY KEY,
            rag_session_id VARCHAR(64) NOT NULL,
            status VARCHAR(16) NOT NULL,
            indexed_files INTEGER NOT NULL DEFAULT 0,
            failed_files INTEGER NOT NULL DEFAULT 0,
            cache_hit_files INTEGER NOT NULL DEFAULT 0,
            cache_miss_files INTEGER NOT NULL DEFAULT 0,
            error_code VARCHAR(128) NULL,
            error_desc TEXT NULL,
            error_module VARCHAR(64) NULL,
            created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
        )
        """,
        """
        CREATE TABLE IF NOT EXISTS rag_chunks (
            id BIGSERIAL PRIMARY KEY,
            rag_session_id VARCHAR(64) NOT NULL,
            path TEXT NOT NULL,
            chunk_index INTEGER NOT NULL,
            content TEXT NOT NULL,
            embedding vector NULL,
            layer VARCHAR(64) NULL,
            lang VARCHAR(32) NULL,
            repo_id VARCHAR(512) NULL,
            commit_sha VARCHAR(128) NULL,
            title TEXT NULL,
            metadata_json JSONB NOT NULL DEFAULT '{}'::jsonb,
            span_start INTEGER NULL,
            span_end INTEGER NULL,
            created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
        )
        """,
        """
        CREATE TABLE IF NOT EXISTS rag_document_cache (
            id BIGSERIAL PRIMARY KEY,
            repo_id VARCHAR(512) NOT NULL,
            blob_sha VARCHAR(128) NOT NULL,
            chunk_index INTEGER NOT NULL,
            path TEXT NOT NULL,
            layer VARCHAR(64) NULL,
            lang VARCHAR(32) NULL,
            title TEXT NULL,
            content TEXT NOT NULL,
            embedding vector NULL,
            metadata_json JSONB NOT NULL DEFAULT '{}'::jsonb,
            links_json JSONB NOT NULL DEFAULT '[]'::jsonb,
            span_start INTEGER NULL,
            span_end INTEGER NULL,
            commit_sha VARCHAR(128) NULL,
            created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
            CONSTRAINT uq_rag_document_cache UNIQUE (repo_id, blob_sha, chunk_index)
        )
        """,
    )


def index_statements() -> tuple[str, ...]:
    return (
        "CREATE INDEX IF NOT EXISTS idx_rag_chunks_session ON rag_chunks (rag_session_id)",
        "CREATE INDEX IF NOT EXISTS idx_rag_chunks_layer ON rag_chunks (rag_session_id, layer)",
        "CREATE INDEX IF NOT EXISTS idx_rag_chunks_layer_path ON rag_chunks (rag_session_id, layer, path)",
        "CREATE INDEX IF NOT EXISTS idx_rag_chunks_path ON rag_chunks (path)",
        "CREATE INDEX IF NOT EXISTS idx_rag_chunks_repo_path ON rag_chunks (repo_id, path)",
        "CREATE INDEX IF NOT EXISTS idx_rag_chunks_metadata_json ON rag_chunks USING GIN (metadata_json)",
        "CREATE INDEX IF NOT EXISTS idx_rag_document_cache_repo_blob ON rag_document_cache (repo_id, blob_sha, chunk_index)",
    )


def legacy_drop_statements() -> tuple[str, ...]:
    return (
        "DROP INDEX IF EXISTS idx_rag_chunks_qname",
        "DROP INDEX IF EXISTS idx_rag_chunks_symbol_id",
        "DROP INDEX IF EXISTS idx_rag_chunks_module_id",
        "DROP INDEX IF EXISTS idx_rag_chunks_doc_kind",
        "DROP INDEX IF EXISTS idx_rag_chunks_entrypoint",
        "DROP INDEX IF EXISTS idx_rag_blob_cache_repo_blob",
        "DROP INDEX IF EXISTS idx_rag_chunk_cache_repo_blob",
        "DROP INDEX IF EXISTS idx_rag_session_chunk_map_session",
        "DROP TABLE IF EXISTS rag_blob_cache",
        "DROP TABLE IF EXISTS rag_chunk_cache",
        "DROP TABLE IF EXISTS rag_session_chunk_map",
    )


def legacy_chunk_columns() -> tuple[str, ...]:
    return (
        "artifact_type",
        "section",
        "doc_id",
        "doc_version",
        "owner",
        "system_component",
        "last_modified",
        "staleness_score",
        "symbol_id",
        "qname",
        "kind",
        "framework",
        "entrypoint_type",
        "module_id",
        "section_path",
        "doc_kind",
        "rag_doc_id",
        "links_json",
    )
