"""add core change sequence

Revision ID: 3f4a5b6c7d8e
Revises: 2e3f4a5b6c7d
Create Date: 2026-07-09 00:00:00.000000
"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa


revision = "3f4a5b6c7d8e"
down_revision = "2e3f4a5b6c7d"
branch_labels = None
depends_on = None


def upgrade() -> None:
    inspector = sa.inspect(op.get_bind())
    if "core_change_sequence" not in inspector.get_table_names():
        op.create_table(
            "core_change_sequence",
            sa.Column("id", sa.BigInteger().with_variant(sa.Integer(), "sqlite"), autoincrement=True, nullable=False),
            sa.Column("tenant_id", sa.String(length=36), nullable=True),
            sa.Column("module_id", sa.String(length=100), nullable=False),
            sa.Column("collection", sa.String(length=150), nullable=False),
            sa.Column("resource_type", sa.String(length=100), nullable=False),
            sa.Column("resource_id", sa.String(length=255), nullable=False),
            sa.Column("operation", sa.String(length=30), nullable=False),
            sa.Column("actor_type", sa.String(length=30), nullable=True),
            sa.Column("actor_id", sa.String(length=255), nullable=True),
            sa.Column("payload", sa.JSON(), nullable=False),
            sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
            sa.PrimaryKeyConstraint("id", name=op.f("pk_core_change_sequence")),
        )

    inspector = sa.inspect(op.get_bind())
    indexes = {item["name"] for item in inspector.get_indexes("core_change_sequence")}
    for column in (
        "tenant_id",
        "module_id",
        "collection",
        "resource_type",
        "resource_id",
        "operation",
        "actor_type",
        "actor_id",
        "created_at",
    ):
        name = op.f(f"ix_core_change_sequence_{column}")
        if name not in indexes:
            op.create_index(name, "core_change_sequence", [column], unique=False)
    for name, columns in (
        ("ix_core_change_sequence_tenant_module_id", ["tenant_id", "module_id", "id"]),
        ("ix_core_change_sequence_collection_id", ["collection", "id"]),
        ("ix_core_change_sequence_resource", ["module_id", "resource_type", "resource_id"]),
    ):
        if name not in indexes:
            op.create_index(name, "core_change_sequence", columns, unique=False)

    inspector = sa.inspect(op.get_bind())
    if "core_change_sequence_retention_floor" not in inspector.get_table_names():
        op.create_table(
            "core_change_sequence_retention_floor",
            sa.Column("id", sa.BigInteger().with_variant(sa.Integer(), "sqlite"), autoincrement=True, nullable=False),
            sa.Column("tenant_key", sa.String(length=36), nullable=False),
            sa.Column("module_id", sa.String(length=100), nullable=False),
            sa.Column("collection", sa.String(length=150), nullable=False),
            sa.Column("min_valid_sequence", sa.BigInteger().with_variant(sa.Integer(), "sqlite"), nullable=False),
            sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
            sa.PrimaryKeyConstraint("id", name=op.f("pk_core_change_sequence_retention_floor")),
            sa.UniqueConstraint("tenant_key", "module_id", "collection", name="uq_core_change_sequence_retention_scope"),
        )
    indexes = {item["name"] for item in inspector.get_indexes("core_change_sequence_retention_floor")}
    if "ix_core_change_sequence_retention_scope" not in indexes:
        op.create_index(
            "ix_core_change_sequence_retention_scope",
            "core_change_sequence_retention_floor",
            ["tenant_key", "module_id", "collection"],
            unique=False,
        )


def downgrade() -> None:
    inspector = sa.inspect(op.get_bind())
    if "core_change_sequence_retention_floor" in inspector.get_table_names():
        indexes = {item["name"] for item in inspector.get_indexes("core_change_sequence_retention_floor")}
        if "ix_core_change_sequence_retention_scope" in indexes:
            op.drop_index("ix_core_change_sequence_retention_scope", table_name="core_change_sequence_retention_floor")
        op.drop_table("core_change_sequence_retention_floor")
    if "core_change_sequence" not in inspector.get_table_names():
        return
    indexes = {item["name"] for item in inspector.get_indexes("core_change_sequence")}
    for name in (
        "ix_core_change_sequence_resource",
        "ix_core_change_sequence_collection_id",
        "ix_core_change_sequence_tenant_module_id",
        op.f("ix_core_change_sequence_created_at"),
        op.f("ix_core_change_sequence_actor_id"),
        op.f("ix_core_change_sequence_actor_type"),
        op.f("ix_core_change_sequence_operation"),
        op.f("ix_core_change_sequence_resource_id"),
        op.f("ix_core_change_sequence_resource_type"),
        op.f("ix_core_change_sequence_collection"),
        op.f("ix_core_change_sequence_module_id"),
        op.f("ix_core_change_sequence_tenant_id"),
    ):
        if name in indexes:
            op.drop_index(name, table_name="core_change_sequence")
    op.drop_table("core_change_sequence")
