"""add tool jobs and srs tables

Revision ID: a4f9c89b7d11
Revises: 3580c0dcd005
Create Date: 2026-04-12 18:00:00.000000

"""
from typing import Sequence, Union

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


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


def upgrade() -> None:
    op.create_table(
        "tool_jobs",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("tool_name", sa.String(length=128), nullable=False),
        sa.Column("status", sa.String(length=32), nullable=False, server_default="pending"),
        sa.Column("input_file_name", sa.String(length=255), nullable=False),
        sa.Column("input_file_path", sa.String(length=512), nullable=False),
        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("output_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_tool_jobs_id"), "tool_jobs", ["id"], unique=False)
    op.create_index(op.f("ix_tool_jobs_tool_name"), "tool_jobs", ["tool_name"], unique=False)
    op.create_index(op.f("ix_tool_jobs_user_id"), "tool_jobs", ["user_id"], unique=False)

    op.create_table(
        "srs_extractions",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("job_id", sa.Integer(), nullable=False),
        sa.Column("document_name", sa.String(length=255), nullable=False),
        sa.Column("document_title", sa.String(length=255), nullable=False),
        sa.Column("generated_at", sa.DateTime(), nullable=False),
        sa.Column("total_requirements", sa.Integer(), nullable=False),
        sa.Column("statistics", sa.JSON(), nullable=True),
        sa.Column("raw_output", sa.JSON(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["job_id"], ["tool_jobs.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("job_id"),
    )
    op.create_index(op.f("ix_srs_extractions_id"), "srs_extractions", ["id"], unique=False)

    op.create_table(
        "srs_requirements",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("extraction_id", sa.Integer(), nullable=False),
        sa.Column("requirement_uid", sa.String(length=64), nullable=False),
        sa.Column("title", sa.String(length=255), nullable=False),
        sa.Column("description", mysql.LONGTEXT(), nullable=False),
        sa.Column("priority", sa.String(length=16), nullable=False),
        sa.Column("acceptance_criteria", sa.JSON(), nullable=False),
        sa.Column("source_field", sa.String(length=255), nullable=False),
        sa.Column("section_number", sa.String(length=64), nullable=True),
        sa.Column("section_title", sa.String(length=255), nullable=True),
        sa.Column("requirement_type", sa.String(length=64), nullable=True),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["extraction_id"], ["srs_extractions.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("extraction_id", "requirement_uid", name="uq_srs_extraction_requirement_uid"),
    )
    op.create_index(op.f("ix_srs_requirements_id"), "srs_requirements", ["id"], unique=False)
    op.create_index(
        "idx_srs_requirements_extraction_sort",
        "srs_requirements",
        ["extraction_id", "sort_order"],
        unique=False,
    )


def downgrade() -> None:
    op.drop_index("idx_srs_requirements_extraction_sort", table_name="srs_requirements")
    op.drop_index(op.f("ix_srs_requirements_id"), table_name="srs_requirements")
    op.drop_table("srs_requirements")

    op.drop_index(op.f("ix_srs_extractions_id"), table_name="srs_extractions")
    op.drop_table("srs_extractions")

    op.drop_index(op.f("ix_tool_jobs_user_id"), table_name="tool_jobs")
    op.drop_index(op.f("ix_tool_jobs_tool_name"), table_name="tool_jobs")
    op.drop_index(op.f("ix_tool_jobs_id"), table_name="tool_jobs")
    op.drop_table("tool_jobs")
