"""explicit temporary and permanent user locks

Revision ID: 5f6a7b8c9d0e
Revises: 4e5f6a7b8c9d
Create Date: 2026-06-13 18:00:00.000000
"""
from __future__ import annotations

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

revision: str = "5f6a7b8c9d0e"
down_revision: Union[str, None] = "4e5f6a7b8c9d"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    with op.batch_alter_table("campaign_versions") as batch_op:
        batch_op.add_column(sa.Column("user_lock_state", sa.String(length=20), nullable=True))
        batch_op.add_column(sa.Column("user_locked_at", sa.DateTime(timezone=True), nullable=True))
        batch_op.add_column(sa.Column("user_locked_by_user_id", sa.String(length=36), nullable=True))
        batch_op.create_foreign_key(
            "fk_campaign_versions_user_locked_by_user_id_users",
            "access_users",
            ["user_locked_by_user_id"],
            ["id"],
            ondelete="SET NULL",
        )
        batch_op.create_index("ix_campaign_versions_user_lock_state", ["user_lock_state"])
        batch_op.create_index("ix_campaign_versions_user_locked_by_user_id", ["user_locked_by_user_id"])

    # Existing published snapshots were the former irreversible user lock.
    op.execute(
        """
        UPDATE campaign_versions
        SET user_lock_state = 'permanent',
            user_locked_at = published_at,
            user_locked_by_user_id = NULL
        WHERE published_at IS NOT NULL
          AND user_lock_state IS NULL
        """
    )


def downgrade() -> None:
    with op.batch_alter_table("campaign_versions") as batch_op:
        batch_op.drop_index("ix_campaign_versions_user_locked_by_user_id")
        batch_op.drop_index("ix_campaign_versions_user_lock_state")
        batch_op.drop_constraint("fk_campaign_versions_user_locked_by_user_id_users", type_="foreignkey")
        batch_op.drop_column("user_locked_by_user_id")
        batch_op.drop_column("user_locked_at")
        batch_op.drop_column("user_lock_state")
