"""Add product type and digital fields to marketplace_products

Revision ID: e1a2b3c4d5e6
Revises: 28d44d503cac
Create Date: 2025-12-11

This migration adds support for:
- Product type classification (physical, digital, service, subscription)
- Digital product fields (delivery method, platform, region restrictions)
- Numeric price fields for filtering/sorting
- Additional images as JSON array
- Source URL tracking
- Flexible attributes JSON column
- Active status flag

It also renames 'product_type' to 'product_type_raw' to preserve the original
Google Shopping feed value while using 'product_type' for the new enum.
"""

from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

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


def upgrade() -> None:
    # Rename existing product_type column to product_type_raw
    # to preserve the original Google Shopping feed value
    op.alter_column(
        "marketplace_products",
        "product_type",
        new_column_name="product_type_raw",
    )

    # Add new product classification columns
    op.add_column(
        "marketplace_products",
        sa.Column(
            "product_type_enum",
            sa.String(20),
            nullable=False,
            server_default="physical",
        ),
    )
    op.add_column(
        "marketplace_products",
        sa.Column(
            "is_digital",
            sa.Boolean(),
            nullable=False,
            server_default=sa.text("false"),
        ),
    )

    # Add digital product specific fields
    op.add_column(
        "marketplace_products",
        sa.Column("digital_delivery_method", sa.String(20), nullable=True),
    )
    op.add_column(
        "marketplace_products",
        sa.Column("platform", sa.String(50), nullable=True),
    )
    op.add_column(
        "marketplace_products",
        sa.Column("region_restrictions", sa.JSON(), nullable=True),
    )
    op.add_column(
        "marketplace_products",
        sa.Column("license_type", sa.String(50), nullable=True),
    )

    # Add source tracking
    op.add_column(
        "marketplace_products",
        sa.Column("source_url", sa.String(), nullable=True),
    )

    # Add numeric price fields for filtering/sorting
    op.add_column(
        "marketplace_products",
        sa.Column("price_numeric", sa.Float(), nullable=True),
    )
    op.add_column(
        "marketplace_products",
        sa.Column("sale_price_numeric", sa.Float(), nullable=True),
    )

    # Add flexible attributes JSON column
    op.add_column(
        "marketplace_products",
        sa.Column("attributes", sa.JSON(), nullable=True),
    )

    # Add additional images as JSON array (complements existing additional_image_link)
    op.add_column(
        "marketplace_products",
        sa.Column("additional_images", sa.JSON(), nullable=True),
    )

    # Add active status flag
    op.add_column(
        "marketplace_products",
        sa.Column(
            "is_active",
            sa.Boolean(),
            nullable=False,
            server_default=sa.text("true"),
        ),
    )

    # Add SKU field for internal reference
    op.add_column(
        "marketplace_products",
        sa.Column("sku", sa.String(), nullable=True),
    )

    # Add weight fields for physical products
    op.add_column(
        "marketplace_products",
        sa.Column("weight", sa.Float(), nullable=True),
    )
    op.add_column(
        "marketplace_products",
        sa.Column("weight_unit", sa.String(10), nullable=True, server_default="kg"),
    )
    op.add_column(
        "marketplace_products",
        sa.Column("dimensions", sa.JSON(), nullable=True),
    )

    # Add category_path for normalized hierarchy
    op.add_column(
        "marketplace_products",
        sa.Column("category_path", sa.String(), nullable=True),
    )

    # Create indexes for new columns
    op.create_index(
        "idx_mp_product_type",
        "marketplace_products",
        ["product_type_enum", "is_digital"],
    )
    op.create_index(
        "idx_mp_is_active",
        "marketplace_products",
        ["is_active"],
    )
    op.create_index(
        "idx_mp_platform",
        "marketplace_products",
        ["platform"],
    )
    op.create_index(
        "idx_mp_sku",
        "marketplace_products",
        ["sku"],
    )
    op.create_index(
        "idx_mp_gtin_marketplace",
        "marketplace_products",
        ["gtin", "marketplace"],
    )


def downgrade() -> None:
    # Drop indexes
    op.drop_index("idx_mp_gtin_marketplace", table_name="marketplace_products")
    op.drop_index("idx_mp_sku", table_name="marketplace_products")
    op.drop_index("idx_mp_platform", table_name="marketplace_products")
    op.drop_index("idx_mp_is_active", table_name="marketplace_products")
    op.drop_index("idx_mp_product_type", table_name="marketplace_products")

    # Drop new columns
    op.drop_column("marketplace_products", "category_path")
    op.drop_column("marketplace_products", "dimensions")
    op.drop_column("marketplace_products", "weight_unit")
    op.drop_column("marketplace_products", "weight")
    op.drop_column("marketplace_products", "sku")
    op.drop_column("marketplace_products", "is_active")
    op.drop_column("marketplace_products", "additional_images")
    op.drop_column("marketplace_products", "attributes")
    op.drop_column("marketplace_products", "sale_price_numeric")
    op.drop_column("marketplace_products", "price_numeric")
    op.drop_column("marketplace_products", "source_url")
    op.drop_column("marketplace_products", "license_type")
    op.drop_column("marketplace_products", "region_restrictions")
    op.drop_column("marketplace_products", "platform")
    op.drop_column("marketplace_products", "digital_delivery_method")
    op.drop_column("marketplace_products", "is_digital")
    op.drop_column("marketplace_products", "product_type_enum")

    # Rename product_type_raw back to product_type
    op.alter_column(
        "marketplace_products",
        "product_type_raw",
        new_column_name="product_type",
    )
