"""Add vendor subscriptions table

Revision ID: i7d8e9f0a1b2
Revises: h6c7d8e9f0a1
Create Date: 2025-12-24

This migration adds:
- vendor_subscriptions: Per-vendor subscription tracking with tier limits
"""

from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "i7d8e9f0a1b2"
down_revision: str | None = "h6c7d8e9f0a1"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # Create vendor_subscriptions table
    op.create_table(
        "vendor_subscriptions",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("vendor_id", sa.Integer(), nullable=False),
        # Tier and status
        sa.Column(
            "tier", sa.String(length=20), server_default="essential", nullable=False
        ),
        sa.Column(
            "status", sa.String(length=20), server_default="trial", nullable=False
        ),
        # Billing period
        sa.Column("period_start", sa.DateTime(timezone=True), nullable=False),
        sa.Column("period_end", sa.DateTime(timezone=True), nullable=False),
        sa.Column("is_annual", sa.Boolean(), server_default="0", nullable=False),
        # Trial
        sa.Column("trial_ends_at", sa.DateTime(timezone=True), nullable=True),
        # Usage counters
        sa.Column("orders_this_period", sa.Integer(), server_default="0", nullable=False),
        sa.Column("orders_limit_reached_at", sa.DateTime(timezone=True), nullable=True),
        # Custom overrides
        sa.Column("custom_orders_limit", sa.Integer(), nullable=True),
        sa.Column("custom_products_limit", sa.Integer(), nullable=True),
        sa.Column("custom_team_limit", sa.Integer(), nullable=True),
        # Payment (future Stripe integration)
        sa.Column("stripe_customer_id", sa.String(length=100), nullable=True),
        sa.Column("stripe_subscription_id", sa.String(length=100), nullable=True),
        # Cancellation
        sa.Column("cancelled_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("cancellation_reason", sa.Text(), nullable=True),
        # Timestamps
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["vendor_id"],
            ["vendors.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("vendor_id"),
    )
    op.create_index(
        op.f("ix_vendor_subscriptions_id"),
        "vendor_subscriptions",
        ["id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_vendor_subscriptions_vendor_id"),
        "vendor_subscriptions",
        ["vendor_id"],
        unique=True,
    )
    op.create_index(
        op.f("ix_vendor_subscriptions_tier"),
        "vendor_subscriptions",
        ["tier"],
        unique=False,
    )
    op.create_index(
        op.f("ix_vendor_subscriptions_status"),
        "vendor_subscriptions",
        ["status"],
        unique=False,
    )
    op.create_index(
        op.f("ix_vendor_subscriptions_stripe_customer_id"),
        "vendor_subscriptions",
        ["stripe_customer_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_vendor_subscriptions_stripe_subscription_id"),
        "vendor_subscriptions",
        ["stripe_subscription_id"],
        unique=False,
    )
    op.create_index(
        "idx_subscription_vendor_status",
        "vendor_subscriptions",
        ["vendor_id", "status"],
        unique=False,
    )
    op.create_index(
        "idx_subscription_period",
        "vendor_subscriptions",
        ["period_start", "period_end"],
        unique=False,
    )


def downgrade() -> None:
    op.drop_index("idx_subscription_period", table_name="vendor_subscriptions")
    op.drop_index("idx_subscription_vendor_status", table_name="vendor_subscriptions")
    op.drop_index(
        op.f("ix_vendor_subscriptions_stripe_subscription_id"),
        table_name="vendor_subscriptions",
    )
    op.drop_index(
        op.f("ix_vendor_subscriptions_stripe_customer_id"),
        table_name="vendor_subscriptions",
    )
    op.drop_index(
        op.f("ix_vendor_subscriptions_status"), table_name="vendor_subscriptions"
    )
    op.drop_index(
        op.f("ix_vendor_subscriptions_tier"), table_name="vendor_subscriptions"
    )
    op.drop_index(
        op.f("ix_vendor_subscriptions_vendor_id"), table_name="vendor_subscriptions"
    )
    op.drop_index(
        op.f("ix_vendor_subscriptions_id"), table_name="vendor_subscriptions"
    )
    op.drop_table("vendor_subscriptions")
