"""Add Meter

Revision ID: 83ad16407f2f
Revises: 8cbe62c5645b
Create Date: 2025-01-30 15:09:09.818919

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

import polar.meter.aggregation

# Polar Custom Imports
import polar.meter.filter

# revision identifiers, used by Alembic.
revision = "83ad16407f2f"
down_revision = "8cbe62c5645b"
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(
        "meters",
        sa.Column("name", sa.String(), nullable=False),
        sa.Column(
            "filter",
            polar.meter.filter.FilterType(astext_type=sa.Text()),
            nullable=False,
        ),
        sa.Column(
            "aggregation",
            polar.meter.aggregation.AggregationType(astext_type=sa.Text()),
            nullable=False,
        ),
        sa.Column("organization_id", sa.Uuid(), nullable=False),
        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(
            "user_metadata", postgresql.JSONB(astext_type=sa.Text()), nullable=False
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name=op.f("meters_organization_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("meters_pkey")),
    )
    op.create_index(
        op.f("ix_meters_created_at"), "meters", ["created_at"], unique=False
    )
    op.create_index(
        op.f("ix_meters_deleted_at"), "meters", ["deleted_at"], unique=False
    )
    op.create_index(
        op.f("ix_meters_modified_at"), "meters", ["modified_at"], unique=False
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_meters_modified_at"), table_name="meters")
    op.drop_index(op.f("ix_meters_deleted_at"), table_name="meters")
    op.drop_index(op.f("ix_meters_created_at"), table_name="meters")
    op.drop_table("meters")
    # ### end Alembic commands ###
