"""add channel attribute alias

Revision ID: 0028_add_channel_attribute_alias
Revises: 0027_add_shopify_attribute_registry
Create Date: 2026-06-05 00:00:00.000000
"""

from __future__ import annotations

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


revision = "0028_add_channel_attribute_alias"
down_revision = "0027_add_shopify_attribute_registry"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "channel_attribute_alias",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("channel_code", sa.String(length=32), nullable=False),
        sa.Column("canonical_code", sa.String(length=128), nullable=False),
        sa.Column("channel_attribute_code", sa.String(length=255), nullable=False),
        sa.Column("action", sa.String(length=32), nullable=False, server_default="use_existing"),
        sa.Column("data_type", sa.String(length=32), nullable=True),
        sa.Column("transform_rule", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("notes", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "channel_code",
            "canonical_code",
            "channel_attribute_code",
            name="uq_channel_attribute_alias_channel_canonical_attr",
        ),
    )
    op.create_index("ix_channel_attribute_alias_channel_code", "channel_attribute_alias", ["channel_code"])
    op.create_index("ix_channel_attribute_alias_canonical_code", "channel_attribute_alias", ["canonical_code"])
    op.create_index("ix_channel_attribute_alias_channel_attribute_code", "channel_attribute_alias", ["channel_attribute_code"])


def downgrade() -> None:
    op.drop_index("ix_channel_attribute_alias_channel_attribute_code", table_name="channel_attribute_alias")
    op.drop_index("ix_channel_attribute_alias_canonical_code", table_name="channel_attribute_alias")
    op.drop_index("ix_channel_attribute_alias_channel_code", table_name="channel_attribute_alias")
    op.drop_table("channel_attribute_alias")
