<?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
    {
        // Recipe Master - Links products to their recipes
        Schema::create('recipes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('product_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->text('description')->nullable();
            $table->text('instructions')->nullable();
            $table->integer('yield_quantity')->default(1); // How many servings this recipe makes
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();

            $table->index('product_id');
            $table->index('is_active');
        });

        // Recipe Ingredients - Maps each recipe to ingredients with quantities
        Schema::create('recipe_ingredients', function (Blueprint $table) {
            $table->id();
            $table->foreignId('recipe_id')->constrained()->cascadeOnDelete();
            $table->foreignId('ingredient_id')->constrained()->cascadeOnDelete();
            $table->decimal('quantity', 10, 4); // Amount of ingredient needed
            $table->string('unit')->nullable(); // Can override ingredient's default unit
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->unique(['recipe_id', 'ingredient_id']);
            $table->index('recipe_id');
            $table->index('ingredient_id');
        });

        // Scoop Yield Configuration - For ice cream products
        Schema::create('scoop_yields', function (Blueprint $table) {
            $table->id();
            $table->foreignId('ingredient_id')->constrained()->cascadeOnDelete();
            $table->foreignId('option_set_item_id')->nullable()->constrained()->cascadeOnDelete(); // Size option
            $table->decimal('yield_per_scoop', 10, 4); // How much ingredient per scoop
            $table->string('unit'); // grams, ml, etc.
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->index('ingredient_id');
            $table->index('option_set_item_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('scoop_yields');
        Schema::dropIfExists('recipe_ingredients');
        Schema::dropIfExists('recipes');
    }
};
