"""Add override fields to products table

Revision ID: a3b4c5d6e7f8
Revises: f2b3c4d5e6f7
Create Date: 2025-12-11

This migration:
- Renames 'product_id' to 'vendor_sku' for clarity
- Adds new override fields (brand, images, digital delivery)
- Adds vendor-specific digital fulfillment fields
- Changes relationship from one-to-one to one-to-many (same marketplace product
  can be in multiple vendor catalogs)

The override pattern: NULL value means "inherit from marketplace_product".
Setting a value creates a vendor-specific override.
"""

from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

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


def upgrade() -> None:
    # Use batch mode for SQLite compatibility
    with op.batch_alter_table("products", schema=None) as batch_op:
        # Rename product_id to vendor_sku for clarity
        batch_op.alter_column(
            "product_id",
            new_column_name="vendor_sku",
        )

    # Add new override fields
    op.add_column(
        "products",
        sa.Column("brand", sa.String(), nullable=True),
    )
    op.add_column(
        "products",
        sa.Column("primary_image_url", sa.String(), nullable=True),
    )
    op.add_column(
        "products",
        sa.Column("additional_images", sa.JSON(), nullable=True),
    )

    # Add digital product override fields
    op.add_column(
        "products",
        sa.Column("download_url", sa.String(), nullable=True),
    )
    op.add_column(
        "products",
        sa.Column("license_type", sa.String(50), nullable=True),
    )

    # Add vendor-specific digital fulfillment settings
    op.add_column(
        "products",
        sa.Column("fulfillment_email_template", sa.String(), nullable=True),
    )

    # Add supplier tracking (for products sourced from CodesWholesale, etc.)
    op.add_column(
        "products",
        sa.Column("supplier", sa.String(50), nullable=True),
    )
    op.add_column(
        "products",
        sa.Column("supplier_product_id", sa.String(), nullable=True),
    )
    op.add_column(
        "products",
        sa.Column("supplier_cost", sa.Float(), nullable=True),
    )

    # Add margin/markup tracking
    op.add_column(
        "products",
        sa.Column("margin_percent", sa.Float(), nullable=True),
    )

    # Create index for vendor_sku
    op.create_index(
        "idx_product_vendor_sku",
        "products",
        ["vendor_id", "vendor_sku"],
    )

    # Create index for supplier queries
    op.create_index(
        "idx_product_supplier",
        "products",
        ["supplier", "supplier_product_id"],
    )


def downgrade() -> None:
    # Drop indexes
    op.drop_index("idx_product_supplier", table_name="products")
    op.drop_index("idx_product_vendor_sku", table_name="products")

    # Drop new columns
    op.drop_column("products", "margin_percent")
    op.drop_column("products", "supplier_cost")
    op.drop_column("products", "supplier_product_id")
    op.drop_column("products", "supplier")
    op.drop_column("products", "fulfillment_email_template")
    op.drop_column("products", "license_type")
    op.drop_column("products", "download_url")
    op.drop_column("products", "additional_images")
    op.drop_column("products", "primary_image_url")
    op.drop_column("products", "brand")

    # Use batch mode for SQLite compatibility
    with op.batch_alter_table("products", schema=None) as batch_op:
        # Rename vendor_sku back to product_id
        batch_op.alter_column(
            "vendor_sku",
            new_column_name="product_id",
        )
