<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

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::table('dberp_purchase_requests', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_purchase_requests', 'source_system')) {
                $table->string('source_system', 32)->default('dberp')->after('source_type')->comment('来源系统');
                $table->index(['source_system'], 'idx_dberp_preq_source_system');
            }

            if (! Schema::hasColumn('dberp_purchase_requests', 'external_reference_id')) {
                $table->string('external_reference_id', 128)->nullable()->after('source_reference_no')->comment('外部引用号');
                $table->index(['external_reference_id'], 'idx_dberp_preq_external_ref');
            }
        });

        Schema::table('dberp_purchase_request_items', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_purchase_request_items', 'external_product_id')) {
                $table->string('external_product_id', 128)->nullable()->after('product_id')->comment('外部商品 ID');
                $table->index(['external_product_id'], 'idx_dberp_preq_item_external_prod');
            }

            if (! Schema::hasColumn('dberp_purchase_request_items', 'external_sku')) {
                $table->string('external_sku', 128)->nullable()->after('external_product_id')->comment('外部 SKU');
                $table->index(['external_sku'], 'idx_dberp_preq_item_external_sku');
            }
        });

        Schema::table('dberp_purchase_inquiry_quotes', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_purchase_inquiry_quotes', 'source_system')) {
                $table->string('source_system', 32)->default('dberp')->after('purchase_request_code')->comment('来源系统');
                $table->index(['source_system'], 'idx_dpiq_source_system');
            }

            if (! Schema::hasColumn('dberp_purchase_inquiry_quotes', 'external_reference_id')) {
                $table->string('external_reference_id', 128)->nullable()->after('source_system')->comment('外部引用号');
                $table->index(['external_reference_id'], 'idx_dpiq_external_ref');
            }

            if (! Schema::hasColumn('dberp_purchase_inquiry_quotes', 'external_product_id')) {
                $table->string('external_product_id', 128)->nullable()->after('product_id')->comment('外部商品 ID');
                $table->index(['external_product_id'], 'idx_dpiq_external_prod');
            }

            if (! Schema::hasColumn('dberp_purchase_inquiry_quotes', 'external_sku')) {
                $table->string('external_sku', 128)->nullable()->after('external_product_id')->comment('外部 SKU');
                $table->index(['external_sku'], 'idx_dpiq_external_sku');
            }
        });

        Schema::table('dberp_purchase_orders', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_purchase_orders', 'source_system')) {
                $table->string('source_system', 32)->default('dberp')->after('purchase_request_code')->comment('来源系统');
                $table->index(['source_system'], 'idx_dberp_po_source_system');
            }

            if (! Schema::hasColumn('dberp_purchase_orders', 'external_reference_id')) {
                $table->string('external_reference_id', 128)->nullable()->after('source_system')->comment('外部引用号');
                $table->index(['external_reference_id'], 'idx_dberp_po_external_ref');
            }
        });

        Schema::table('dberp_purchase_order_items', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_purchase_order_items', 'external_product_id')) {
                $table->string('external_product_id', 128)->nullable()->after('product_id')->comment('外部商品 ID');
                $table->index(['external_product_id'], 'idx_dberp_poi_external_prod');
            }

            if (! Schema::hasColumn('dberp_purchase_order_items', 'external_sku')) {
                $table->string('external_sku', 128)->nullable()->after('external_product_id')->comment('外部 SKU');
                $table->index(['external_sku'], 'idx_dberp_poi_external_sku');
            }
        });
    }

    public function down(): void
    {
        Schema::table('dberp_purchase_order_items', function (Blueprint $table): void {
            if (Schema::hasColumn('dberp_purchase_order_items', 'external_sku')) {
                $table->dropIndex('idx_dberp_poi_external_sku');
                $table->dropColumn('external_sku');
            }

            if (Schema::hasColumn('dberp_purchase_order_items', 'external_product_id')) {
                $table->dropIndex('idx_dberp_poi_external_prod');
                $table->dropColumn('external_product_id');
            }
        });

        Schema::table('dberp_purchase_orders', function (Blueprint $table): void {
            if (Schema::hasColumn('dberp_purchase_orders', 'external_reference_id')) {
                $table->dropIndex('idx_dberp_po_external_ref');
                $table->dropColumn('external_reference_id');
            }

            if (Schema::hasColumn('dberp_purchase_orders', 'source_system')) {
                $table->dropIndex('idx_dberp_po_source_system');
                $table->dropColumn('source_system');
            }
        });

        Schema::table('dberp_purchase_inquiry_quotes', function (Blueprint $table): void {
            if (Schema::hasColumn('dberp_purchase_inquiry_quotes', 'external_sku')) {
                $table->dropIndex('idx_dpiq_external_sku');
                $table->dropColumn('external_sku');
            }

            if (Schema::hasColumn('dberp_purchase_inquiry_quotes', 'external_product_id')) {
                $table->dropIndex('idx_dpiq_external_prod');
                $table->dropColumn('external_product_id');
            }

            if (Schema::hasColumn('dberp_purchase_inquiry_quotes', 'external_reference_id')) {
                $table->dropIndex('idx_dpiq_external_ref');
                $table->dropColumn('external_reference_id');
            }

            if (Schema::hasColumn('dberp_purchase_inquiry_quotes', 'source_system')) {
                $table->dropIndex('idx_dpiq_source_system');
                $table->dropColumn('source_system');
            }
        });

        Schema::table('dberp_purchase_request_items', function (Blueprint $table): void {
            if (Schema::hasColumn('dberp_purchase_request_items', 'external_sku')) {
                $table->dropIndex('idx_dberp_preq_item_external_sku');
                $table->dropColumn('external_sku');
            }

            if (Schema::hasColumn('dberp_purchase_request_items', 'external_product_id')) {
                $table->dropIndex('idx_dberp_preq_item_external_prod');
                $table->dropColumn('external_product_id');
            }
        });

        Schema::table('dberp_purchase_requests', function (Blueprint $table): void {
            if (Schema::hasColumn('dberp_purchase_requests', 'external_reference_id')) {
                $table->dropIndex('idx_dberp_preq_external_ref');
                $table->dropColumn('external_reference_id');
            }

            if (Schema::hasColumn('dberp_purchase_requests', 'source_system')) {
                $table->dropIndex('idx_dberp_preq_source_system');
                $table->dropColumn('source_system');
            }
        });
    }
};