"""Convert tests.attack_success from Boolean to a 3-value enum.

Revision ID: b058
Revises: b057
Create Date: 2026-07-06
"""

from alembic import op
import sqlalchemy as sa

revision = "b058"
down_revision = "b057"
branch_labels = None
depends_on = None

_ENUM_NAME = "attacksuccessresult"


def upgrade() -> None:
    attack_success_enum = sa.Enum(
        "successful", "not_successful", "partially_successful", name=_ENUM_NAME
    )
    attack_success_enum.create(op.get_bind(), checkfirst=True)

    op.add_column("tests", sa.Column("attack_success_new", attack_success_enum, nullable=True))

    op.execute(
        "UPDATE tests SET attack_success_new = (CASE "
        "WHEN attack_success = true THEN 'successful' "
        "WHEN attack_success = false THEN 'not_successful' "
        f"ELSE NULL END)::{_ENUM_NAME}"
    )

    op.drop_column("tests", "attack_success")
    op.alter_column("tests", "attack_success_new", new_column_name="attack_success")


def downgrade() -> None:
    op.add_column("tests", sa.Column("attack_success_bool", sa.Boolean(), nullable=True))
    op.execute(
        "UPDATE tests SET attack_success_bool = CASE "
        "WHEN attack_success = 'successful' THEN true "
        "WHEN attack_success = 'not_successful' THEN false "
        "ELSE NULL END"
    )
    op.drop_column("tests", "attack_success")
    op.alter_column("tests", "attack_success_bool", new_column_name="attack_success")
    sa.Enum(name=_ENUM_NAME).drop(op.get_bind(), checkfirst=True)
