<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);
            $table->text('description')->nullable();
            $table->foreignId('category_id')->constrained('product_categories')->onDelete('restrict');
            $table->decimal('price', 10, 2);
            $table->decimal('original_price', 10, 2)->nullable();
            $table->string('sku', 50)->unique()->nullable();
            $table->integer('stock_quantity')->default(0);
            $table->string('image_url', 255)->nullable();
            $table->json('images')->nullable();
            $table->json('features')->nullable();
            $table->json('specifications')->nullable();
            $table->json('compatible_cars')->nullable();
            $table->integer('warranty_months')->default(12);
            $table->enum('status', ['active', 'inactive', 'out_of_stock'])->default('active');
            $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();

            $table->index(['category_id']);
            $table->index(['status']);
            $table->index(['price']);
            // $table->fullText(['name', 'description']); // SQLite doesn't support fulltext indexes
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('products');
    }
};