"""create aws_pricing table

Revision ID: 48f2231e7c12
Revises: 5e247ed57b77
Create Date: 2026-04-07 13:50:15.040833

"""

from typing import Sequence, Union

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


# revision identifiers, used by Alembic.
revision: str = "48f2231e7c12"
down_revision: Union[str, Sequence[str], None] = "5e247ed57b77"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    op.create_table(
        "aws_pricing",
        sa.Column(
            "id",
            postgresql.UUID(as_uuid=True),
            primary_key=True,
            server_default=sa.text("uuid_generate_v4()"),
        ),
        sa.Column(
            "service", sa.String(50), nullable=False
        ),  # 'sqs', 'lambda', 'bedrock'
        sa.Column("region", sa.String(50), nullable=False),
        sa.Column("tier", sa.String(50), server_default="standard", nullable=False),
        sa.Column("price_per_unit", sa.DECIMAL(15, 10), nullable=False),
        sa.Column(
            "unit", sa.String(20), nullable=False
        ),  # 'per_million_requests', 'per_gb_second', 'per_1k_tokens'
        sa.Column(
            "effective_from",
            sa.Date(),
            server_default=sa.text("CURRENT_DATE"),
            nullable=False,
        ),
        sa.Column("effective_to", sa.Date(), nullable=True),
        sa.Column("is_active", sa.Boolean(), server_default="true", nullable=False),
        sa.Column("source_url", sa.String(500), nullable=True),
        sa.Column("description", sa.Text(), nullable=True),
    )

    # Add constraints
    op.create_check_constraint(
        "chk_price_positive", "aws_pricing", sa.column("price_per_unit") >= 0
    )

    # Add indexes
    op.create_index("idx_pricing_service", "aws_pricing", ["service"])
    op.create_index("idx_pricing_region", "aws_pricing", ["region"])
    op.create_index(
        "idx_pricing_active",
        "aws_pricing",
        ["service", "region", "tier"],
        postgresql_where=sa.text("is_active = true"),
    )


def downgrade() -> None:
    """Downgrade schema."""
    # Drop indexes
    op.drop_index("idx_pricing_active", table_name="aws_pricing")
    op.drop_index("idx_pricing_region", table_name="aws_pricing")
    op.drop_index("idx_pricing_service", table_name="aws_pricing")

    # Drop table
    op.drop_table("aws_pricing")
