<?php

declare(strict_types=1);

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

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('dberp_purchase_exceptions')) {
            Schema::create('dberp_purchase_exceptions', function (Blueprint $table): void {
                $table->id();
                $table->string('code', 64)->unique('uq_dberp_purchase_exception_code')->comment('采购异常编号');
                $table->foreignId('purchase_order_id')->nullable()->constrained('dberp_purchase_orders')->nullOnDelete();
                $table->string('purchase_order_code', 64)->nullable()->comment('采购订单号');
                $table->foreignId('purchase_order_item_id')->nullable()->constrained('dberp_purchase_order_items')->nullOnDelete();
                $table->foreignId('purchase_receipt_order_id')->nullable()->constrained('dberp_purchase_receipt_orders')->nullOnDelete();
                $table->string('purchase_receipt_code', 64)->nullable()->comment('到货单号');
                $table->foreignId('purchase_receipt_item_id')->nullable()->unique('uq_dberp_purchase_exception_receipt_item')->constrained('dberp_purchase_receipt_order_items')->nullOnDelete();
                $table->foreignId('supplier_id')->nullable()->constrained('dberp_suppliers')->nullOnDelete();
                $table->foreignId('product_id')->nullable()->constrained('dberp_products')->nullOnDelete();
                $table->string('exception_type', 32)->comment('异常类型');
                $table->string('status', 32)->default('pending')->comment('异常状态');
                $table->string('resolution_result', 32)->nullable()->comment('处理结果');
                $table->decimal('arrival_quantity', 18, 4)->default(0)->comment('本次实到数量');
                $table->decimal('received_quantity', 18, 4)->default(0)->comment('本次接收数量');
                $table->decimal('rejected_quantity', 18, 4)->default(0)->comment('本次拒收数量');
                $table->decimal('difference_quantity', 18, 4)->default(0)->comment('本次差异数量');
                $table->decimal('pending_quantity', 18, 4)->default(0)->comment('待处理数量');
                $table->text('reason_detail')->nullable()->comment('异常原因说明');
                $table->text('handling_suggestion')->nullable()->comment('处理建议');
                $table->text('resolution_remark')->nullable()->comment('处理结果说明');
                $table->unsignedBigInteger('handled_by')->nullable()->comment('处理人');
                $table->dateTime('occurred_at')->nullable()->comment('发现时间');
                $table->dateTime('handled_at')->nullable()->comment('处理时间');
                $table->dateTime('closed_at')->nullable()->comment('关闭时间');
                $table->json('context_snapshot')->nullable()->comment('上下文快照');
                $table->unsignedInteger('sort_order')->default(0)->comment('排序');
                $table->timestamps();

                $table->index(['status', 'resolution_result'], 'idx_dppe_status_result');
                $table->index(['supplier_id', 'status', 'created_at'], 'idx_dppe_supplier_status_created');
                $table->index(['purchase_order_id', 'status'], 'idx_dppe_order_status');
                $table->index(['purchase_receipt_order_id', 'status'], 'idx_dppe_receipt_status');
            });
        }

        if (Schema::hasTable('dberp_purchase_receipt_order_items')) {
            Schema::table('dberp_purchase_receipt_order_items', function (Blueprint $table): void {
                if (! Schema::hasColumn('dberp_purchase_receipt_order_items', 'exception_type')) {
                    $table->string('exception_type', 32)->nullable()->after('received_quantity')->comment('异常类型');
                }

                if (! Schema::hasColumn('dberp_purchase_receipt_order_items', 'exception_reason_detail')) {
                    $table->text('exception_reason_detail')->nullable()->after('exception_type')->comment('异常原因说明');
                }

                if (! Schema::hasColumn('dberp_purchase_receipt_order_items', 'handling_suggestion')) {
                    $table->text('handling_suggestion')->nullable()->after('exception_reason_detail')->comment('处理建议');
                }
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('dberp_purchase_receipt_order_items')) {
            Schema::table('dberp_purchase_receipt_order_items', function (Blueprint $table): void {
                $columns = array_values(array_filter([
                    Schema::hasColumn('dberp_purchase_receipt_order_items', 'exception_type') ? 'exception_type' : null,
                    Schema::hasColumn('dberp_purchase_receipt_order_items', 'exception_reason_detail') ? 'exception_reason_detail' : null,
                    Schema::hasColumn('dberp_purchase_receipt_order_items', 'handling_suggestion') ? 'handling_suggestion' : null,
                ]));

                if ($columns !== []) {
                    $table->dropColumn($columns);
                }
            });
        }

        Schema::dropIfExists('dberp_purchase_exceptions');
    }
};