"""Add campaign_modification_requests table.

Revision ID: b055
Revises: b054
Create Date: 2026-07-02
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

revision = "b055"
down_revision = "b054"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "campaign_modification_requests",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column(
            "campaign_id", postgresql.UUID(as_uuid=True),
            sa.ForeignKey("campaigns.id", ondelete="CASCADE"), nullable=False,
        ),
        sa.Column(
            "requested_by", postgresql.UUID(as_uuid=True),
            sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True,
        ),
        sa.Column("action", sa.String(), nullable=False),
        sa.Column(
            "test_id", postgresql.UUID(as_uuid=True),
            sa.ForeignKey("tests.id", ondelete="CASCADE"), nullable=False,
        ),
        sa.Column("order_index", sa.Integer(), nullable=True),
        sa.Column("phase", sa.String(), nullable=True),
        sa.Column("justification", sa.Text(), nullable=False),
        sa.Column("status", sa.String(), nullable=False, server_default="pending"),
        sa.Column(
            "reviewed_by", postgresql.UUID(as_uuid=True),
            sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True,
        ),
        sa.Column("reviewed_at", sa.DateTime(), nullable=True),
        sa.Column("review_notes", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
    )
    op.create_index(
        "ix_campaign_mod_requests_campaign",
        "campaign_modification_requests", ["campaign_id"],
    )
    op.create_index(
        "ix_campaign_mod_requests_status",
        "campaign_modification_requests", ["status"],
    )


def downgrade() -> None:
    op.drop_index("ix_campaign_mod_requests_status", table_name="campaign_modification_requests")
    op.drop_index("ix_campaign_mod_requests_campaign", table_name="campaign_modification_requests")
    op.drop_table("campaign_modification_requests")
