"""marketplace initial - marketplace products, imports, letzshop integration

Revision ID: marketplace_001
Revises: billing_001
Create Date: 2026-02-07
"""
import sqlalchemy as sa

from alembic import op

revision = "marketplace_001"
down_revision = "billing_001"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # --- marketplace_products ---
    op.create_table(
        "marketplace_products",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("marketplace_product_id", sa.String(), unique=True, nullable=False, index=True),
        sa.Column("gtin", sa.String(), nullable=True, index=True),
        sa.Column("mpn", sa.String(), nullable=True, index=True),
        sa.Column("sku", sa.String(), nullable=True, index=True),
        sa.Column("marketplace", sa.String(), nullable=True, server_default="letzshop", index=True),
        sa.Column("source_url", sa.String(), nullable=True),
        sa.Column("store_name", sa.String(), nullable=True, index=True),
        sa.Column("product_type_enum", sa.String(20), nullable=False, server_default="physical"),
        sa.Column("is_digital", sa.Boolean(), nullable=True, server_default="false", index=True),
        sa.Column("digital_delivery_method", sa.String(20), nullable=True),
        sa.Column("platform", sa.String(50), nullable=True, index=True),
        sa.Column("region_restrictions", sa.JSON(), nullable=True),
        sa.Column("license_type", sa.String(50), nullable=True),
        sa.Column("brand", sa.String(), nullable=True, index=True),
        sa.Column("google_product_category", sa.String(), nullable=True, index=True),
        sa.Column("category_path", sa.String(), nullable=True),
        sa.Column("condition", sa.String(), nullable=True),
        sa.Column("price", sa.String(), nullable=True),
        sa.Column("price_cents", sa.Integer(), nullable=True),
        sa.Column("sale_price", sa.String(), nullable=True),
        sa.Column("sale_price_cents", sa.Integer(), nullable=True),
        sa.Column("currency", sa.String(3), nullable=True, server_default="EUR"),
        sa.Column("tax_rate_percent", sa.Integer(), nullable=False, server_default="17"),
        sa.Column("image_link", sa.String(), nullable=True),
        sa.Column("additional_image_link", sa.String(), nullable=True),
        sa.Column("additional_images", sa.JSON(), nullable=True),
        sa.Column("attributes", sa.JSON(), nullable=True),
        sa.Column("weight_grams", sa.Integer(), nullable=True),
        sa.Column("weight_unit", sa.String(10), nullable=True, server_default="kg"),
        sa.Column("dimensions", sa.JSON(), nullable=True),
        sa.Column("link", sa.String(), nullable=True),
        sa.Column("availability", sa.String(), nullable=True, index=True),
        sa.Column("adult", sa.String(), nullable=True),
        sa.Column("multipack", sa.Integer(), nullable=True),
        sa.Column("is_bundle", sa.String(), nullable=True),
        sa.Column("age_group", sa.String(), nullable=True),
        sa.Column("color", sa.String(), nullable=True),
        sa.Column("gender", sa.String(), nullable=True),
        sa.Column("material", sa.String(), nullable=True),
        sa.Column("pattern", sa.String(), nullable=True),
        sa.Column("size", sa.String(), nullable=True),
        sa.Column("size_type", sa.String(), nullable=True),
        sa.Column("size_system", sa.String(), nullable=True),
        sa.Column("item_group_id", sa.String(), nullable=True),
        sa.Column("product_type_raw", sa.String(), nullable=True),
        sa.Column("custom_label_0", sa.String(), nullable=True),
        sa.Column("custom_label_1", sa.String(), nullable=True),
        sa.Column("custom_label_2", sa.String(), nullable=True),
        sa.Column("custom_label_3", sa.String(), nullable=True),
        sa.Column("custom_label_4", sa.String(), nullable=True),
        sa.Column("unit_pricing_measure", sa.String(), nullable=True),
        sa.Column("unit_pricing_base_measure", sa.String(), nullable=True),
        sa.Column("identifier_exists", sa.String(), nullable=True),
        sa.Column("shipping", sa.String(), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=True, server_default="true", index=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),
    )
    op.create_index("idx_marketplace_store", "marketplace_products", ["marketplace", "store_name"])
    op.create_index("idx_marketplace_brand", "marketplace_products", ["marketplace", "brand"])
    op.create_index("idx_mp_gtin_marketplace", "marketplace_products", ["gtin", "marketplace"])
    op.create_index("idx_mp_product_type", "marketplace_products", ["product_type_enum", "is_digital"])

    # --- marketplace_product_translations ---
    op.create_table(
        "marketplace_product_translations",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("marketplace_product_id", sa.Integer(), sa.ForeignKey("marketplace_products.id", ondelete="CASCADE"), nullable=False),
        sa.Column("language", sa.String(5), nullable=False),
        sa.Column("title", sa.String(), nullable=False),
        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("source_import_id", sa.Integer(), nullable=True),
        sa.Column("source_file", 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("marketplace_product_id", "language", name="uq_marketplace_product_translation"),
    )
    op.create_index("idx_mpt_marketplace_product_id", "marketplace_product_translations", ["marketplace_product_id"])
    op.create_index("idx_mpt_language", "marketplace_product_translations", ["language"])

    # --- marketplace_import_jobs ---
    op.create_table(
        "marketplace_import_jobs",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=False, index=True),
        sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=False),
        sa.Column("marketplace", sa.String(), nullable=False, server_default="Letzshop", index=True),
        sa.Column("source_url", sa.String(), nullable=False),
        sa.Column("language", sa.String(5), nullable=False, server_default="en"),
        sa.Column("status", sa.String(), nullable=False, server_default="pending"),
        sa.Column("imported_count", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("updated_count", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("error_count", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("total_processed", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("error_message", sa.Text(), nullable=True),
        sa.Column("celery_task_id", sa.String(255), nullable=True, index=True),
        sa.Column("started_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("completed_at", sa.DateTime(timezone=True), 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),
    )
    op.create_index("idx_import_store_status", "marketplace_import_jobs", ["store_id", "status"])
    op.create_index("idx_import_store_created", "marketplace_import_jobs", ["store_id", "created_at"])
    op.create_index("idx_import_user_marketplace", "marketplace_import_jobs", ["user_id", "marketplace"])

    # --- marketplace_import_errors ---
    op.create_table(
        "marketplace_import_errors",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("import_job_id", sa.Integer(), sa.ForeignKey("marketplace_import_jobs.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("row_number", sa.Integer(), nullable=False),
        sa.Column("identifier", sa.String(), nullable=True),
        sa.Column("error_type", sa.String(50), nullable=False),
        sa.Column("error_message", sa.Text(), nullable=False),
        sa.Column("row_data", sa.JSON(), 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),
    )
    op.create_index("idx_import_error_job_id", "marketplace_import_errors", ["import_job_id"])
    op.create_index("idx_import_error_type", "marketplace_import_errors", ["error_type"])

    # --- store_letzshop_credentials ---
    op.create_table(
        "store_letzshop_credentials",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), unique=True, nullable=False, index=True),
        sa.Column("api_key_encrypted", sa.Text(), nullable=False),
        sa.Column("api_endpoint", sa.String(255), nullable=True, server_default="https://letzshop.lu/graphql"),
        sa.Column("auto_sync_enabled", sa.Boolean(), nullable=True, server_default="false"),
        sa.Column("sync_interval_minutes", sa.Integer(), nullable=True, server_default="15"),
        sa.Column("test_mode_enabled", sa.Boolean(), nullable=True, server_default="false"),
        sa.Column("default_carrier", sa.String(50), nullable=True),
        sa.Column("carrier_greco_label_url", sa.String(500), nullable=True, server_default="https://dispatchweb.fr/Tracky/Home/"),
        sa.Column("carrier_colissimo_label_url", sa.String(500), nullable=True),
        sa.Column("carrier_xpresslogistics_label_url", sa.String(500), nullable=True),
        sa.Column("last_sync_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("last_sync_status", sa.String(50), nullable=True),
        sa.Column("last_sync_error", sa.Text(), 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),
    )

    # --- letzshop_sync_logs ---
    op.create_table(
        "letzshop_sync_logs",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=False, index=True),
        sa.Column("operation_type", sa.String(50), nullable=False),
        sa.Column("direction", sa.String(10), nullable=False),
        sa.Column("status", sa.String(50), nullable=False),
        sa.Column("records_processed", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("records_succeeded", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("records_failed", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("error_details", sa.JSON(), nullable=True),
        sa.Column("started_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("duration_seconds", sa.Integer(), nullable=True),
        sa.Column("triggered_by", sa.String(100), 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),
    )
    op.create_index("idx_sync_log_store_type", "letzshop_sync_logs", ["store_id", "operation_type"])
    op.create_index("idx_sync_log_store_date", "letzshop_sync_logs", ["store_id", "started_at"])

    # --- letzshop_store_cache ---
    op.create_table(
        "letzshop_store_cache",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("letzshop_id", sa.String(50), unique=True, nullable=False, index=True),
        sa.Column("slug", sa.String(200), unique=True, nullable=False, index=True),
        sa.Column("name", sa.String(255), nullable=False),
        sa.Column("merchant_name", sa.String(255), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=True, server_default="true"),
        sa.Column("description_en", sa.Text(), nullable=True),
        sa.Column("description_fr", sa.Text(), nullable=True),
        sa.Column("description_de", sa.Text(), nullable=True),
        sa.Column("email", sa.String(255), nullable=True),
        sa.Column("phone", sa.String(50), nullable=True),
        sa.Column("fax", sa.String(50), nullable=True),
        sa.Column("website", sa.String(500), nullable=True),
        sa.Column("street", sa.String(255), nullable=True),
        sa.Column("street_number", sa.String(50), nullable=True),
        sa.Column("city", sa.String(100), nullable=True),
        sa.Column("zipcode", sa.String(20), nullable=True),
        sa.Column("country_iso", sa.String(5), nullable=True, server_default="LU"),
        sa.Column("latitude", sa.String(20), nullable=True),
        sa.Column("longitude", sa.String(20), nullable=True),
        sa.Column("categories", sa.JSON(), nullable=True),
        sa.Column("background_image_url", sa.String(500), nullable=True),
        sa.Column("social_media_links", sa.JSON(), nullable=True),
        sa.Column("opening_hours_en", sa.Text(), nullable=True),
        sa.Column("opening_hours_fr", sa.Text(), nullable=True),
        sa.Column("opening_hours_de", sa.Text(), nullable=True),
        sa.Column("representative_name", sa.String(255), nullable=True),
        sa.Column("representative_title", sa.String(100), nullable=True),
        sa.Column("claimed_by_store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=True, index=True),
        sa.Column("claimed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("last_synced_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("raw_data", sa.JSON(), 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),
    )
    op.create_index("idx_letzshop_cache_city", "letzshop_store_cache", ["city"])
    op.create_index("idx_letzshop_cache_claimed", "letzshop_store_cache", ["claimed_by_store_id"])
    op.create_index("idx_letzshop_cache_active", "letzshop_store_cache", ["is_active"])

    # --- letzshop_historical_import_jobs ---
    op.create_table(
        "letzshop_historical_import_jobs",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=False, index=True),
        sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=False),
        sa.Column("status", sa.String(50), nullable=False, server_default="pending"),
        sa.Column("current_phase", sa.String(20), nullable=True),
        sa.Column("current_page", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("total_pages", sa.Integer(), nullable=True),
        sa.Column("shipments_fetched", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("orders_processed", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("orders_imported", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("orders_updated", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("orders_skipped", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("products_matched", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("products_not_found", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("confirmed_stats", sa.JSON(), nullable=True),
        sa.Column("declined_stats", sa.JSON(), nullable=True),
        sa.Column("error_message", sa.Text(), nullable=True),
        sa.Column("celery_task_id", sa.String(255), nullable=True, index=True),
        sa.Column("started_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("completed_at", sa.DateTime(timezone=True), 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),
    )
    op.create_index("idx_historical_import_store", "letzshop_historical_import_jobs", ["store_id", "status"])

    # --- store_onboarding ---
    op.create_table(
        "store_onboarding",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id", ondelete="CASCADE"), unique=True, nullable=False, index=True),
        sa.Column("status", sa.String(20), nullable=False, server_default="not_started", index=True),
        sa.Column("current_step", sa.String(30), nullable=False, server_default="merchant_profile"),
        sa.Column("step_merchant_profile_completed", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("step_merchant_profile_completed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("step_merchant_profile_data", sa.JSON(), nullable=True),
        sa.Column("step_letzshop_api_completed", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("step_letzshop_api_completed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("step_letzshop_api_connection_verified", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("step_product_import_completed", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("step_product_import_completed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("step_product_import_csv_url_set", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("step_order_sync_completed", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("step_order_sync_completed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("step_order_sync_job_id", sa.Integer(), nullable=True),
        sa.Column("started_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("skipped_by_admin", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("skipped_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("skipped_reason", sa.Text(), nullable=True),
        sa.Column("skipped_by_user_id", sa.Integer(), sa.ForeignKey("users.id"), 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),
    )
    op.create_index("idx_onboarding_store_status", "store_onboarding", ["store_id", "status"])


def downgrade() -> None:
    op.drop_table("store_onboarding")
    op.drop_table("letzshop_historical_import_jobs")
    op.drop_table("letzshop_store_cache")
    op.drop_table("letzshop_sync_logs")
    op.drop_table("store_letzshop_credentials")
    op.drop_table("marketplace_import_errors")
    op.drop_table("marketplace_import_jobs")
    op.drop_table("marketplace_product_translations")
    op.drop_table("marketplace_products")
