"""Add BillingEntry and ProductPriceMeteredUnit

Revision ID: d62a0050666e
Revises: cbbed7381d63
Create Date: 2025-03-24 15:47:31.910180

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

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


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "billing_entry",
        sa.Column("start_timestamp", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("end_timestamp", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("direction", sa.String(), nullable=False),
        sa.Column("customer_id", sa.Uuid(), nullable=False),
        sa.Column("product_price_id", sa.Uuid(), nullable=False),
        sa.Column("subscription_id", sa.Uuid(), nullable=True),
        sa.Column("event_id", sa.Uuid(), nullable=False),
        sa.Column("order_item_id", sa.Uuid(), nullable=True),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["customer_id"],
            ["customers.id"],
            name=op.f("billing_entry_customer_id_fkey"),
            ondelete="cascade",
        ),
        sa.ForeignKeyConstraint(
            ["event_id"],
            ["events.id"],
            name=op.f("billing_entry_event_id_fkey"),
            ondelete="cascade",
        ),
        sa.ForeignKeyConstraint(
            ["order_item_id"],
            ["order_items.id"],
            name=op.f("billing_entry_order_item_id_fkey"),
            ondelete="cascade",
        ),
        sa.ForeignKeyConstraint(
            ["product_price_id"],
            ["product_prices.id"],
            name=op.f("billing_entry_product_price_id_fkey"),
            ondelete="cascade",
        ),
        sa.ForeignKeyConstraint(
            ["subscription_id"],
            ["subscriptions.id"],
            name=op.f("billing_entry_subscription_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("billing_entry_pkey")),
    )
    op.create_index(
        op.f("ix_billing_entry_created_at"),
        "billing_entry",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_billing_entry_deleted_at"),
        "billing_entry",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_billing_entry_end_timestamp"),
        "billing_entry",
        ["end_timestamp"],
        unique=False,
    )
    op.create_index(
        op.f("ix_billing_entry_modified_at"),
        "billing_entry",
        ["modified_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_billing_entry_start_timestamp"),
        "billing_entry",
        ["start_timestamp"],
        unique=False,
    )
    op.add_column("meters", sa.Column("last_billed_event_id", sa.Uuid(), nullable=True))
    op.create_index(
        op.f("ix_meters_last_billed_event_id"),
        "meters",
        ["last_billed_event_id"],
        unique=False,
    )
    op.create_foreign_key(
        op.f("meters_last_billed_event_id_fkey"),
        "meters",
        "events",
        ["last_billed_event_id"],
        ["id"],
    )
    op.add_column(
        "product_prices", sa.Column("unit_amount", sa.Integer(), nullable=True)
    )
    op.add_column(
        "product_prices", sa.Column("included_units", sa.Integer(), nullable=True)
    )
    op.add_column(
        "product_prices", sa.Column("cap_amount", sa.Integer(), nullable=True)
    )
    op.add_column("product_prices", sa.Column("meter_id", sa.Uuid(), nullable=True))
    op.alter_column(
        "product_prices", "stripe_price_id", existing_type=sa.VARCHAR(), nullable=True
    )
    op.create_index(
        op.f("ix_product_prices_meter_id"), "product_prices", ["meter_id"], unique=False
    )
    op.create_foreign_key(
        op.f("product_prices_meter_id_fkey"),
        "product_prices",
        "meters",
        ["meter_id"],
        ["id"],
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(
        op.f("product_prices_meter_id_fkey"), "product_prices", type_="foreignkey"
    )
    op.drop_index(op.f("ix_product_prices_meter_id"), table_name="product_prices")
    op.alter_column(
        "product_prices", "stripe_price_id", existing_type=sa.VARCHAR(), nullable=False
    )
    op.drop_column("product_prices", "meter_id")
    op.drop_column("product_prices", "cap_amount")
    op.drop_column("product_prices", "included_units")
    op.drop_column("product_prices", "unit_amount")
    op.drop_constraint(
        op.f("meters_last_billed_event_id_fkey"), "meters", type_="foreignkey"
    )
    op.drop_index(op.f("ix_meters_last_billed_event_id"), table_name="meters")
    op.drop_column("meters", "last_billed_event_id")
    op.drop_index(op.f("ix_billing_entry_start_timestamp"), table_name="billing_entry")
    op.drop_index(op.f("ix_billing_entry_modified_at"), table_name="billing_entry")
    op.drop_index(op.f("ix_billing_entry_end_timestamp"), table_name="billing_entry")
    op.drop_index(op.f("ix_billing_entry_deleted_at"), table_name="billing_entry")
    op.drop_index(op.f("ix_billing_entry_created_at"), table_name="billing_entry")
    op.drop_table("billing_entry")
    # ### end Alembic commands ###
