"""Add manual variation assignment persistence tables.

Revision ID: 0066_add_manual_variation_assignment_tables
Revises: 0065_add_manual_attribute_vocabulary_tables
Create Date: 2026-08-03 16:20:00.000000
"""

from __future__ import annotations

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

revision = "0066_add_manual_variation_assignment_tables"
down_revision = "0065_add_manual_attribute_vocabulary_tables"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "manual_variation_assignment",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("source_parent_sku", sa.String(length=128), nullable=False),
        sa.Column("source_parent_name", sa.Text(), nullable=True),
        sa.Column("scope_kind", sa.String(length=32), nullable=False, server_default="selected"),
        sa.Column("option_attrs", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("group_values", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("assignment_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("source_parent_sku", name="uq_manual_variation_assignment_source_parent_sku"),
    )
    op.create_index("ix_mva_source_parent_sku", "manual_variation_assignment", ["source_parent_sku"])
    op.create_index("ix_mva_scope_kind", "manual_variation_assignment", ["scope_kind"])
    op.create_index("ix_mva_assignment_status", "manual_variation_assignment", ["assignment_status"])

    op.create_table(
        "manual_variation_assignment_collection",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("assignment_id", sa.Integer(), sa.ForeignKey("manual_variation_assignment.id"), nullable=False),
        sa.Column("collection_registry_id", sa.Integer(), sa.ForeignKey("master_collection_registry.id"), nullable=True),
        sa.Column("collection_code", sa.String(length=128), nullable=False),
        sa.Column("source_collection_code", sa.String(length=128), nullable=True),
        sa.Column("sku_prefix", sa.String(length=128), nullable=False),
        sa.Column("collection_name", sa.Text(), nullable=True),
        sa.Column("collection_path_slug", sa.String(length=512), nullable=True),
        sa.Column("parent_sku", sa.String(length=128), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False, server_default="100"),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
        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("assignment_id", "collection_code", name="uq_manual_variation_assignment_collection_code"),
    )
    op.create_index(
        "ix_mvac_assignment_id",
        "manual_variation_assignment_collection",
        ["assignment_id"],
    )
    op.create_index(
        "ix_mvac_collection_registry_id",
        "manual_variation_assignment_collection",
        ["collection_registry_id"],
    )
    op.create_index(
        "ix_mvac_collection_code",
        "manual_variation_assignment_collection",
        ["collection_code"],
    )
    op.create_index(
        "ix_mvac_source_collection_code",
        "manual_variation_assignment_collection",
        ["source_collection_code"],
    )
    op.create_index(
        "ix_mvac_sku_prefix",
        "manual_variation_assignment_collection",
        ["sku_prefix"],
    )
    op.create_index(
        "ix_mvac_collection_path_slug",
        "manual_variation_assignment_collection",
        ["collection_path_slug"],
    )
    op.create_index(
        "ix_mvac_parent_sku",
        "manual_variation_assignment_collection",
        ["parent_sku"],
    )

    op.create_table(
        "manual_variation_assignment_child",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("assignment_id", sa.Integer(), sa.ForeignKey("manual_variation_assignment.id"), nullable=False),
        sa.Column("source_child_sku", sa.String(length=128), nullable=False),
        sa.Column("child_name", sa.Text(), nullable=True),
        sa.Column("option_mapping", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("sort_order", sa.Integer(), nullable=False, server_default="100"),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
        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("assignment_id", "source_child_sku", name="uq_manual_variation_assignment_child_source_sku"),
    )
    op.create_index(
        "ix_mvach_assignment_id",
        "manual_variation_assignment_child",
        ["assignment_id"],
    )
    op.create_index(
        "ix_mvach_source_child_sku",
        "manual_variation_assignment_child",
        ["source_child_sku"],
    )


def downgrade() -> None:
    op.drop_index(
        "ix_mvach_source_child_sku",
        table_name="manual_variation_assignment_child",
    )
    op.drop_index(
        "ix_mvach_assignment_id",
        table_name="manual_variation_assignment_child",
    )
    op.drop_table("manual_variation_assignment_child")

    op.drop_index(
        "ix_mvac_parent_sku",
        table_name="manual_variation_assignment_collection",
    )
    op.drop_index(
        "ix_mvac_collection_path_slug",
        table_name="manual_variation_assignment_collection",
    )
    op.drop_index(
        "ix_mvac_sku_prefix",
        table_name="manual_variation_assignment_collection",
    )
    op.drop_index(
        "ix_mvac_source_collection_code",
        table_name="manual_variation_assignment_collection",
    )
    op.drop_index(
        "ix_mvac_collection_code",
        table_name="manual_variation_assignment_collection",
    )
    op.drop_index(
        "ix_mvac_collection_registry_id",
        table_name="manual_variation_assignment_collection",
    )
    op.drop_index(
        "ix_mvac_assignment_id",
        table_name="manual_variation_assignment_collection",
    )
    op.drop_table("manual_variation_assignment_collection")

    op.drop_index(
        "ix_mva_assignment_status",
        table_name="manual_variation_assignment",
    )
    op.drop_index(
        "ix_mva_scope_kind",
        table_name="manual_variation_assignment",
    )
    op.drop_index(
        "ix_mva_source_parent_sku",
        table_name="manual_variation_assignment",
    )
    op.drop_table("manual_variation_assignment")
