<?php

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

/**
 * Replace yield_quantity (integer servings) with yield_grams (decimal weight).
 * Production is now tracked by grams, not servings.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('recipes', function (Blueprint $table) {
            $table->decimal('yield_grams', 10, 2)->default(0)->after('yield_quantity')
                  ->comment('Total weight in grams this recipe produces');
        });

        // Copy existing data: treat yield_quantity as grams (best effort)
        \Illuminate\Support\Facades\DB::statement('UPDATE recipes SET yield_grams = yield_quantity');

        Schema::table('recipes', function (Blueprint $table) {
            $table->dropColumn('yield_quantity');
        });
    }

    public function down(): void
    {
        Schema::table('recipes', function (Blueprint $table) {
            $table->integer('yield_quantity')->default(1)->after('yield_grams');
        });

        \Illuminate\Support\Facades\DB::statement('UPDATE recipes SET yield_quantity = CAST(yield_grams AS UNSIGNED)');

        Schema::table('recipes', function (Blueprint $table) {
            $table->dropColumn('yield_grams');
        });
    }
};
