"""Add complimentary status to subscription enum; make stripe IDs nullable for internal subscriptions.

Revision ID: 031
Revises: 030

Free and staff plan workspaces now get an internal subscription record (no Stripe IDs).
"""
from alembic import op
import sqlalchemy as sa

revision = "031"
down_revision = "030"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # Add new value to the existing enum (safe; existing rows unaffected)
    op.execute("ALTER TYPE p2hs_subscription_status ADD VALUE IF NOT EXISTS 'complimentary'")

    # Allow NULL stripe IDs — internal (no-charge) subscriptions have no Stripe counterpart
    op.alter_column("subscriptions", "stripe_subscription_id", nullable=True)
    op.alter_column("subscriptions", "stripe_customer_id", nullable=True)


def downgrade() -> None:
    # Remove internal subscriptions before reversing nullable change
    op.execute("DELETE FROM subscriptions WHERE stripe_subscription_id IS NULL")
    op.alter_column("subscriptions", "stripe_subscription_id", nullable=False)
    op.alter_column("subscriptions", "stripe_customer_id", nullable=False)
    # PostgreSQL does not support removing enum values; complimentary stays in the type
