<?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('recipes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('product_id')->constrained('products')->onDelete('cascade')->comment('連結的成品商品 ID');
            $table->string('code')->unique()->comment('配方代號');
            $table->string('name')->comment('配方名稱');
            $table->text('description')->nullable()->comment('配方描述');
            $table->decimal('yield_quantity', 10, 2)->default(1.00)->comment('標準產出數量');
            $table->boolean('is_active')->default(true)->comment('是否啟用');
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('recipe_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('recipe_id')->constrained('recipes')->onDelete('cascade');
            $table->foreignId('product_id')->constrained('products')->comment('原物料商品 ID');
            $table->decimal('quantity', 10, 4)->comment('標準用量');
            $table->foreignId('unit_id')->nullable()->constrained('units')->comment('單位 ID');
            $table->string('remark')->nullable()->comment('備註');
            $table->timestamps();
        });
    }

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