<?php

declare(strict_types=1);

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->uuid('id')->primary();
            $table->foreignUuid('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('slug', 191);
            $table->string('name', 255);
            $table->longText('description')->nullable();
            $table->uuid('category_id')->nullable();
            $table->string('brand', 120)->nullable();
            $table->string('sku', 80)->nullable();
            $table->bigInteger('price')->default(0);
            $table->bigInteger('sale_price')->nullable();
            $table->string('currency', 8)->default('KRW');
            $table->integer('stock_qty')->default(0);
            $table->enum('status', ['draft', 'active', 'out_of_stock', 'discontinued'])->default('draft');
            $table->string('thumb_url', 500)->nullable();
            $table->json('meta')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['tenant_id', 'slug']);
            $table->index(['tenant_id', 'status', 'category_id']);
            $table->index(['tenant_id', 'sku']);
        });
    }

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