<?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('order_nodes', function (Blueprint $table) {
            $table->id()->comment('ID');
            $table->foreignId('tenant_id')->comment('테넌트 ID');
            $table->foreignId('order_id')->comment('수주 ID');

            // ---- 트리 구조 ----
            $table->foreignId('parent_id')->nullable()->comment('상위 노드 ID (NULL=루트)');

            // ---- 고정 코어 (통계/집계용) ----
            $table->string('node_type', 50)->comment('노드 유형 (location, zone, floor, room, process...)');
            $table->string('code', 100)->comment('식별 코드');
            $table->string('name', 200)->comment('표시명');
            $table->string('status_code', 30)->default('PENDING')
                ->comment('상태 (PENDING/CONFIRMED/IN_PRODUCTION/PRODUCED/SHIPPED/COMPLETED/CANCELLED)');
            $table->integer('quantity')->default(1)->comment('수량');
            $table->decimal('unit_price', 15, 2)->default(0)->comment('단가');
            $table->decimal('total_price', 15, 2)->default(0)->comment('합계');

            // ---- 유연 확장 (유형별 상세) ----
            $table->json('options')->nullable()->comment('유형별 동적 속성 JSON');

            // ---- 정렬 ----
            $table->integer('depth')->default(0)->comment('트리 깊이 (0=루트)');
            $table->integer('sort_order')->default(0)->comment('정렬 순서');

            // ---- 감사 ----
            $table->foreignId('created_by')->nullable()->comment('생성자 ID');
            $table->foreignId('updated_by')->nullable()->comment('수정자 ID');
            $table->foreignId('deleted_by')->nullable()->comment('삭제자 ID');
            $table->timestamps();
            $table->softDeletes();

            // ---- 인덱스 ----
            $table->index('tenant_id');
            $table->index('parent_id');
            $table->index(['order_id', 'depth', 'sort_order']);
            $table->index(['order_id', 'node_type']);
            $table->index(['tenant_id', 'node_type', 'status_code']); // 통계용
        });
    }

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