"""Add CustomerMeter

Revision ID: 5cc3e60ba6a1
Revises: 07905d0ea9a7
Create Date: 2025-04-03 13:41:59.143513

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "5cc3e60ba6a1"
down_revision = "07905d0ea9a7"
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(
        "customer_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("customer_id", sa.Uuid(), nullable=False),
        sa.Column("meter_id", sa.Uuid(), nullable=False),
        sa.Column("last_balanced_event_id", sa.Uuid(), nullable=True),
        sa.Column("units_balance", sa.Numeric(), nullable=False),
        sa.ForeignKeyConstraint(
            ["customer_id"],
            ["customers.id"],
            name=op.f("customer_meters_customer_id_fkey"),
            ondelete="cascade",
        ),
        sa.ForeignKeyConstraint(
            ["last_balanced_event_id"],
            ["events.id"],
            name=op.f("customer_meters_last_balanced_event_id_fkey"),
        ),
        sa.ForeignKeyConstraint(
            ["meter_id"],
            ["meters.id"],
            name=op.f("customer_meters_meter_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("customer_meters_pkey")),
        sa.UniqueConstraint(
            "customer_id",
            "meter_id",
            name=op.f("customer_meters_customer_id_meter_id_key"),
        ),
    )
    op.create_index(
        op.f("ix_customer_meters_created_at"),
        "customer_meters",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_customer_meters_deleted_at"),
        "customer_meters",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_customer_meters_last_balanced_event_id"),
        "customer_meters",
        ["last_balanced_event_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_customer_meters_meter_id"),
        "customer_meters",
        ["meter_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_customer_meters_modified_at"),
        "customer_meters",
        ["modified_at"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_customer_meters_modified_at"), table_name="customer_meters")
    op.drop_index(op.f("ix_customer_meters_meter_id"), table_name="customer_meters")
    op.drop_index(
        op.f("ix_customer_meters_last_balanced_event_id"), table_name="customer_meters"
    )
    op.drop_index(op.f("ix_customer_meters_deleted_at"), table_name="customer_meters")
    op.drop_index(op.f("ix_customer_meters_created_at"), table_name="customer_meters")
    op.drop_table("customer_meters")
    # ### end Alembic commands ###
