"""add intelligence job recovery attempts

Revision ID: d4e7f9a1b2c3
Revises: c2f4a6b8d0e1
Create Date: 2026-08-11
"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "d4e7f9a1b2c3"
down_revision: Union[str, Sequence[str], None] = "c2f4a6b8d0e1"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    inspector = sa.inspect(op.get_bind())
    analysis_columns = {column["name"] for column in inspector.get_columns("campaign_analyses")}
    if "recovery_attempts" not in analysis_columns:
        op.add_column(
            "campaign_analyses",
            sa.Column("recovery_attempts", sa.Integer(), nullable=False, server_default="0"),
        )
    comparison_columns = {
        column["name"] for column in inspector.get_columns("campaign_period_comparisons")
    }
    if "recovery_attempts" not in comparison_columns:
        op.add_column(
            "campaign_period_comparisons",
            sa.Column("recovery_attempts", sa.Integer(), nullable=False, server_default="0"),
        )


def downgrade() -> None:
    inspector = sa.inspect(op.get_bind())
    comparison_columns = {
        column["name"] for column in inspector.get_columns("campaign_period_comparisons")
    }
    if "recovery_attempts" in comparison_columns:
        op.drop_column("campaign_period_comparisons", "recovery_attempts")
    analysis_columns = {column["name"] for column in inspector.get_columns("campaign_analyses")}
    if "recovery_attempts" in analysis_columns:
        op.drop_column("campaign_analyses", "recovery_attempts")
