"""add promo code discount level

Revision ID: 046_promo_code_discount_level
Revises: 045_promo_code_usage
Create Date: 2026-06-19
"""

import sqlalchemy as sa
from alembic import op


revision = "046_promo_code_discount_level"
down_revision = "045_promo_code_usage"
branch_labels = None
depends_on = None


def _columns(bind, table_name: str) -> set[str]:
    return {column["name"] for column in sa.inspect(bind).get_columns(table_name)}


def upgrade() -> None:
    bind = op.get_bind()
    columns = _columns(bind, "promo_codes")
    if "discount_level" not in columns:
        op.add_column(
            "promo_codes",
            sa.Column(
                "discount_level",
                sa.String(length=16),
                nullable=False,
                server_default=sa.text("'order'"),
            ),
        )


def downgrade() -> None:
    bind = op.get_bind()
    columns = _columns(bind, "promo_codes")
    if "discount_level" in columns:
        op.drop_column("promo_codes", "discount_level")
