"""Add master_product_image for SKU-linked gallery URLs.

Revision ID: 0041_add_master_product_image
Revises: 0040_add_listing_paths_and_url_workspace
Create Date: 2026-06-14 12:00:00.000000
"""

from __future__ import annotations

from alembic import op
import sqlalchemy as sa

revision = "0041_add_master_product_image"
down_revision = "0040_add_listing_paths_and_url_workspace"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "master_product_image",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("product_id", sa.Integer(), sa.ForeignKey("master_product.id"), nullable=False),
        sa.Column("sku", sa.String(128), nullable=False),
        sa.Column("file_name", sa.String(512), nullable=False),
        sa.Column("image_url", sa.Text(), nullable=False),
        sa.Column("view_suffix", sa.String(64), nullable=True),
        sa.Column("sort_order", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("source_label", sa.String(128), nullable=True),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.func.now(),
        ),
        sa.UniqueConstraint("product_id", "file_name", name="uq_master_product_image_product_file"),
    )
    op.create_index("ix_master_product_image_sku", "master_product_image", ["sku"])
    op.create_index("ix_master_product_image_product_id", "master_product_image", ["product_id"])


def downgrade() -> None:
    op.drop_index("ix_master_product_image_product_id", table_name="master_product_image")
    op.drop_index("ix_master_product_image_sku", table_name="master_product_image")
    op.drop_table("master_product_image")
