<?php

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

return new class extends Migration
{
    /**
     * 수주 상세(order_items)에 금액 및 품목 정보 필드 추가
     * - 견적 → 수주 전환 시 품목별 금액 정보 저장
     * - 생산/출하/정산 플로우에서 품목별 추적 가능
     */
    public function up(): void
    {
        Schema::table('order_items', function (Blueprint $table) {
            // 품목 정보 (quote_items와 동일 구조)
            $table->string('item_code', 50)->nullable()->after('item_id')->comment('품목코드');
            $table->string('item_name', 200)->nullable()->after('item_code')->comment('품명');
            $table->string('specification', 100)->nullable()->after('item_name')->comment('규격');
            $table->string('unit', 20)->default('EA')->after('specification')->comment('단위');

            // 금액 정보
            $table->decimal('unit_price', 15, 2)->default(0)->after('quantity')->comment('단가');
            $table->decimal('supply_amount', 15, 2)->default(0)->after('unit_price')->comment('공급가액');
            $table->decimal('tax_amount', 15, 2)->default(0)->after('supply_amount')->comment('세액');
            $table->decimal('total_amount', 15, 2)->default(0)->after('tax_amount')->comment('금액 (공급가액 + 세액)');

            // 할인 정보
            $table->decimal('discount_rate', 5, 2)->default(0)->after('total_amount')->comment('할인율 (%)');
            $table->decimal('discount_amount', 15, 2)->default(0)->after('discount_rate')->comment('할인금액');

            // 추가 정보
            $table->text('note')->nullable()->after('remarks')->comment('비고');
            $table->unsignedInteger('sort_order')->default(0)->after('note')->comment('정렬순서');

            // 견적 연결 (추적용)
            $table->unsignedBigInteger('quote_id')->nullable()->after('order_id')->comment('원본 견적 ID');
            $table->unsignedBigInteger('quote_item_id')->nullable()->after('quote_id')->comment('원본 견적 품목 ID');

            // 인덱스
            $table->index('item_code', 'idx_order_items_item_code');
            $table->index('quote_id', 'idx_order_items_quote_id');
            $table->index('sort_order', 'idx_order_items_sort_order');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('order_items', function (Blueprint $table) {
            // 인덱스 삭제
            $table->dropIndex('idx_order_items_item_code');
            $table->dropIndex('idx_order_items_quote_id');
            $table->dropIndex('idx_order_items_sort_order');

            // 컬럼 삭제
            $table->dropColumn([
                'item_code',
                'item_name',
                'specification',
                'unit',
                'unit_price',
                'supply_amount',
                'tax_amount',
                'total_amount',
                'discount_rate',
                'discount_amount',
                'note',
                'sort_order',
                'quote_id',
                'quote_item_id',
            ]);
        });
    }
};
