"""Safe delivery lifecycle and immutable execution snapshot.

Revision ID: 7b8c9d0e1f2a
Revises: 6a7b8c9d0e1f
Create Date: 2026-06-14 12:00:00.000000
"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa

revision = "7b8c9d0e1f2a"
down_revision = "6a7b8c9d0e1f"
branch_labels = None
depends_on = None


def upgrade() -> None:
    with op.batch_alter_table("campaign_versions") as batch_op:
        batch_op.add_column(sa.Column("execution_snapshot", sa.JSON(), nullable=True))
        batch_op.add_column(sa.Column("execution_snapshot_hash", sa.String(length=64), nullable=True))
        batch_op.add_column(sa.Column("execution_snapshot_at", sa.DateTime(timezone=True), nullable=True))
        batch_op.create_index("ix_campaign_versions_execution_snapshot_hash", ["execution_snapshot_hash"], unique=False)

    with op.batch_alter_table("campaign_jobs") as batch_op:
        batch_op.add_column(sa.Column("claimed_at", sa.DateTime(timezone=True), nullable=True))
        batch_op.add_column(sa.Column("claim_token", sa.String(length=36), nullable=True))
        batch_op.add_column(sa.Column("smtp_started_at", sa.DateTime(timezone=True), nullable=True))
        batch_op.add_column(sa.Column("outcome_unknown_at", sa.DateTime(timezone=True), nullable=True))
        batch_op.add_column(sa.Column("eml_sha256", sa.String(length=64), nullable=True))
        batch_op.create_index("ix_campaign_jobs_claim_token", ["claim_token"], unique=False)
        batch_op.create_index("ix_campaign_jobs_eml_sha256", ["eml_sha256"], unique=False)

    with op.batch_alter_table("send_attempts") as batch_op:
        batch_op.add_column(sa.Column("status", sa.String(length=50), nullable=False, server_default="started"))
        batch_op.add_column(sa.Column("claim_token", sa.String(length=36), nullable=True))
        batch_op.create_index("ix_send_attempts_status", ["status"], unique=False)
        batch_op.create_index("ix_send_attempts_claim_token", ["claim_token"], unique=False)

    # Existing successful rows remain readable through the legacy 'sent' value.
    # No data rewrite is required; new deliveries use 'smtp_accepted'.


def downgrade() -> None:
    with op.batch_alter_table("send_attempts") as batch_op:
        batch_op.drop_index("ix_send_attempts_claim_token")
        batch_op.drop_index("ix_send_attempts_status")
        batch_op.drop_column("claim_token")
        batch_op.drop_column("status")

    with op.batch_alter_table("campaign_jobs") as batch_op:
        batch_op.drop_index("ix_campaign_jobs_eml_sha256")
        batch_op.drop_index("ix_campaign_jobs_claim_token")
        batch_op.drop_column("eml_sha256")
        batch_op.drop_column("outcome_unknown_at")
        batch_op.drop_column("smtp_started_at")
        batch_op.drop_column("claim_token")
        batch_op.drop_column("claimed_at")

    with op.batch_alter_table("campaign_versions") as batch_op:
        batch_op.drop_index("ix_campaign_versions_execution_snapshot_hash")
        batch_op.drop_column("execution_snapshot_at")
        batch_op.drop_column("execution_snapshot_hash")
        batch_op.drop_column("execution_snapshot")
