"""Add recurring_interval_count to Product and Subscription models

Revision ID: bbdc673dc236
Revises: 3b951bebf866
Create Date: 2025-09-29 11:46:22.707113

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "bbdc673dc236"
down_revision = "3b951bebf866"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "products",
        sa.Column("recurring_interval_count", sa.Integer(), nullable=True),
    )
    op.add_column(
        "subscriptions",
        sa.Column("recurring_interval_count", sa.Integer(), nullable=True),
    )

    # Set recurring_interval_count = 1 for all existing subscriptions
    op.execute("UPDATE subscriptions SET recurring_interval_count = 1")
    op.execute(
        "UPDATE products SET recurring_interval_count = 1 WHERE recurring_interval IS NOT NULL"
    )

    # Make subscriptions.recurring_interval_count NOT NULL
    op.alter_column(
        "subscriptions",
        "recurring_interval_count",
        nullable=False,
    )
    # ### end Alembic commands ###


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