<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('quality_documents', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained();
            $table->string('quality_doc_number', 30)->comment('품질관리서 번호');
            $table->string('site_name')->comment('현장명');
            $table->string('status', 20)->default('received')->comment('received/in_progress/completed');
            $table->foreignId('client_id')->nullable()->constrained('clients')->comment('수주처');
            $table->foreignId('inspector_id')->nullable()->constrained('users')->comment('검사자');
            $table->date('received_date')->nullable()->comment('접수일');
            $table->json('options')->nullable()->comment('관련자정보, 검사정보, 현장주소 등');
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->unsignedBigInteger('deleted_by')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['tenant_id', 'quality_doc_number']);
            $table->index(['tenant_id', 'status']);
            $table->index(['tenant_id', 'client_id']);
            $table->index(['tenant_id', 'inspector_id']);
            $table->index(['tenant_id', 'received_date']);
        });
    }

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