"""add_debug_attempts_table

Revision ID: 4b518923afb2
Revises: 001_initial_schema
Create Date: 2025-11-20 00:00:00

"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "4b518923afb2"
down_revision: str | Sequence[str] | None = "001_initial_schema"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    """Add debug_attempts table for AutoDebugAgent."""
    op.create_table(
        "debug_attempts",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("plan_id", sa.Integer(), nullable=False),
        sa.Column("error_message", sa.Text(), nullable=False),
        sa.Column("attempted_fix", sa.Text(), nullable=True),
        sa.Column("success", sa.Boolean(), nullable=False, server_default="0"),
        sa.Column("attempt_number", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["plan_id"],
            ["plans.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_debug_attempts_plan_id"), "debug_attempts", ["plan_id"], unique=False
    )


def downgrade() -> None:
    """Remove debug_attempts table."""
    op.drop_index(op.f("ix_debug_attempts_plan_id"), table_name="debug_attempts")
    op.drop_table("debug_attempts")
