<?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;

/**
 * 第 2 步：为仍仅按物料承载的明细表增加可空的 product_sku_id 与规格组合快照列（不回填、不改过账逻辑）。
 */
return new class extends Migration
{
    public function up(): void
    {
        $this->upgradePurchaseRequestItems();
        $this->upgradePurchaseInquiryItems();
        $this->upgradeSalesQuoteItems();
        $this->upgradePurchaseInquiryQuotes();
        $this->upgradeOtherStockInItems();
        $this->upgradeOtherStockOutItems();
        $this->upgradeTransferOrderItems();
        $this->upgradeStocktakeOrderItems();
        $this->upgradeStocktakeDifferences();
        $this->upgradePurchaseReturnOrderItems();
        $this->upgradeSalesReturnOrderItems();
    }

    public function down(): void
    {
        $this->revertSalesReturnOrderItems();
        $this->revertPurchaseReturnOrderItems();
        $this->revertStocktakeDifferences();
        $this->revertStocktakeOrderItems();
        $this->revertTransferOrderItems();
        $this->revertOtherStockOutItems();
        $this->revertOtherStockInItems();
        $this->revertPurchaseInquiryQuotes();
        $this->revertSalesQuoteItems();
        $this->revertPurchaseInquiryItems();
        $this->revertPurchaseRequestItems();
    }

    private function upgradePurchaseRequestItems(): void
    {
        if (! Schema::hasTable('dberp_purchase_request_items')) {
            return;
        }

        Schema::table('dberp_purchase_request_items', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_purchase_request_items', 'product_sku_id')) {
                $table->unsignedBigInteger('product_sku_id')->nullable()->after('product_id')->comment('规格组合');
                $table->string('product_sku_code', 64)->nullable()->after('product_sku_id')->comment('规格组合编码快照');
                $table->string('product_sku_name', 160)->nullable()->after('product_sku_code')->comment('规格组合展示名快照');
                $table->string('product_sku_spec', 512)->nullable()->after('product_sku_name')->comment('规格摘要快照');
                $table->foreign('product_sku_id', 'fk_dpri_psku')->references('id')->on('dberp_product_skus')->restrictOnDelete();
                $table->index(['product_sku_id'], 'idx_dpri_psku');
                $table->index(['product_id', 'product_sku_id'], 'idx_dpri_prod_psku');
                $table->index(['warehouse_id', 'product_id', 'product_sku_id'], 'idx_dpri_wh_prod_psku');
            }
        });
    }

    private function revertPurchaseRequestItems(): void
    {
        if (! Schema::hasTable('dberp_purchase_request_items') || ! Schema::hasColumn('dberp_purchase_request_items', 'product_sku_id')) {
            return;
        }

        Schema::table('dberp_purchase_request_items', function (Blueprint $table): void {
            $table->dropForeign('fk_dpri_psku');
            $table->dropIndex('idx_dpri_psku');
            $table->dropIndex('idx_dpri_prod_psku');
            $table->dropIndex('idx_dpri_wh_prod_psku');
            $table->dropColumn(['product_sku_id', 'product_sku_code', 'product_sku_name', 'product_sku_spec']);
        });
    }

    private function upgradeSalesQuoteItems(): void
    {
        if (! Schema::hasTable('dberp_sales_quote_items')) {
            return;
        }

        Schema::table('dberp_sales_quote_items', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_sales_quote_items', 'product_sku_id')) {
                $table->unsignedBigInteger('product_sku_id')->nullable()->after('product_id')->comment('规格组合');
                $table->string('product_sku_code', 64)->nullable()->after('product_sku_id')->comment('规格组合编码快照');
                $table->string('product_sku_name', 160)->nullable()->after('product_sku_code')->comment('规格组合展示名快照');
                $table->string('product_sku_spec', 512)->nullable()->after('product_sku_name')->comment('规格摘要快照');
                $table->foreign('product_sku_id', 'fk_dsqi_psku')->references('id')->on('dberp_product_skus')->restrictOnDelete();
                $table->index(['product_sku_id'], 'idx_dsqi_psku');
                $table->index(['product_id', 'product_sku_id'], 'idx_dsqi_prod_psku');
            }
        });
    }

    private function revertSalesQuoteItems(): void
    {
        if (! Schema::hasTable('dberp_sales_quote_items') || ! Schema::hasColumn('dberp_sales_quote_items', 'product_sku_id')) {
            return;
        }

        Schema::table('dberp_sales_quote_items', function (Blueprint $table): void {
            $table->dropForeign('fk_dsqi_psku');
            $table->dropIndex('idx_dsqi_psku');
            $table->dropIndex('idx_dsqi_prod_psku');
            $table->dropColumn(['product_sku_id', 'product_sku_code', 'product_sku_name', 'product_sku_spec']);
        });
    }

    private function upgradePurchaseInquiryItems(): void
    {
        if (! Schema::hasTable('dberp_purchase_inquiry_items')) {
            return;
        }

        Schema::table('dberp_purchase_inquiry_items', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_purchase_inquiry_items', 'product_sku_id')) {
                $table->unsignedBigInteger('product_sku_id')->nullable()->after('product_id')->comment('规格组合');
                $table->string('product_sku_code', 64)->nullable()->after('product_sku_id')->comment('规格组合编码快照');
                $table->string('product_sku_name', 160)->nullable()->after('product_sku_code')->comment('规格组合展示名快照');
                $table->string('product_sku_spec', 512)->nullable()->after('product_sku_name')->comment('规格摘要快照');
                $table->foreign('product_sku_id', 'fk_dpii_psku')->references('id')->on('dberp_product_skus')->restrictOnDelete();
                $table->index(['product_sku_id'], 'idx_dpii_psku');
                $table->index(['product_id', 'product_sku_id'], 'idx_dpii_prod_psku');
            }
        });
    }

    private function revertPurchaseInquiryItems(): void
    {
        if (! Schema::hasTable('dberp_purchase_inquiry_items') || ! Schema::hasColumn('dberp_purchase_inquiry_items', 'product_sku_id')) {
            return;
        }

        Schema::table('dberp_purchase_inquiry_items', function (Blueprint $table): void {
            $table->dropForeign('fk_dpii_psku');
            $table->dropIndex('idx_dpii_psku');
            $table->dropIndex('idx_dpii_prod_psku');
            $table->dropColumn(['product_sku_id', 'product_sku_code', 'product_sku_name', 'product_sku_spec']);
        });
    }

    private function upgradePurchaseInquiryQuotes(): void
    {
        if (! Schema::hasTable('dberp_purchase_inquiry_quotes')) {
            return;
        }

        Schema::table('dberp_purchase_inquiry_quotes', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_purchase_inquiry_quotes', 'product_sku_id')) {
                $table->unsignedBigInteger('product_sku_id')->nullable()->after('product_id')->comment('规格组合');
                $table->string('product_sku_code', 64)->nullable()->after('product_sku_id')->comment('规格组合编码快照');
                $table->string('product_sku_name', 160)->nullable()->after('product_sku_code')->comment('规格组合展示名快照');
                $table->string('product_sku_spec', 512)->nullable()->after('product_sku_name')->comment('规格摘要快照');
                $table->foreign('product_sku_id', 'fk_dpiq_psku')->references('id')->on('dberp_product_skus')->restrictOnDelete();
                $table->index(['product_sku_id'], 'idx_dpiq_psku');
                $table->index(['product_id', 'product_sku_id'], 'idx_dpiq_prod_psku');
            }
        });
    }

    private function revertPurchaseInquiryQuotes(): void
    {
        if (! Schema::hasTable('dberp_purchase_inquiry_quotes') || ! Schema::hasColumn('dberp_purchase_inquiry_quotes', 'product_sku_id')) {
            return;
        }

        Schema::table('dberp_purchase_inquiry_quotes', function (Blueprint $table): void {
            $table->dropForeign('fk_dpiq_psku');
            $table->dropIndex('idx_dpiq_psku');
            $table->dropIndex('idx_dpiq_prod_psku');
            $table->dropColumn(['product_sku_id', 'product_sku_code', 'product_sku_name', 'product_sku_spec']);
        });
    }

    private function upgradeOtherStockInItems(): void
    {
        if (! Schema::hasTable('dberp_other_stock_in_order_items')) {
            return;
        }

        Schema::table('dberp_other_stock_in_order_items', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_other_stock_in_order_items', 'product_sku_id')) {
                $table->unsignedBigInteger('product_sku_id')->nullable()->after('product_id')->comment('规格组合');
                $table->string('product_sku_code', 64)->nullable()->after('product_sku_id')->comment('规格组合编码快照');
                $table->string('product_sku_name', 160)->nullable()->after('product_sku_code')->comment('规格组合展示名快照');
                $table->string('product_sku_spec', 512)->nullable()->after('product_sku_name')->comment('规格摘要快照');
                $table->foreign('product_sku_id', 'fk_dosii_psku')->references('id')->on('dberp_product_skus')->restrictOnDelete();
                $table->index(['product_sku_id'], 'idx_dosii_psku');
                $table->index(['product_id', 'product_sku_id'], 'idx_dosii_prod_psku');
            }
        });
    }

    private function revertOtherStockInItems(): void
    {
        if (! Schema::hasTable('dberp_other_stock_in_order_items') || ! Schema::hasColumn('dberp_other_stock_in_order_items', 'product_sku_id')) {
            return;
        }

        Schema::table('dberp_other_stock_in_order_items', function (Blueprint $table): void {
            $table->dropForeign('fk_dosii_psku');
            $table->dropIndex('idx_dosii_psku');
            $table->dropIndex('idx_dosii_prod_psku');
            $table->dropColumn(['product_sku_id', 'product_sku_code', 'product_sku_name', 'product_sku_spec']);
        });
    }

    private function upgradeOtherStockOutItems(): void
    {
        if (! Schema::hasTable('dberp_other_stock_out_order_items')) {
            return;
        }

        Schema::table('dberp_other_stock_out_order_items', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_other_stock_out_order_items', 'product_sku_id')) {
                $table->unsignedBigInteger('product_sku_id')->nullable()->after('product_id')->comment('规格组合');
                $table->string('product_sku_code', 64)->nullable()->after('product_sku_id')->comment('规格组合编码快照');
                $table->string('product_sku_name', 160)->nullable()->after('product_sku_code')->comment('规格组合展示名快照');
                $table->string('product_sku_spec', 512)->nullable()->after('product_sku_name')->comment('规格摘要快照');
                $table->foreign('product_sku_id', 'fk_dosoi_psku')->references('id')->on('dberp_product_skus')->restrictOnDelete();
                $table->index(['product_sku_id'], 'idx_dosoi_psku');
                $table->index(['product_id', 'product_sku_id'], 'idx_dosoi_prod_psku');
            }
        });
    }

    private function revertOtherStockOutItems(): void
    {
        if (! Schema::hasTable('dberp_other_stock_out_order_items') || ! Schema::hasColumn('dberp_other_stock_out_order_items', 'product_sku_id')) {
            return;
        }

        Schema::table('dberp_other_stock_out_order_items', function (Blueprint $table): void {
            $table->dropForeign('fk_dosoi_psku');
            $table->dropIndex('idx_dosoi_psku');
            $table->dropIndex('idx_dosoi_prod_psku');
            $table->dropColumn(['product_sku_id', 'product_sku_code', 'product_sku_name', 'product_sku_spec']);
        });
    }

    private function upgradeTransferOrderItems(): void
    {
        if (! Schema::hasTable('dberp_transfer_order_items')) {
            return;
        }

        Schema::table('dberp_transfer_order_items', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_transfer_order_items', 'product_sku_id')) {
                $table->unsignedBigInteger('product_sku_id')->nullable()->after('product_id')->comment('规格组合');
                $table->string('product_sku_code', 64)->nullable()->after('product_sku_id')->comment('规格组合编码快照');
                $table->string('product_sku_name', 160)->nullable()->after('product_sku_code')->comment('规格组合展示名快照');
                $table->string('product_sku_spec', 512)->nullable()->after('product_sku_name')->comment('规格摘要快照');
                $table->foreign('product_sku_id', 'fk_dtoi_psku')->references('id')->on('dberp_product_skus')->restrictOnDelete();
                $table->index(['product_sku_id'], 'idx_dtoi_psku');
                $table->index(['product_id', 'product_sku_id'], 'idx_dtoi_prod_psku');
            }
        });
    }

    private function revertTransferOrderItems(): void
    {
        if (! Schema::hasTable('dberp_transfer_order_items') || ! Schema::hasColumn('dberp_transfer_order_items', 'product_sku_id')) {
            return;
        }

        Schema::table('dberp_transfer_order_items', function (Blueprint $table): void {
            $table->dropForeign('fk_dtoi_psku');
            $table->dropIndex('idx_dtoi_psku');
            $table->dropIndex('idx_dtoi_prod_psku');
            $table->dropColumn(['product_sku_id', 'product_sku_code', 'product_sku_name', 'product_sku_spec']);
        });
    }

    private function upgradeStocktakeOrderItems(): void
    {
        if (! Schema::hasTable('dberp_stocktake_order_items')) {
            return;
        }

        Schema::table('dberp_stocktake_order_items', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_stocktake_order_items', 'product_sku_id')) {
                $table->unsignedBigInteger('product_sku_id')->nullable()->after('product_id')->comment('规格组合');
                $table->string('product_sku_code', 64)->nullable()->after('product_sku_id')->comment('规格组合编码快照');
                $table->string('product_sku_name', 160)->nullable()->after('product_sku_code')->comment('规格组合展示名快照');
                $table->string('product_sku_spec', 512)->nullable()->after('product_sku_name')->comment('规格摘要快照');
                $table->foreign('product_sku_id', 'fk_dstki_psku')->references('id')->on('dberp_product_skus')->restrictOnDelete();
                $table->index(['product_sku_id'], 'idx_dstki_psku');
                $table->index(['product_id', 'product_sku_id'], 'idx_dstki_prod_psku');
            }
        });
    }

    private function revertStocktakeOrderItems(): void
    {
        if (! Schema::hasTable('dberp_stocktake_order_items') || ! Schema::hasColumn('dberp_stocktake_order_items', 'product_sku_id')) {
            return;
        }

        Schema::table('dberp_stocktake_order_items', function (Blueprint $table): void {
            $table->dropForeign('fk_dstki_psku');
            $table->dropIndex('idx_dstki_psku');
            $table->dropIndex('idx_dstki_prod_psku');
            $table->dropColumn(['product_sku_id', 'product_sku_code', 'product_sku_name', 'product_sku_spec']);
        });
    }

    private function upgradeStocktakeDifferences(): void
    {
        if (! Schema::hasTable('dberp_stocktake_differences')) {
            return;
        }

        Schema::table('dberp_stocktake_differences', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_stocktake_differences', 'product_sku_id')) {
                $table->unsignedBigInteger('product_sku_id')->nullable()->after('product_id')->comment('规格组合');
                $table->string('product_sku_code', 64)->nullable()->after('product_sku_id')->comment('规格组合编码快照');
                $table->string('product_sku_name', 160)->nullable()->after('product_sku_code')->comment('规格组合展示名快照');
                $table->string('product_sku_spec', 512)->nullable()->after('product_sku_name')->comment('规格摘要快照');
                $table->foreign('product_sku_id', 'fk_dstd_psku')->references('id')->on('dberp_product_skus')->restrictOnDelete();
                $table->index(['product_sku_id'], 'idx_dstd_psku');
                $table->index(['product_id', 'product_sku_id'], 'idx_dstd_prod_psku');
                $table->index(['warehouse_id', 'product_id', 'product_sku_id'], 'idx_dstd_wh_prod_psku');
            }
        });
    }

    private function revertStocktakeDifferences(): void
    {
        if (! Schema::hasTable('dberp_stocktake_differences') || ! Schema::hasColumn('dberp_stocktake_differences', 'product_sku_id')) {
            return;
        }

        Schema::table('dberp_stocktake_differences', function (Blueprint $table): void {
            $table->dropForeign('fk_dstd_psku');
            $table->dropIndex('idx_dstd_psku');
            $table->dropIndex('idx_dstd_prod_psku');
            $table->dropIndex('idx_dstd_wh_prod_psku');
            $table->dropColumn(['product_sku_id', 'product_sku_code', 'product_sku_name', 'product_sku_spec']);
        });
    }

    private function upgradePurchaseReturnOrderItems(): void
    {
        if (! Schema::hasTable('dberp_purchase_return_order_items')) {
            return;
        }

        Schema::table('dberp_purchase_return_order_items', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_purchase_return_order_items', 'product_sku_id')) {
                $table->unsignedBigInteger('product_sku_id')->nullable()->after('product_id')->comment('规格组合');
                $table->string('product_sku_code', 64)->nullable()->after('product_sku_id')->comment('规格组合编码快照');
                $table->string('product_sku_name', 160)->nullable()->after('product_sku_code')->comment('规格组合展示名快照');
                $table->string('product_sku_spec', 512)->nullable()->after('product_sku_name')->comment('规格摘要快照');
                $table->foreign('product_sku_id', 'fk_dproi_psku')->references('id')->on('dberp_product_skus')->restrictOnDelete();
                $table->index(['product_sku_id'], 'idx_dproi_psku');
                $table->index(['product_id', 'product_sku_id'], 'idx_dproi_prod_psku');
            }
        });
    }

    private function revertPurchaseReturnOrderItems(): void
    {
        if (! Schema::hasTable('dberp_purchase_return_order_items') || ! Schema::hasColumn('dberp_purchase_return_order_items', 'product_sku_id')) {
            return;
        }

        Schema::table('dberp_purchase_return_order_items', function (Blueprint $table): void {
            $table->dropForeign('fk_dproi_psku');
            $table->dropIndex('idx_dproi_psku');
            $table->dropIndex('idx_dproi_prod_psku');
            $table->dropColumn(['product_sku_id', 'product_sku_code', 'product_sku_name', 'product_sku_spec']);
        });
    }

    private function upgradeSalesReturnOrderItems(): void
    {
        if (! Schema::hasTable('dberp_sales_return_order_items')) {
            return;
        }

        Schema::table('dberp_sales_return_order_items', function (Blueprint $table): void {
            if (! Schema::hasColumn('dberp_sales_return_order_items', 'product_sku_id')) {
                $table->unsignedBigInteger('product_sku_id')->nullable()->after('product_id')->comment('规格组合');
                $table->string('product_sku_code', 64)->nullable()->after('product_sku_id')->comment('规格组合编码快照');
                $table->string('product_sku_name', 160)->nullable()->after('product_sku_code')->comment('规格组合展示名快照');
                $table->string('product_sku_spec', 512)->nullable()->after('product_sku_name')->comment('规格摘要快照');
                $table->foreign('product_sku_id', 'fk_dsroi_psku')->references('id')->on('dberp_product_skus')->restrictOnDelete();
                $table->index(['product_sku_id'], 'idx_dsroi_psku');
                $table->index(['product_id', 'product_sku_id'], 'idx_dsroi_prod_psku');
            }
        });
    }

    private function revertSalesReturnOrderItems(): void
    {
        if (! Schema::hasTable('dberp_sales_return_order_items') || ! Schema::hasColumn('dberp_sales_return_order_items', 'product_sku_id')) {
            return;
        }

        Schema::table('dberp_sales_return_order_items', function (Blueprint $table): void {
            $table->dropForeign('fk_dsroi_psku');
            $table->dropIndex('idx_dsroi_psku');
            $table->dropIndex('idx_dsroi_prod_psku');
            $table->dropColumn(['product_sku_id', 'product_sku_code', 'product_sku_name', 'product_sku_spec']);
        });
    }
};
