"""user full refund quota controls

Revision ID: 059_user_refund_quota
Revises: 058_full_refund_limit_three
Create Date: 2026-06-21
"""

import sqlalchemy as sa
from alembic import op


revision = "059_user_refund_quota"
down_revision = "058_full_refund_limit_three"
branch_labels = None
depends_on = None


def _column_names(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()
    tables = set(sa.inspect(bind).get_table_names())
    if "users" not in tables:
        return

    columns = _column_names(bind, "users")
    if "full_refund_quota_extra" not in columns:
        op.add_column(
            "users",
            sa.Column("full_refund_quota_extra", sa.Integer(), nullable=False, server_default="0"),
        )
    if "full_refund_quota_reset_at" not in columns:
        op.add_column(
            "users",
            sa.Column("full_refund_quota_reset_at", sa.DateTime(timezone=True), nullable=True),
        )


def downgrade() -> None:
    bind = op.get_bind()
    tables = set(sa.inspect(bind).get_table_names())
    if "users" not in tables:
        return

    columns = _column_names(bind, "users")
    if "full_refund_quota_reset_at" in columns:
        op.drop_column("users", "full_refund_quota_reset_at")
    if "full_refund_quota_extra" in columns:
        op.drop_column("users", "full_refund_quota_extra")
