"""add legacy meter tables

Revision ID: a4v5w6x7y8z0
Revises: z3u4v5w6x7y8
Create Date: 2026-06-04

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

revision = "a4v5w6x7y8z0"
down_revision = "z3u4v5w6x7y8"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "legacy_interfaces",
        sa.Column("id", UUID(as_uuid=True), primary_key=True),
        sa.Column("interface_key", sa.String(length=300), nullable=False),
        sa.Column("interface_kind", sa.String(length=40), nullable=False),
        sa.Column("legacy_since", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("replacement_ref", sa.Text(), nullable=False),
        sa.Column("owner_component", sa.String(length=100), server_default="state-hub", nullable=False),
        sa.Column("status", sa.String(length=30), server_default="legacy", nullable=False),
        sa.Column("replacement_verified", sa.Boolean(), server_default=sa.text("false"), nullable=False),
        sa.Column("manual_hold", sa.Boolean(), server_default=sa.text("false"), nullable=False),
        sa.Column("hold_reason", sa.Text(), nullable=True),
        sa.Column("notes", sa.Text(), nullable=True),
        sa.Column("retired_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.UniqueConstraint("interface_key", name="uq_legacy_interfaces_interface_key"),
    )
    op.create_index("ix_legacy_interfaces_interface_key", "legacy_interfaces", ["interface_key"])
    op.create_index("ix_legacy_interfaces_interface_kind", "legacy_interfaces", ["interface_kind"])
    op.create_index("ix_legacy_interfaces_legacy_since", "legacy_interfaces", ["legacy_since"])
    op.create_index("ix_legacy_interfaces_owner_component", "legacy_interfaces", ["owner_component"])
    op.create_index("ix_legacy_interfaces_status", "legacy_interfaces", ["status"])

    op.create_table(
        "legacy_interface_usage_buckets",
        sa.Column("id", UUID(as_uuid=True), primary_key=True),
        sa.Column(
            "legacy_interface_id",
            UUID(as_uuid=True),
            sa.ForeignKey("legacy_interfaces.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("period_start", sa.Date(), nullable=False),
        sa.Column("bucket_kind", sa.String(length=30), nullable=False),
        sa.Column("bucket_key", sa.String(length=200), nullable=False),
        sa.Column("call_count", sa.Integer(), server_default="0", nullable=False),
        sa.Column("first_seen_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("last_seen_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.UniqueConstraint(
            "legacy_interface_id",
            "period_start",
            "bucket_kind",
            "bucket_key",
            name="uq_legacy_usage_bucket",
        ),
    )
    op.create_index(
        "ix_legacy_interface_usage_buckets_legacy_interface_id",
        "legacy_interface_usage_buckets",
        ["legacy_interface_id"],
    )
    op.create_index(
        "ix_legacy_interface_usage_buckets_period_start",
        "legacy_interface_usage_buckets",
        ["period_start"],
    )
    op.create_index(
        "ix_legacy_interface_usage_buckets_last_seen_at",
        "legacy_interface_usage_buckets",
        ["last_seen_at"],
    )
    op.create_index(
        "ix_legacy_usage_interface_period",
        "legacy_interface_usage_buckets",
        ["legacy_interface_id", "period_start"],
    )
    op.create_index(
        "ix_legacy_usage_bucket_kind_key",
        "legacy_interface_usage_buckets",
        ["bucket_kind", "bucket_key"],
    )


def downgrade() -> None:
    op.drop_index("ix_legacy_usage_bucket_kind_key", table_name="legacy_interface_usage_buckets")
    op.drop_index("ix_legacy_usage_interface_period", table_name="legacy_interface_usage_buckets")
    op.drop_index("ix_legacy_interface_usage_buckets_last_seen_at", table_name="legacy_interface_usage_buckets")
    op.drop_index("ix_legacy_interface_usage_buckets_period_start", table_name="legacy_interface_usage_buckets")
    op.drop_index("ix_legacy_interface_usage_buckets_legacy_interface_id", table_name="legacy_interface_usage_buckets")
    op.drop_table("legacy_interface_usage_buckets")
    op.drop_index("ix_legacy_interfaces_status", table_name="legacy_interfaces")
    op.drop_index("ix_legacy_interfaces_owner_component", table_name="legacy_interfaces")
    op.drop_index("ix_legacy_interfaces_legacy_since", table_name="legacy_interfaces")
    op.drop_index("ix_legacy_interfaces_interface_kind", table_name="legacy_interfaces")
    op.drop_index("ix_legacy_interfaces_interface_key", table_name="legacy_interfaces")
    op.drop_table("legacy_interfaces")
