"""Add SubscriptionMeter

Revision ID: 871bf4f37db0
Revises: 5496aa5b12ad
Create Date: 2025-04-15 15:47:50.672894

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "871bf4f37db0"
down_revision = "5496aa5b12ad"
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(
        "subscription_meters",
        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.Column("subscription_id", sa.Uuid(), nullable=False),
        sa.Column("meter_id", sa.Uuid(), nullable=False),
        sa.Column("consumed_units", sa.Numeric(), nullable=False),
        sa.Column("credited_units", sa.Integer(), nullable=False),
        sa.Column("amount", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["meter_id"],
            ["meters.id"],
            name=op.f("subscription_meters_meter_id_fkey"),
            ondelete="cascade",
        ),
        sa.ForeignKeyConstraint(
            ["subscription_id"],
            ["subscriptions.id"],
            name=op.f("subscription_meters_subscription_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("subscription_meters_pkey")),
    )
    op.create_index(
        op.f("ix_subscription_meters_created_at"),
        "subscription_meters",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_subscription_meters_deleted_at"),
        "subscription_meters",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_subscription_meters_modified_at"),
        "subscription_meters",
        ["modified_at"],
        unique=False,
    )

    op.execute(
        """
        INSERT INTO subscription_meters (id, created_at, modified_at, consumed_units, credited_units, amount, subscription_id, meter_id)
        SELECT subscription_product_prices.id, subscription_product_prices.created_at, subscription_product_prices.modified_at, 0, 0, 0, subscription_product_prices.subscription_id, product_prices.meter_id
        FROM subscription_product_prices
        JOIN product_prices ON subscription_product_prices.product_price_id = product_prices.id
        WHERE product_prices.amount_type = 'metered_unit'
        """
    )

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_subscription_meters_modified_at"), table_name="subscription_meters"
    )
    op.drop_index(
        op.f("ix_subscription_meters_deleted_at"), table_name="subscription_meters"
    )
    op.drop_index(
        op.f("ix_subscription_meters_created_at"), table_name="subscription_meters"
    )
    op.drop_table("subscription_meters")
    # ### end Alembic commands ###
