"""add generic resource ownership transfer state

Revision ID: d03a7b9c1e5f
Revises: c91f0a72be34
Create Date: 2026-07-30 00:00:00.000000
"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa


revision = "d03a7b9c1e5f"
down_revision = "c91f0a72be34"
branch_labels = None
depends_on = None


def upgrade() -> None:
    inspector = sa.inspect(op.get_bind())
    if "core_ownership_transfers" in inspector.get_table_names():
        return
    op.create_table(
        "core_ownership_transfers",
        sa.Column("id", sa.String(length=36), nullable=False),
        sa.Column("tenant_id", sa.String(length=36), nullable=False),
        sa.Column("resource_module", sa.String(length=100), nullable=False),
        sa.Column("resource_type", sa.String(length=100), nullable=False),
        sa.Column("resource_id", sa.String(length=255), nullable=False),
        sa.Column("kind", sa.String(length=40), nullable=False),
        sa.Column("status", sa.String(length=50), nullable=False),
        sa.Column("current_owner_type", sa.String(length=40), nullable=False),
        sa.Column("current_owner_id", sa.String(length=255), nullable=False),
        sa.Column("target_owner_type", sa.String(length=40), nullable=False),
        sa.Column("target_owner_id", sa.String(length=255), nullable=False),
        sa.Column("initiated_by_type", sa.String(length=40), nullable=False),
        sa.Column("initiated_by_id", sa.String(length=255), nullable=False),
        sa.Column("owner_approved_by_type", sa.String(length=40), nullable=True),
        sa.Column("owner_approved_by_id", sa.String(length=255), nullable=True),
        sa.Column("target_accepted_by_type", sa.String(length=40), nullable=True),
        sa.Column("target_accepted_by_id", sa.String(length=255), nullable=True),
        sa.Column("reason", sa.Text(), nullable=True),
        sa.Column("assurance_profile", sa.String(length=80), nullable=True),
        sa.Column("required_approvals", sa.Integer(), nullable=False),
        sa.Column("approvals", sa.JSON(), nullable=False),
        sa.Column("decisions", sa.JSON(), nullable=False),
        sa.Column("idempotency_key", sa.String(length=200), nullable=False),
        sa.Column("canonical_request_hash", sa.String(length=64), nullable=False),
        sa.Column("expires_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("execute_after", sa.DateTime(timezone=True), nullable=True),
        sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("declined_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("cancelled_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("expired_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("revision", sa.Integer(), nullable=False),
        sa.Column("metadata", sa.JSON(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_core_ownership_transfers")),
        sa.UniqueConstraint(
            "tenant_id",
            "resource_module",
            "idempotency_key",
            name="uq_core_ownership_transfer_idempotency",
        ),
    )
    op.create_index(
        op.f("ix_core_ownership_transfers_tenant_id"),
        "core_ownership_transfers",
        ["tenant_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_core_ownership_transfers_kind"),
        "core_ownership_transfers",
        ["kind"],
        unique=False,
    )
    op.create_index(
        op.f("ix_core_ownership_transfers_status"),
        "core_ownership_transfers",
        ["status"],
        unique=False,
    )
    op.create_index(
        "ix_core_ownership_transfer_resource",
        "core_ownership_transfers",
        [
            "tenant_id",
            "resource_module",
            "resource_type",
            "resource_id",
            "status",
        ],
        unique=False,
    )
    op.create_index(
        "ix_core_ownership_transfer_expiry",
        "core_ownership_transfers",
        ["status", "expires_at"],
        unique=False,
    )


def downgrade() -> None:
    inspector = sa.inspect(op.get_bind())
    if "core_ownership_transfers" not in inspector.get_table_names():
        return
    op.drop_index(
        "ix_core_ownership_transfer_expiry",
        table_name="core_ownership_transfers",
    )
    op.drop_index(
        "ix_core_ownership_transfer_resource",
        table_name="core_ownership_transfers",
    )
    op.drop_index(
        op.f("ix_core_ownership_transfers_status"),
        table_name="core_ownership_transfers",
    )
    op.drop_index(
        op.f("ix_core_ownership_transfers_kind"),
        table_name="core_ownership_transfers",
    )
    op.drop_index(
        op.f("ix_core_ownership_transfers_tenant_id"),
        table_name="core_ownership_transfers",
    )
    op.drop_table("core_ownership_transfers")
