"""initial schema

Revision ID: 40de6c7e554f
Revises:
Create Date: 2025-12-01 17:16:09.650374
"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = "40de6c7e554f"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "documents",
        sa.Column("id", sa.UUID(), server_default=sa.text("gen_random_uuid()"), nullable=False),
        sa.Column("domain", sa.String(), nullable=False),
        sa.Column("url", sa.String(), nullable=False),
        sa.Column("path", sa.String(), nullable=False),
        sa.Column("content_hash", sa.String(), nullable=False),
        sa.Column("content_refreshed_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("generation_id", sa.UUID(), nullable=False),
        sa.Column("generation_active", sa.Boolean(), server_default="false", nullable=False),
        sa.Column("inserted_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("url"),
    )
    op.create_index(op.f("ix_documents_domain"), "documents", ["domain"], unique=False)
    op.create_table(
        "queries",
        sa.Column("id", sa.UUID(), server_default=sa.text("gen_random_uuid()"), nullable=False),
        sa.Column("query", sa.Text(), nullable=False),
        sa.Column("language", sa.String(), nullable=True),
        sa.Column("library", sa.String(), nullable=True),
        sa.Column("version", sa.String(), nullable=True),
        sa.Column("output", sa.Text(), nullable=False),
        sa.Column("messages", sa.Text(), nullable=True),
        sa.Column("parent_id", sa.UUID(), nullable=True),
        sa.Column(
            "query_tsvector",
            postgresql.TSVECTOR(),
            sa.Computed("to_tsvector('english', query)", persisted=True),
            nullable=True,
        ),
        sa.Column("inserted_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.ForeignKeyConstraint(
            ["parent_id"],
            ["queries.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "ratings",
        sa.Column("id", sa.UUID(), server_default=sa.text("gen_random_uuid()"), nullable=False),
        sa.Column("query_id", sa.UUID(), nullable=False),
        sa.Column("correctness", sa.Integer(), nullable=False),
        sa.Column("relevance", sa.Integer(), nullable=False),
        sa.Column("usefulness", sa.Integer(), nullable=False),
        sa.Column("reasoning", sa.Text(), nullable=True),
        sa.Column("agent_model", sa.String(), nullable=True),
        sa.Column("agent_system", sa.String(), nullable=True),
        sa.Column("agent_version", sa.String(), nullable=True),
        sa.Column("inserted_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.CheckConstraint("correctness BETWEEN 1 AND 5", name="correctness_range_check"),
        sa.CheckConstraint("relevance BETWEEN 1 AND 5", name="relevance_range_check"),
        sa.CheckConstraint("usefulness BETWEEN 1 AND 5", name="usefulness_range_check"),
        sa.ForeignKeyConstraint(
            ["query_id"],
            ["queries.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "sections",
        sa.Column("id", sa.UUID(), server_default=sa.text("gen_random_uuid()"), nullable=False),
        sa.Column("document_id", sa.UUID(), nullable=False),
        sa.Column("parent_section_id", sa.UUID(), nullable=True),
        sa.Column("heading", sa.String(), nullable=True),
        sa.Column("level", sa.Integer(), nullable=False),
        sa.Column("content", sa.Text(), nullable=False),
        sa.Column("position", sa.Integer(), nullable=False),
        sa.Column("heading_path", sa.String(), nullable=True),
        sa.Column(
            "content_tsvector",
            postgresql.TSVECTOR(),
            sa.Computed("to_tsvector('english', content)", persisted=True),
            nullable=True,
        ),
        sa.Column("inserted_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.ForeignKeyConstraint(["document_id"], ["documents.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["parent_section_id"], ["sections.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_sections_document_id"), "sections", ["document_id"], unique=False)
    op.create_index(op.f("ix_sections_parent_section_id"), "sections", ["parent_section_id"], unique=False)
    # GIN indexes for full-text search on tsvector columns
    op.create_index("ix_queries_query_tsvector", "queries", ["query_tsvector"], postgresql_using="gin")
    op.create_index("ix_sections_content_tsvector", "sections", ["content_tsvector"], postgresql_using="gin")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # Drop GIN indexes for full-text search
    op.drop_index("ix_sections_content_tsvector", table_name="sections")
    op.drop_index("ix_queries_query_tsvector", table_name="queries")
    op.drop_index(op.f("ix_sections_parent_section_id"), table_name="sections")
    op.drop_index(op.f("ix_sections_document_id"), table_name="sections")
    op.drop_table("sections")
    op.drop_table("ratings")
    op.drop_table("queries")
    op.drop_index(op.f("ix_documents_domain"), table_name="documents")
    op.drop_table("documents")
    # ### end Alembic commands ###
