"""create scenario_logs table

Revision ID: e46de4b0264a
Revises: 8c29fdcbbf85
Create Date: 2026-04-07 13:48:26.383709

"""

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 = "e46de4b0264a"
down_revision: Union[str, Sequence[str], None] = "8c29fdcbbf85"
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_logs",
        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(
            "received_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("NOW()"),
            nullable=False,
        ),
        sa.Column("message_hash", sa.String(64), nullable=False),  # SHA256
        sa.Column("message_preview", sa.String(500), nullable=True),
        sa.Column("source", sa.String(100), server_default="unknown", nullable=False),
        sa.Column("size_bytes", sa.Integer(), server_default="0", nullable=False),
        sa.Column("has_pii", sa.Boolean(), server_default="false", nullable=False),
        sa.Column("token_count", sa.Integer(), server_default="0", nullable=False),
        sa.Column("sqs_blocks", sa.Integer(), server_default="1", nullable=False),
    )

    # Add constraints
    op.create_check_constraint(
        "chk_size_positive", "scenario_logs", sa.column("size_bytes") >= 0
    )
    op.create_check_constraint(
        "chk_token_positive", "scenario_logs", sa.column("token_count") >= 0
    )
    op.create_check_constraint(
        "chk_blocks_positive", "scenario_logs", sa.column("sqs_blocks") >= 1
    )

    # Add indexes
    op.create_index("idx_logs_scenario_id", "scenario_logs", ["scenario_id"])
    op.create_index(
        "idx_logs_received_at",
        "scenario_logs",
        ["received_at"],
        postgresql_using="brin",
    )
    op.create_index("idx_logs_message_hash", "scenario_logs", ["message_hash"])
    op.create_index(
        "idx_logs_has_pii",
        "scenario_logs",
        ["has_pii"],
        postgresql_where=sa.text("has_pii = true"),
    )


def downgrade() -> None:
    """Downgrade schema."""
    # Drop indexes
    op.drop_index("idx_logs_has_pii", table_name="scenario_logs")
    op.drop_index("idx_logs_message_hash", table_name="scenario_logs")
    op.drop_index("idx_logs_received_at", table_name="scenario_logs")
    op.drop_index("idx_logs_scenario_id", table_name="scenario_logs")

    # Drop table
    op.drop_table("scenario_logs")
