"""add consistency comparison tables

Revision ID: d4c2a9e8b731
Revises: c9f6e7a1bd34
Create Date: 2026-05-10 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


revision: str = "d4c2a9e8b731"
down_revision: Union[str, None] = "c9f6e7a1bd34"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "code_knowledge_bases",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("project_path", sa.String(length=1024), nullable=True),
        sa.Column("vector_path", sa.String(length=1024), nullable=False),
        sa.Column("metadata_path", sa.String(length=1024), nullable=False),
        sa.Column("graph_path", sa.String(length=1024), nullable=False),
        sa.Column("status", sa.String(length=32), nullable=False),
        sa.Column("metadata_summary", sa.JSON(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_code_knowledge_bases_id"), "code_knowledge_bases", ["id"], unique=False)
    op.create_index(op.f("ix_code_knowledge_bases_status"), "code_knowledge_bases", ["status"], unique=False)
    op.create_index(op.f("ix_code_knowledge_bases_user_id"), "code_knowledge_bases", ["user_id"], unique=False)

    op.create_table(
        "consistency_jobs",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("srs_extraction_id", sa.Integer(), nullable=False),
        sa.Column("code_kb_id", sa.Integer(), nullable=False),
        sa.Column("status", sa.String(length=32), nullable=False),
        sa.Column("total_requirements", sa.Integer(), nullable=False),
        sa.Column("completed_requirements", sa.Integer(), nullable=False),
        sa.Column("output_summary", sa.JSON(), nullable=True),
        sa.Column("error_message", sa.Text(), nullable=True),
        sa.Column("started_at", sa.DateTime(), nullable=True),
        sa.Column("completed_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["code_kb_id"], ["code_knowledge_bases.id"]),
        sa.ForeignKeyConstraint(["srs_extraction_id"], ["srs_extractions.id"]),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_consistency_jobs_code_kb_id"), "consistency_jobs", ["code_kb_id"], unique=False)
    op.create_index(op.f("ix_consistency_jobs_id"), "consistency_jobs", ["id"], unique=False)
    op.create_index(op.f("ix_consistency_jobs_srs_extraction_id"), "consistency_jobs", ["srs_extraction_id"], unique=False)
    op.create_index(op.f("ix_consistency_jobs_status"), "consistency_jobs", ["status"], unique=False)
    op.create_index(op.f("ix_consistency_jobs_user_id"), "consistency_jobs", ["user_id"], unique=False)

    op.create_table(
        "consistency_results",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("job_id", sa.Integer(), nullable=False),
        sa.Column("requirement_uid", sa.String(length=64), nullable=False),
        sa.Column("verdict", sa.String(length=32), nullable=False),
        sa.Column("coverage_score", sa.Float(), nullable=False),
        sa.Column("confidence", sa.Float(), nullable=False),
        sa.Column("matched_functions", sa.JSON(), nullable=False),
        sa.Column("covered_points", sa.JSON(), nullable=False),
        sa.Column("missing_points", sa.JSON(), nullable=False),
        sa.Column("conflict_points", sa.JSON(), nullable=False),
        sa.Column("call_chain_evidence", sa.JSON(), nullable=False),
        sa.Column("suggestion", sa.Text(), nullable=True),
        sa.Column("raw_judgment", sa.JSON(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["job_id"], ["consistency_jobs.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_consistency_results_id"), "consistency_results", ["id"], unique=False)
    op.create_index(op.f("ix_consistency_results_job_id"), "consistency_results", ["job_id"], unique=False)
    op.create_index(op.f("ix_consistency_results_requirement_uid"), "consistency_results", ["requirement_uid"], unique=False)
    op.create_index(op.f("ix_consistency_results_verdict"), "consistency_results", ["verdict"], unique=False)


def downgrade() -> None:
    op.drop_index(op.f("ix_consistency_results_verdict"), table_name="consistency_results")
    op.drop_index(op.f("ix_consistency_results_requirement_uid"), table_name="consistency_results")
    op.drop_index(op.f("ix_consistency_results_job_id"), table_name="consistency_results")
    op.drop_index(op.f("ix_consistency_results_id"), table_name="consistency_results")
    op.drop_table("consistency_results")

    op.drop_index(op.f("ix_consistency_jobs_user_id"), table_name="consistency_jobs")
    op.drop_index(op.f("ix_consistency_jobs_status"), table_name="consistency_jobs")
    op.drop_index(op.f("ix_consistency_jobs_srs_extraction_id"), table_name="consistency_jobs")
    op.drop_index(op.f("ix_consistency_jobs_id"), table_name="consistency_jobs")
    op.drop_index(op.f("ix_consistency_jobs_code_kb_id"), table_name="consistency_jobs")
    op.drop_table("consistency_jobs")

    op.drop_index(op.f("ix_code_knowledge_bases_user_id"), table_name="code_knowledge_bases")
    op.drop_index(op.f("ix_code_knowledge_bases_status"), table_name="code_knowledge_bases")
    op.drop_index(op.f("ix_code_knowledge_bases_id"), table_name="code_knowledge_bases")
    op.drop_table("code_knowledge_bases")

