"""Add VPS staging cleanup audit fields to document storage jobs.

Revision ID: 20260720_document_vps_auto_cleanup
Revises: 20260720_phase5c_workflow_escalations
"""
from alembic import op
import sqlalchemy as sa

revision = "20260720_document_vps_auto_cleanup"
down_revision = "20260720_phase5c_workflow_escalations"
branch_labels = None
depends_on = None


def _add_cleanup_columns(table_name: str) -> None:
    op.add_column(table_name, sa.Column("vps_cleanup_status", sa.String(length=30), nullable=False, server_default="pending"))
    op.add_column(table_name, sa.Column("vps_cleanup_attempts", sa.Integer(), nullable=False, server_default="0"))
    op.add_column(table_name, sa.Column("vps_cleanup_error", sa.Text(), nullable=True))
    op.add_column(table_name, sa.Column("vps_deleted_at_utc", sa.DateTime(timezone=True), nullable=True))
    op.add_column(table_name, sa.Column("vps_deleted_bytes", sa.Integer(), nullable=True))
    op.create_index(f"ix_{table_name}_vps_cleanup_status", table_name, ["vps_cleanup_status"], unique=False)
    op.create_index(f"ix_{table_name}_vps_deleted_at_utc", table_name, ["vps_deleted_at_utc"], unique=False)


def _drop_cleanup_columns(table_name: str) -> None:
    op.drop_index(f"ix_{table_name}_vps_deleted_at_utc", table_name=table_name)
    op.drop_index(f"ix_{table_name}_vps_cleanup_status", table_name=table_name)
    op.drop_column(table_name, "vps_deleted_bytes")
    op.drop_column(table_name, "vps_deleted_at_utc")
    op.drop_column(table_name, "vps_cleanup_error")
    op.drop_column(table_name, "vps_cleanup_attempts")
    op.drop_column(table_name, "vps_cleanup_status")


def upgrade() -> None:
    _add_cleanup_columns("document_storage_jobs")
    _add_cleanup_columns("permanent_document_storage_jobs")


def downgrade() -> None:
    _drop_cleanup_columns("permanent_document_storage_jobs")
    _drop_cleanup_columns("document_storage_jobs")
