"""Add PLP listing path registry and SKU listing path assignments.

Revision ID: 0040_add_listing_paths_and_url_workspace
Revises: 0039_add_taxonomy_registry_and_sku_assignments
Create Date: 2026-06-13 22:00:00.000000
"""

from __future__ import annotations

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

revision = "0040_add_listing_paths_and_url_workspace"
down_revision = "0039_add_taxonomy_registry_and_sku_assignments"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "channel_listing_path",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("path_slug", sa.String(512), nullable=False),
        sa.Column("path_kind", sa.String(32), nullable=False, server_default="facet"),
        sa.Column("title", sa.Text(), nullable=False),
        sa.Column("parent_path_slug", sa.String(512), nullable=True),
        sa.Column("hub_path_slug", sa.String(512), nullable=True),
        sa.Column("facet_terms", JSONB, nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default="true"),
        sa.Column("notes", sa.Text(), nullable=True),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.func.now(),
        ),
        sa.UniqueConstraint("path_slug", name="uq_channel_listing_path_slug"),
    )
    op.create_index("ix_channel_listing_path_parent", "channel_listing_path", ["parent_path_slug"])

    op.create_table(
        "channel_listing_path_target",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("listing_path_id", sa.Integer(), sa.ForeignKey("channel_listing_path.id"), nullable=False),
        sa.Column("channel_code", sa.String(32), nullable=False),
        sa.Column("connection_id", sa.Integer(), nullable=True),
        sa.Column("taxonomy_kind", sa.String(32), nullable=False, server_default="category"),
        sa.Column("remote_id", sa.String(255), nullable=False),
        sa.Column("remote_path", sa.Text(), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default="true"),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.func.now(),
        ),
        sa.UniqueConstraint(
            "listing_path_id",
            "channel_code",
            "connection_id",
            "taxonomy_kind",
            "remote_id",
            name="uq_channel_listing_path_target",
        ),
    )

    op.create_table(
        "product_listing_path_assignment",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("master_sku", sa.String(128), nullable=False),
        sa.Column("listing_path_id", sa.Integer(), sa.ForeignKey("channel_listing_path.id"), nullable=False),
        sa.Column("assignment_status", sa.String(32), nullable=False, server_default="active"),
        sa.Column("match_source", sa.String(64), nullable=True),
        sa.Column("sort_order", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("notes", sa.Text(), nullable=True),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.func.now(),
        ),
        sa.UniqueConstraint(
            "master_sku",
            "listing_path_id",
            name="uq_product_listing_path_assignment",
        ),
    )
    op.create_index(
        "ix_product_listing_path_assignment_sku",
        "product_listing_path_assignment",
        ["master_sku"],
    )


def downgrade() -> None:
    op.drop_index("ix_product_listing_path_assignment_sku", table_name="product_listing_path_assignment")
    op.drop_table("product_listing_path_assignment")
    op.drop_table("channel_listing_path_target")
    op.drop_index("ix_channel_listing_path_parent", table_name="channel_listing_path")
    op.drop_table("channel_listing_path")
