<?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('manufacturing_runs', function (Blueprint $table) {
            $table->id();
            $table->string('m_number')->unique();
            $table->foreignId('manufacturing_bom_id')->constrained('manufacturing_boms')->restrictOnDelete();
            $table->decimal('planned_output_qty', 15, 4);
            $table->decimal('scaling_factor', 10, 6);
            $table->foreignId('warehouse_id')->constrained('warehouses')->restrictOnDelete();
            $table->date('due_date')->nullable();
            $table->enum('status', ['planned', 'in_progress', 'completed', 'cancelled'])->default('planned');
            $table->string('pdf_path')->nullable();
            $table->text('notes')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();

            $table->index(['m_number']);
            $table->index(['manufacturing_bom_id', 'status']);
            $table->index(['warehouse_id', 'status']);
            $table->index(['status', 'due_date']);
            $table->index(['created_by']);
        });
    }

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