<?php

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

return new class extends Migration
{
    /**
     * 작업지시 이슈 테이블 (Work Order Issues)
     */
    public function up(): void
    {
        Schema::create('work_order_issues', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('work_order_id')->comment('작업지시ID');
            $table->string('title', 200)->comment('이슈 제목');
            $table->text('description')->nullable()->comment('이슈 설명');
            $table->string('priority', 20)->default('medium')->comment('우선순위: high/medium/low');
            $table->string('status', 30)->default('open')->comment('상태: open/in_progress/resolved');
            $table->unsignedBigInteger('reported_by')->nullable()->comment('보고자ID');
            $table->unsignedBigInteger('resolved_by')->nullable()->comment('해결자ID');
            $table->timestamp('resolved_at')->nullable()->comment('해결일시');
            $table->timestamps();

            // Foreign Keys
            $table->foreign('work_order_id')
                ->references('id')
                ->on('work_orders')
                ->onDelete('cascade');

            // Indexes
            $table->index(['work_order_id', 'status'], 'idx_work_order_issues_order_status');
            $table->index(['work_order_id', 'priority'], 'idx_work_order_issues_order_priority');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('work_order_issues');
    }
};
