"""Phase 9.1 — Membership Catalog schema."""
from alembic import op
import sqlalchemy as sa
from app.core.database import Base
import app.models  # noqa: F401

revision = "0002_phase_91_membership_catalog"
down_revision = "0001_initial"
branch_labels = None
depends_on = None


def upgrade():
    bind = op.get_bind()
    # Additive columns on membership_types (backward compatible)
    columns = {c["name"] for c in sa.inspect(bind).get_columns("membership_types")}
    with op.batch_alter_table("membership_types") as batch:
        if "package_id" not in columns:
            batch.add_column(sa.Column("package_id", sa.String(36), nullable=True))
        if "plan_id" not in columns:
            batch.add_column(sa.Column("plan_id", sa.String(36), nullable=True))
        if "pricing_model_id" not in columns:
            batch.add_column(sa.Column("pricing_model_id", sa.String(36), nullable=True))
        if "age_group_id" not in columns:
            batch.add_column(sa.Column("age_group_id", sa.String(36), nullable=True))
        if "sport_category_id" not in columns:
            batch.add_column(sa.Column("sport_category_id", sa.String(36), nullable=True))
        if "renewal_policy_id" not in columns:
            batch.add_column(sa.Column("renewal_policy_id", sa.String(36), nullable=True))
        if "freezing_rule_id" not in columns:
            batch.add_column(sa.Column("freezing_rule_id", sa.String(36), nullable=True))
        if "expiration_policy_id" not in columns:
            batch.add_column(sa.Column("expiration_policy_id", sa.String(36), nullable=True))
        if "is_transferable" not in columns:
            batch.add_column(
                sa.Column("is_transferable", sa.Boolean(), nullable=False, server_default=sa.false())
            )
        if "sort_order" not in columns:
            batch.add_column(
                sa.Column("sort_order", sa.Integer(), nullable=False, server_default="0")
            )
    # Create new catalog tables from metadata (idempotent for missing tables)
    Base.metadata.create_all(bind=bind)


def downgrade():
    bind = op.get_bind()
    for table in (
        "expiration_policies",
        "freezing_rules",
        "renewal_policies",
        "membership_rules",
        "membership_plans",
        "membership_packages",
        "pricing_models",
        "age_groups",
        "sport_categories",
    ):
        if sa.inspect(bind).has_table(table):
            op.drop_table(table)
    columns = {c["name"] for c in sa.inspect(bind).get_columns("membership_types")}
    with op.batch_alter_table("membership_types") as batch:
        for col in (
            "package_id",
            "plan_id",
            "pricing_model_id",
            "age_group_id",
            "sport_category_id",
            "renewal_policy_id",
            "freezing_rule_id",
            "expiration_policy_id",
            "is_transferable",
            "sort_order",
        ):
            if col in columns:
                batch.drop_column(col)
