<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * 部门、岗位及员工组织外键（合并原 departments、positions、employees FK 迁移）。
 */
return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('dberp_departments')) {
            Schema::create('dberp_departments', function (Blueprint $table): void {
                $table->id();
                $table->string('name', 120)->comment('部门名称');
                $table->string('code', 64)->nullable()->comment('部门编码');
                $table->foreignId('parent_id')->nullable()->constrained('dberp_departments')->nullOnDelete();
                $table->foreignId('manager_employee_id')->nullable()->constrained('dberp_employees')->nullOnDelete();
                $table->boolean('is_enabled')->default(true)->comment('启停状态');
                $table->unsignedInteger('sort_order')->default(0)->comment('排序（越小越靠前）');
                $table->text('remark')->nullable()->comment('备注');
                $table->timestamps();

                $table->unique('code', 'uq_dberp_department_code');
                $table->index(['parent_id', 'sort_order', 'id'], 'idx_dberp_dept_parent_sort');
                $table->index(['is_enabled', 'sort_order', 'id'], 'idx_dberp_dept_enabled_sort');
            });
        }

        if (! Schema::hasTable('dberp_positions')) {
            Schema::create('dberp_positions', function (Blueprint $table): void {
                $table->id();
                $table->string('name', 120)->comment('岗位名称');
                $table->string('code', 64)->nullable()->comment('岗位编码');
                $table->foreignId('department_id')->nullable()->constrained('dberp_departments')->restrictOnDelete();
                $table->boolean('is_enabled')->default(true)->comment('启停状态');
                $table->unsignedInteger('sort_order')->default(0)->comment('排序（越小越靠前）');
                $table->text('remark')->nullable()->comment('备注');
                $table->timestamps();

                $table->unique('code', 'uq_dberp_position_code');
                $table->index(['department_id', 'sort_order', 'id'], 'idx_dberp_pos_dept_sort');
                $table->index(['is_enabled', 'sort_order', 'id'], 'idx_dberp_pos_enabled_sort');
            });
        }

        if (Schema::hasTable('dberp_employees') && ! Schema::hasColumn('dberp_employees', 'department_id')) {
            Schema::table('dberp_employees', function (Blueprint $table): void {
                $table->foreignId('department_id')
                    ->nullable()
                    ->after('position_name')
                    ->constrained('dberp_departments')
                    ->nullOnDelete();
            });
        }

        if (Schema::hasTable('dberp_employees') && ! Schema::hasColumn('dberp_employees', 'position_id')) {
            Schema::table('dberp_employees', function (Blueprint $table): void {
                $table->foreignId('position_id')
                    ->nullable()
                    ->after('department_id')
                    ->constrained('dberp_positions')
                    ->nullOnDelete();
            });
        }
    }

    public function down(): void
    {
        Schema::table('dberp_employees', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('position_id');
            $table->dropConstrainedForeignId('department_id');
        });

        Schema::dropIfExists('dberp_positions');
        Schema::dropIfExists('dberp_departments');
    }
};
