"""Add manual attribute lock tables for taxonomy/collection-scoped import overrides.

Revision ID: 0062_add_manual_attribute_lock_tables
Revises: 0061_add_manual_taxonomy_assignment_tables
Create Date: 2026-07-28 20:10:00.000000
"""

from __future__ import annotations

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

revision = "0062_add_manual_attribute_lock_tables"
down_revision = "0061_add_manual_taxonomy_assignment_tables"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "manual_attribute_lock_rule",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("canonical_taxonomy_path_slug", sa.String(length=512), nullable=False),
        sa.Column("path_match_kind", sa.String(length=32), nullable=False, server_default="exact"),
        sa.Column("collection_registry_id", sa.Integer(), sa.ForeignKey("master_collection_registry.id"), nullable=True),
        sa.Column("collection_code", sa.String(length=128), nullable=True),
        sa.Column("collection_name", sa.Text(), nullable=True),
        sa.Column("priority", sa.Integer(), nullable=False, server_default="100"),
        sa.Column("rule_status", sa.String(length=32), nullable=False, server_default="active"),
        sa.Column("raw_payload", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("notes", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.UniqueConstraint(
            "canonical_taxonomy_path_slug",
            "path_match_kind",
            "collection_code",
            name="uq_manual_attribute_lock_rule_scope",
        ),
    )
    op.create_index(
        "ix_manual_attribute_lock_rule_canonical_taxonomy_path_slug",
        "manual_attribute_lock_rule",
        ["canonical_taxonomy_path_slug"],
    )
    op.create_index(
        "ix_manual_attribute_lock_rule_path_match_kind",
        "manual_attribute_lock_rule",
        ["path_match_kind"],
    )
    op.create_index(
        "ix_manual_attribute_lock_rule_collection_registry_id",
        "manual_attribute_lock_rule",
        ["collection_registry_id"],
    )
    op.create_index(
        "ix_manual_attribute_lock_rule_collection_code",
        "manual_attribute_lock_rule",
        ["collection_code"],
    )
    op.create_index(
        "ix_manual_attribute_lock_rule_priority",
        "manual_attribute_lock_rule",
        ["priority"],
    )
    op.create_index(
        "ix_manual_attribute_lock_rule_rule_status",
        "manual_attribute_lock_rule",
        ["rule_status"],
    )

    op.create_table(
        "manual_attribute_lock_target",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("rule_id", sa.Integer(), sa.ForeignKey("manual_attribute_lock_rule.id"), nullable=False),
        sa.Column("attribute_code", sa.String(length=128), nullable=False),
        sa.Column("assignment_mode", sa.String(length=32), nullable=False, server_default="force"),
        sa.Column("attribute_value", sa.Text(), nullable=True),
        sa.Column("attribute_values_json", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("is_locked", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("sort_order", sa.Integer(), nullable=False, server_default="100"),
        sa.Column("raw_payload", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("notes", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.UniqueConstraint("rule_id", "attribute_code", name="uq_manual_attribute_lock_target_code"),
    )
    op.create_index(
        "ix_manual_attribute_lock_target_rule_id",
        "manual_attribute_lock_target",
        ["rule_id"],
    )
    op.create_index(
        "ix_manual_attribute_lock_target_attribute_code",
        "manual_attribute_lock_target",
        ["attribute_code"],
    )
    op.create_index(
        "ix_manual_attribute_lock_target_assignment_mode",
        "manual_attribute_lock_target",
        ["assignment_mode"],
    )


def downgrade() -> None:
    op.drop_index("ix_manual_attribute_lock_target_assignment_mode", table_name="manual_attribute_lock_target")
    op.drop_index("ix_manual_attribute_lock_target_attribute_code", table_name="manual_attribute_lock_target")
    op.drop_index("ix_manual_attribute_lock_target_rule_id", table_name="manual_attribute_lock_target")
    op.drop_table("manual_attribute_lock_target")

    op.drop_index("ix_manual_attribute_lock_rule_rule_status", table_name="manual_attribute_lock_rule")
    op.drop_index("ix_manual_attribute_lock_rule_priority", table_name="manual_attribute_lock_rule")
    op.drop_index("ix_manual_attribute_lock_rule_collection_code", table_name="manual_attribute_lock_rule")
    op.drop_index(
        "ix_manual_attribute_lock_rule_collection_registry_id",
        table_name="manual_attribute_lock_rule",
    )
    op.drop_index("ix_manual_attribute_lock_rule_path_match_kind", table_name="manual_attribute_lock_rule")
    op.drop_index(
        "ix_manual_attribute_lock_rule_canonical_taxonomy_path_slug",
        table_name="manual_attribute_lock_rule",
    )
    op.drop_table("manual_attribute_lock_rule")
