<?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('products', function (Blueprint $table) {
            $table->id();
            $table->ulid('uuid')->unique();
            $table->string('name', 200);
            $table->text('description')->nullable();
            $table->string('sku', 60)->unique();
            $table->string('barcode', 50)->nullable()->unique();
            $table->string('image_url', 500)->nullable();
            $table->decimal('sale_price', 12, 2);
            $table->decimal('purchase_price', 12, 2)->nullable();
            $table->decimal('vat_rate', 5, 2)->default(0);

            // Attributes (free-text per SRS)
            $table->string('brand', 100)->nullable();
            $table->string('model', 100)->nullable();
            $table->string('color', 50)->nullable();
            $table->string('size', 50)->nullable();
            $table->string('weight', 50)->nullable();
            $table->string('dimensions', 100)->nullable();
            $table->string('material', 100)->nullable();

            // Taxonomy
            $table->foreignId('main_category_id')->nullable()->constrained('main_categories')->nullOnDelete();
            $table->foreignId('sub_category_id')->nullable()->constrained('sub_categories')->nullOnDelete();

            $table->boolean('is_active')->default(true);

            // Audit
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('deleted_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();

            $table->index('barcode');
            $table->index('sku');
        });
    }

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