<?php

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

/**
 * 매출-수주 연동을 위한 컬럼 추가
 *
 * - order_id: 수주 연결
 * - shipment_id: 출하 연결
 * - source_type: 매출 생성 시점 (order_confirm/shipment_complete)
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('sales', function (Blueprint $table) {
            $table->unsignedBigInteger('order_id')->nullable()->after('tenant_id')->comment('수주 ID');
            $table->unsignedBigInteger('shipment_id')->nullable()->after('order_id')->comment('출하 ID');
            $table->string('source_type', 30)->nullable()->after('status')->comment('매출 생성 시점: order_confirm/shipment_complete/manual');

            // 인덱스
            $table->index('order_id', 'idx_sales_order_id');
            $table->index('shipment_id', 'idx_sales_shipment_id');
            $table->index('source_type', 'idx_sales_source_type');
        });
    }

    public function down(): void
    {
        Schema::table('sales', function (Blueprint $table) {
            $table->dropIndex('idx_sales_order_id');
            $table->dropIndex('idx_sales_shipment_id');
            $table->dropIndex('idx_sales_source_type');

            $table->dropColumn(['order_id', 'shipment_id', 'source_type']);
        });
    }
};
