<?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('work_results', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->comment('테넌트 ID');
            $table->foreignId('work_order_id')->constrained()->comment('작업지시 ID');
            $table->foreignId('work_order_item_id')->nullable()->constrained()->comment('작업지시 품목 ID');

            $table->string('lot_no', 50)->comment('로트번호');
            $table->date('work_date')->comment('작업일');
            $table->enum('process_type', ['screen', 'slat', 'bending'])->comment('공정구분');
            $table->string('product_name', 200)->comment('품목명');
            $table->string('specification', 100)->nullable()->comment('규격');

            $table->integer('production_qty')->default(0)->comment('생산수량');
            $table->integer('good_qty')->default(0)->comment('양품수량');
            $table->integer('defect_qty')->default(0)->comment('불량수량');
            $table->decimal('defect_rate', 5, 2)->default(0)->comment('불량률 (%)');

            $table->boolean('is_inspected')->default(false)->comment('검사 완료 여부');
            $table->boolean('is_packaged')->default(false)->comment('포장 완료 여부');

            $table->foreignId('worker_id')->nullable()->constrained('users')->comment('작업자 ID');
            $table->text('memo')->nullable()->comment('비고');

            $table->foreignId('created_by')->nullable()->constrained('users')->comment('생성자');
            $table->foreignId('updated_by')->nullable()->constrained('users')->comment('수정자');
            $table->timestamps();
            $table->softDeletes();

            // 인덱스
            $table->index(['tenant_id', 'work_date']);
            $table->index(['tenant_id', 'process_type']);
            $table->index(['tenant_id', 'work_order_id']);
            $table->index('lot_no');
        });
    }

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