<?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('core_products', function (Blueprint $table) {
            $table->id();
            $table->string('product_code')->unique();
            $table->string('sku')->unique();
            $table->string('barcode')->nullable()->unique();
            $table->string('name');
            $table->text('description')->nullable();
            $table->foreignId('category_id')->nullable()->constrained('core_product_categories')->nullOnDelete();
            $table->string('brand')->nullable();
            $table->string('status')->default('active');
            $table->string('unit_of_measure')->default('pcs'); // pcs, kg, ltr, etc.
            $table->decimal('base_price', 15, 2)->default(0);
            $table->decimal('cost_price', 15, 2)->default(0);
            $table->decimal('selling_price', 15, 2)->default(0);
            $table->foreignId('tax_id')->nullable()->constrained('core_tax_configurations')->nullOnDelete();
            $table->decimal('weight', 10, 2)->nullable();
            $table->string('weight_unit')->nullable(); // kg, g, lb, oz
            $table->json('dimensions')->nullable(); // {length, width, height, unit}
            $table->integer('min_stock_level')->default(0);
            $table->integer('max_stock_level')->default(0);
            $table->integer('reorder_level')->default(0);
            $table->json('images')->nullable();
            $table->string('thumbnail')->nullable();
            $table->json('tags')->nullable();
            $table->json('metadata')->nullable();
            $table->boolean('is_active')->default(true);
            $table->boolean('is_trackable')->default(true);
            $table->boolean('has_variants')->default(false);
            $table->foreignId('created_by_id')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['category_id', 'status']);
            $table->index('is_active');
            $table->index('brand');
        });
    }

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