<?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->foreignId('category_id')->nullable()->constrained()->nullOnDelete();
            $table->string('name');
            $table->string('sku')->unique()->comment('Kode produk');
            $table->string('barcode')->nullable()->unique();
            $table->text('description')->nullable();
            $table->string('image')->nullable();
            $table->decimal('cost_price', 15, 2)->default(0)->comment('Harga modal/HPP');
            $table->decimal('selling_price', 15, 2)->default(0)->comment('Harga jual');
            $table->decimal('min_selling_price', 15, 2)->nullable()->comment('Harga minimum, jika ada diskon');
            $table->string('unit', 20)->default('pcs')->comment('Satuan: pcs, kg, liter, dll');
            $table->integer('stock')->default(0);
            $table->integer('min_stock')->default(5)->comment('Stok minimum alert');
            $table->boolean('track_stock')->default(true)->comment('Apakah stok dilacak');
            $table->boolean('is_active')->default(true);
            $table->boolean('is_featured')->default(false);
            $table->json('variants')->nullable()->comment('Varian produk: ukuran, warna, dll');
            $table->timestamps();
            $table->softDeletes();

            $table->index(['category_id', 'is_active']);
            $table->index('barcode');
            $table->index('sku');
        });
    }

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