"""create scenario_metrics table

Revision ID: 5e247ed57b77
Revises: e46de4b0264a
Create Date: 2026-04-07 13:49:11.267167

"""

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 = "5e247ed57b77"
down_revision: Union[str, Sequence[str], None] = "e46de4b0264a"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    op.create_table(
        "scenario_metrics",
        sa.Column(
            "id",
            postgresql.UUID(as_uuid=True),
            primary_key=True,
            server_default=sa.text("uuid_generate_v4()"),
        ),
        sa.Column(
            "scenario_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("scenarios.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column(
            "timestamp",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("NOW()"),
            nullable=False,
        ),
        sa.Column(
            "metric_type", sa.String(50), nullable=False
        ),  # 'sqs', 'lambda', 'bedrock', 'safety'
        sa.Column("metric_name", sa.String(100), nullable=False),
        sa.Column(
            "value", sa.DECIMAL(15, 6), server_default="0.000000", nullable=False
        ),
        sa.Column(
            "unit", sa.String(20), nullable=False
        ),  # 'count', 'bytes', 'tokens', 'usd', 'invocations'
        sa.Column("extra_data", postgresql.JSONB(), server_default="{}"),
    )

    # Add indexes
    op.create_index("idx_metrics_scenario_id", "scenario_metrics", ["scenario_id"])
    op.create_index(
        "idx_metrics_timestamp",
        "scenario_metrics",
        ["timestamp"],
        postgresql_using="brin",
    )
    op.create_index("idx_metrics_type", "scenario_metrics", ["metric_type"])
    op.create_index(
        "idx_metrics_scenario_type", "scenario_metrics", ["scenario_id", "metric_type"]
    )


def downgrade() -> None:
    """Downgrade schema."""
    # Drop indexes
    op.drop_index("idx_metrics_scenario_type", table_name="scenario_metrics")
    op.drop_index("idx_metrics_type", table_name="scenario_metrics")
    op.drop_index("idx_metrics_timestamp", table_name="scenario_metrics")
    op.drop_index("idx_metrics_scenario_id", table_name="scenario_metrics")

    # Drop table
    op.drop_table("scenario_metrics")
