"""Drop legacy Core commercial tables (plans/features/subscriptions).

Commercial Runtime (`commercial_*`) is the sole commercial SoT.

Revision ID: 0008_drop_legacy_commercial
Revises: 0007_commercial_runtime
Create Date: 2026-07-28
"""
from __future__ import annotations

from typing import Sequence, Union

from alembic import op

revision: str = "0008_drop_legacy_commercial"
down_revision: Union[str, None] = "0007_commercial_runtime"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # Order: dependents first. IF EXISTS for environments partially migrated.
    op.execute("DROP TABLE IF EXISTS tenant_feature_access CASCADE")
    op.execute("DROP TABLE IF EXISTS tenant_subscriptions CASCADE")
    op.execute("DROP TABLE IF EXISTS plan_features CASCADE")
    op.execute("DROP TABLE IF EXISTS features CASCADE")
    op.execute("DROP TABLE IF EXISTS plans CASCADE")
    op.execute("DROP TYPE IF EXISTS subscription_status")


def downgrade() -> None:
    # Intentionally irreversible — restore from Commercial Runtime / backups if needed.
    raise NotImplementedError(
        "Legacy Core commercial tables cannot be restored; use Commercial Runtime."
    )
