<?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('production_orders', function (Blueprint $table) {
            $table->id();
            $table->string('code', 50)->unique()->comment('生產單號 (如: PRO-20260121-001)');
            
            // 成品資訊
            $table->foreignId('product_id')->constrained()->onDelete('restrict')
                ->comment('成品商品');
            $table->string('output_batch_number', 50)->comment('成品批號');
            $table->string('output_box_count', 10)->nullable()->comment('成品箱數');
            $table->decimal('output_quantity', 10, 2)->comment('生產數量');
            
            // 入庫倉庫
            $table->foreignId('warehouse_id')->constrained()->onDelete('restrict')
                ->comment('入庫倉庫');
            
            // 生產資訊
            $table->date('production_date')->comment('生產日期');
            $table->date('expiry_date')->nullable()->comment('成品效期');
            
            // 操作人員
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete()
                ->comment('操作人員');
            
            // 狀態與備註
            $table->enum('status', ['draft', 'completed', 'cancelled'])
                ->default('completed')->comment('狀態：草稿/完成/取消');
            $table->text('remark')->nullable()->comment('備註');
            
            $table->timestamps();
            
            // 索引
            $table->index(['production_date', 'product_id']);
        });
    }

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