<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('kits', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('academy_id')->constrained('academies');
            $table->string('name_ar');
            $table->string('name')->nullable();
            $table->string('sku', 50);
            $table->text('description_ar')->nullable();
            $table->bigInteger('selling_price');
            $table->boolean('is_active')->default(true);
            $table->text('assembly_instructions')->nullable();
            $table->string('photo_path')->nullable();
            $table->foreignId('created_by')->constrained('users');
            $table->jsonb('metadata')->default('{}');
            $table->timestamps();
            $table->softDeletes();

            $table->index(['academy_id', 'is_active']);
            $table->unique(['academy_id', 'sku']);
        });

        Schema::create('kit_components', function (Blueprint $table) {
            $table->id();
            $table->foreignId('kit_id')->constrained('kits')->cascadeOnDelete();
            $table->foreignId('product_id')->constrained('products');
            $table->integer('quantity')->default(1);
            $table->integer('sort_order')->default(0);
            $table->timestamps();

            $table->index('kit_id');
            $table->index('product_id');
            $table->unique(['kit_id', 'product_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('kit_components');
        Schema::dropIfExists('kits');
    }
};
