"""add_gtin_columns_to_product_table

Revision ID: cb88bc9b5f86
Revises: a9a86cef6cca
Create Date: 2025-12-18 20:54:55.185857

"""
from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "cb88bc9b5f86"
down_revision: str | None = "a9a86cef6cca"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # Add GTIN (EAN/UPC barcode) columns to products table for order EAN matching
    # gtin: The barcode number (e.g., "0889698273022")
    # gtin_type: The format type from Letzshop (e.g., "gtin13", "gtin14", "isbn13")
    op.add_column("products", sa.Column("gtin", sa.String(length=50), nullable=True))
    op.add_column("products", sa.Column("gtin_type", sa.String(length=20), nullable=True))

    # Add index for EAN lookups during order matching
    op.create_index("idx_product_gtin", "products", ["gtin"], unique=False)
    op.create_index("idx_product_vendor_gtin", "products", ["vendor_id", "gtin"], unique=False)


def downgrade() -> None:
    op.drop_index("idx_product_vendor_gtin", table_name="products")
    op.drop_index("idx_product_gtin", table_name="products")
    op.drop_column("products", "gtin_type")
    op.drop_column("products", "gtin")
