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

/**
 * 客户/供应商分类及主数据外键（合并原 customer_categories、supplier_categories、add_category_fk 迁移）。
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('dberp_customer_categories', function (Blueprint $table): void {
            $table->id();
            $table->string('code', 64)->comment('分类编码');
            $table->string('name', 120)->comment('分类名称');
            $table->string('status', 32)->default('active')->comment('状态（active=启用，inactive=停用）');
            $table->unsignedInteger('sort')->default(0)->comment('排序（越小越靠前）');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->unique('code', 'uq_dberp_customer_category_code');
            $table->index('status', 'idx_dberp_customer_category_status');
            $table->index('sort', 'idx_dberp_customer_category_sort');
        });

        Schema::create('dberp_supplier_categories', function (Blueprint $table): void {
            $table->id();
            $table->string('code', 64)->comment('分类编码');
            $table->string('name', 120)->comment('分类名称');
            $table->string('status', 32)->default('active')->comment('状态（active=启用，inactive=停用）');
            $table->unsignedInteger('sort')->default(0)->comment('排序（越小越靠前）');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->unique('code', 'uq_dberp_supplier_category_code');
            $table->index('status', 'idx_dberp_supplier_category_status');
            $table->index('sort', 'idx_dberp_supplier_category_sort');
        });

        Schema::table('dberp_customers', function (Blueprint $table): void {
            $table->foreignId('customer_category_id')
                ->nullable()
                ->after('name')
                ->comment('客户分类')
                ->constrained('dberp_customer_categories')
                ->nullOnDelete();

            $table->index('customer_category_id', 'idx_dberp_customers_customer_category');
        });

        Schema::table('dberp_suppliers', function (Blueprint $table): void {
            $table->foreignId('supplier_category_id')
                ->nullable()
                ->after('name')
                ->comment('供应商分类')
                ->constrained('dberp_supplier_categories')
                ->nullOnDelete();

            $table->index('supplier_category_id', 'idx_dberp_suppliers_supplier_category');
        });
    }

    public function down(): void
    {
        Schema::table('dberp_suppliers', function (Blueprint $table): void {
            $table->dropForeign(['supplier_category_id']);
            $table->dropIndex('idx_dberp_suppliers_supplier_category');
            $table->dropColumn('supplier_category_id');
        });

        Schema::table('dberp_customers', function (Blueprint $table): void {
            $table->dropForeign(['customer_category_id']);
            $table->dropIndex('idx_dberp_customers_customer_category');
            $table->dropColumn('customer_category_id');
        });

        Schema::dropIfExists('dberp_supplier_categories');
        Schema::dropIfExists('dberp_customer_categories');
    }
};
