<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * recipe_production_items — defines which production batch (ice cream / gelato / yogurt)
     * a recipe must deduct from, and how many grams.
     *
     * Examples:
     *   Ice Cream Shake recipe → source_product_id = vanilla_ice_cream, grams = 170 (regular)
     *   Ice Cream Shake recipe → source_product_id = vanilla_ice_cream, grams = 230, option_set_item_id = <large>
     *
     * When option_set_item_id is NULL  → used as the base / default quantity.
     * When option_set_item_id is set   → used when that size option is selected in the order.
     */
    public function up(): void
    {
        Schema::create('recipe_production_items', function (Blueprint $table) {
            $table->id();

            $table->foreignId('recipe_id')
                  ->constrained('recipes')
                  ->cascadeOnDelete();

            // Which product's active production batch to deduct from
            $table->foreignId('source_product_id')
                  ->constrained('products')
                  ->cascadeOnDelete();

            // Grams to deduct for this size variant (or default when option_set_item_id is null)
            $table->decimal('grams', 8, 2);

            // NULL = default/base quantity; set = size-specific quantity
            $table->foreignId('option_set_item_id')
                  ->nullable()
                  ->constrained('option_set_items')
                  ->nullOnDelete();

            $table->text('notes')->nullable();
            $table->timestamps();

            $table->index('recipe_id');
            $table->index('source_product_id');
        });
    }

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