<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * Product‑first extension: BMS marketing metadata + media gallery keyed to a
 * core ERP product, so a product is the single source of truth for the funnel
 * (Arabic name/description, add‑on flag + default price, image/video/360 gallery,
 * per‑variation Arabic labels). No core product table changes — BMS owns these.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('bms_product_meta')) {
            Schema::create('bms_product_meta', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedInteger('product_id');
                $t->string('name_ar', 191)->nullable();
                $t->text('description_ar')->nullable();
                $t->boolean('is_addon')->default(false);
                $t->decimal('addon_price', 15, 2)->nullable();
                $t->string('addon_unit', 30)->nullable();
                $t->json('variation_labels_ar')->nullable(); // { variation_id: "AR label" }
                $t->string('default_unit_ar', 30)->nullable(); // serving unit for combo components
                $t->timestamps();
                $t->unique(['product_id'], 'bms_product_meta_product_unique');
                $t->index(['business_id', 'is_addon']);
            });
        }

        if (! Schema::hasTable('bms_product_media')) {
            Schema::create('bms_product_media', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedInteger('product_id');
                $t->enum('type', ['image', 'video', '360'])->default('image');
                $t->string('disk', 60)->nullable();
                $t->string('path', 255)->nullable();
                $t->string('url', 500)->nullable();
                $t->string('thumbnail_path', 255)->nullable();
                $t->string('title', 191)->nullable();
                $t->boolean('is_cover')->default(false);
                $t->integer('sort_order')->default(0);
                $t->json('meta')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'product_id']);
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('bms_product_media');
        Schema::dropIfExists('bms_product_meta');
    }
};
