<?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
    {
        Schema::create('dberp_salespersons', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('employee_id')->nullable()->constrained('dberp_employees')->nullOnDelete();
            $table->foreignId('department_id')->nullable()->constrained('dberp_departments')->nullOnDelete();
            $table->foreignId('position_id')->nullable()->constrained('dberp_positions')->nullOnDelete();
            $table->string('name', 120)->nullable()->comment('业务员展示名（无关联员工时使用）');
            $table->string('department_text', 120)->nullable()->comment('部门（旧版文本）');
            $table->string('position_text', 120)->nullable()->comment('岗位（旧版文本）');
            $table->string('phone', 32)->nullable()->comment('联系电话');
            $table->boolean('is_enabled')->default(true)->comment('启停状态');
            $table->unsignedInteger('sort_order')->default(100)->comment('排序（越小越靠前）');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->index(['employee_id', 'is_enabled'], 'idx_dberp_salesperson_emp_enabled');
            $table->index(['department_id', 'is_enabled'], 'idx_dberp_salesperson_dept_enabled');
            $table->index(['position_id', 'is_enabled'], 'idx_dberp_salesperson_pos_enabled');
            $table->index(['is_enabled', 'sort_order', 'id'], 'idx_dberp_salesperson_enabled_sort');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('dberp_salespersons');
    }
};
