"""catalog initial - products, translations, product media

Revision ID: catalog_001
Revises: cms_001
Create Date: 2026-02-07
"""
import sqlalchemy as sa

from alembic import op

revision = "catalog_001"
down_revision = "cms_001"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # --- products ---
    op.create_table(
        "products",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=False),
        sa.Column("marketplace_product_id", sa.Integer(), sa.ForeignKey("marketplace_products.id"), nullable=True),
        sa.Column("store_sku", sa.String(), nullable=True, index=True),
        sa.Column("gtin", sa.String(50), nullable=True, index=True),
        sa.Column("gtin_type", sa.String(20), nullable=True),
        sa.Column("price_cents", sa.Integer(), nullable=True),
        sa.Column("sale_price_cents", sa.Integer(), nullable=True),
        sa.Column("currency", sa.String(3), nullable=True, server_default="EUR"),
        sa.Column("brand", sa.String(), nullable=True),
        sa.Column("condition", sa.String(), nullable=True),
        sa.Column("availability", sa.String(), nullable=True),
        sa.Column("primary_image_url", sa.String(), nullable=True),
        sa.Column("additional_images", sa.JSON(), nullable=True),
        sa.Column("download_url", sa.String(), nullable=True),
        sa.Column("license_type", sa.String(50), nullable=True),
        sa.Column("tax_rate_percent", sa.Integer(), nullable=False, server_default="17"),
        sa.Column("supplier", sa.String(50), nullable=True),
        sa.Column("supplier_product_id", sa.String(), nullable=True),
        sa.Column("cost_cents", sa.Integer(), nullable=True),
        sa.Column("margin_percent_x100", sa.Integer(), nullable=True),
        sa.Column("is_digital", sa.Boolean(), nullable=True, server_default="false", index=True),
        sa.Column("product_type", sa.String(20), nullable=True, server_default="physical"),
        sa.Column("is_featured", sa.Boolean(), nullable=True, server_default="false"),
        sa.Column("is_active", sa.Boolean(), nullable=True, server_default="true"),
        sa.Column("display_order", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("min_quantity", sa.Integer(), nullable=True, server_default="1"),
        sa.Column("max_quantity", sa.Integer(), nullable=True),
        sa.Column("fulfillment_email_template", sa.String(), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.UniqueConstraint("store_id", "marketplace_product_id", name="uq_store_marketplace_product"),
    )
    op.create_index("idx_product_store_active", "products", ["store_id", "is_active"])
    op.create_index("idx_product_store_featured", "products", ["store_id", "is_featured"])
    op.create_index("idx_product_store_sku", "products", ["store_id", "store_sku"])
    op.create_index("idx_product_supplier", "products", ["supplier", "supplier_product_id"])

    # --- product_translations ---
    op.create_table(
        "product_translations",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("product_id", sa.Integer(), sa.ForeignKey("products.id", ondelete="CASCADE"), nullable=False),
        sa.Column("language", sa.String(5), nullable=False),
        sa.Column("title", sa.String(), nullable=True),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("short_description", sa.String(500), nullable=True),
        sa.Column("meta_title", sa.String(70), nullable=True),
        sa.Column("meta_description", sa.String(160), nullable=True),
        sa.Column("url_slug", sa.String(255), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.UniqueConstraint("product_id", "language", name="uq_product_translation"),
    )
    op.create_index("idx_pt_product_id", "product_translations", ["product_id"])
    op.create_index("idx_pt_product_language", "product_translations", ["product_id", "language"])

    # --- product_media ---
    op.create_table(
        "product_media",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("product_id", sa.Integer(), sa.ForeignKey("products.id", ondelete="CASCADE"), nullable=False),
        sa.Column("media_id", sa.Integer(), sa.ForeignKey("media_files.id", ondelete="CASCADE"), nullable=False),
        sa.Column("usage_type", sa.String(50), nullable=False, server_default="gallery"),
        sa.Column("display_order", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("variant_id", sa.Integer(), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.UniqueConstraint("product_id", "media_id", "usage_type", name="uq_product_media_usage"),
    )
    op.create_index("idx_product_media_product", "product_media", ["product_id"])
    op.create_index("idx_product_media_media", "product_media", ["media_id"])


def downgrade() -> None:
    op.drop_table("product_media")
    op.drop_table("product_translations")
    op.drop_table("products")
