<?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('batch_deductions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('production_batch_id')->constrained('production_batches')->onDelete('cascade');
            $table->foreignId('order_id')->constrained('orders')->onDelete('cascade');
            $table->foreignId('order_item_id')->constrained('order_items')->onDelete('cascade');
            $table->decimal('weight_grams', 8, 2); // Weight deducted from batch in grams
            $table->integer('scoop_count')->nullable(); // Equivalent scoop count (for reference)
            $table->decimal('cost_amount', 8, 2)->nullable(); // Cost of this deduction
            $table->text('notes')->nullable();
            $table->timestamps();

            // Indexes for performance
            $table->index('production_batch_id');
            $table->index('order_id');
            $table->index('created_at');
        });
    }

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