"""create reports table

Revision ID: e80c6eef58b2
Revises: 48f2231e7c12
Create Date: 2026-04-07 13:51:51.381906

"""

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 = "e80c6eef58b2"
down_revision: Union[str, Sequence[str], None] = "48f2231e7c12"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    op.create_table(
        "reports",
        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(
            "format", sa.Enum("pdf", "csv", name="report_format"), nullable=False
        ),
        sa.Column("file_path", sa.String(500), nullable=False),
        sa.Column("file_size_bytes", sa.Integer(), nullable=True),
        sa.Column(
            "generated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("NOW()"),
            nullable=False,
        ),
        sa.Column(
            "generated_by", sa.String(100), nullable=True
        ),  # user_id or api_key_id
        sa.Column("extra_data", postgresql.JSONB(), server_default="{}"),
        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,
        ),
    )

    # Add indexes
    op.create_index("idx_reports_scenario_id", "reports", ["scenario_id"])
    op.create_index(
        "idx_reports_generated_at", "reports", ["generated_at"], postgresql_using="brin"
    )


def downgrade() -> None:
    """Downgrade schema."""
    # Drop indexes
    op.drop_index("idx_reports_generated_at", table_name="reports")
    op.drop_index("idx_reports_scenario_id", table_name="reports")

    # Drop table
    op.drop_table("reports")

    # Drop enum type
    op.execute("DROP TYPE IF EXISTS report_format;")
