"""Add provider field to subscriptions

Revision ID: dc285eabea90
Revises: 4e77ec2a7faa
Create Date: 2025-07-25 22:19:01.846200

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "dc285eabea90"
down_revision: Union[str, None] = "4e77ec2a7faa"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # Add column as nullable
    op.add_column(
        "subscriptions", sa.Column("provider", sa.String(length=20), nullable=True)
    )

    # Set provider to 'stripe' only for paid subscriptions (pro/enterprise)
    # Leave free subscriptions with NULL provider
    op.execute("""
        UPDATE subscriptions
        SET provider = 'stripe'
        WHERE plan_type IN ('pro', 'enterprise')
        AND provider IS NULL
    """)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("subscriptions", "provider")
    # ### end Alembic commands ###
