"""add shopify attribute registry

Revision ID: 0027_add_shopify_attribute_registry
Revises: 0026_add_plytix_attribute_catalog
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 = "0027_add_shopify_attribute_registry"
down_revision = "0026_add_plytix_attribute_catalog"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "shopify_attribute_registry",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("shop_code", sa.String(length=255), nullable=False),
        sa.Column("owner_type", sa.String(length=64), nullable=False),
        sa.Column("attribute_code", sa.String(length=255), nullable=False),
        sa.Column("name", sa.Text(), nullable=True),
        sa.Column("namespace", sa.String(length=255), nullable=True),
        sa.Column("key", sa.String(length=255), nullable=True),
        sa.Column("data_type", sa.String(length=128), nullable=True),
        sa.Column("is_standard", sa.Boolean(), nullable=False, server_default=sa.false()),
        sa.Column("raw_json", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("fetched_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "shop_code",
            "owner_type",
            "attribute_code",
            name="uq_shopify_attr_registry_shop_owner_code",
        ),
    )
    op.create_index("ix_shopify_attribute_registry_shop_code", "shopify_attribute_registry", ["shop_code"])
    op.create_index("ix_shopify_attribute_registry_owner_type", "shopify_attribute_registry", ["owner_type"])
    op.create_index("ix_shopify_attribute_registry_attribute_code", "shopify_attribute_registry", ["attribute_code"])


def downgrade() -> None:
    op.drop_index("ix_shopify_attribute_registry_attribute_code", table_name="shopify_attribute_registry")
    op.drop_index("ix_shopify_attribute_registry_owner_type", table_name="shopify_attribute_registry")
    op.drop_index("ix_shopify_attribute_registry_shop_code", table_name="shopify_attribute_registry")
    op.drop_table("shopify_attribute_registry")
