<?php

declare(strict_types=1);

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');
    }
};
