<?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('departments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('parent_id')->nullable()->constrained('departments')->nullOnDelete();
            $table->string('name', 50)->comment('部门名称');
            $table->string('code', 20)->unique()->comment('部门编码');
            $table->foreignId('manager_id')->nullable()->comment('部门负责人');
            $table->unsignedInteger('sort')->default(0)->comment('排序');
            $table->unsignedTinyInteger('status')->default(1)->comment('状态:1启用,0禁用');
            $table->timestamps();
            $table->softDeletes();

            $table->index('parent_id');
        });

        // 岗位表
        Schema::create('positions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('department_id')->constrained()->cascadeOnDelete();
            $table->string('name', 50)->comment('岗位名称');
            $table->string('code', 20)->unique()->comment('岗位编码');
            $table->unsignedInteger('level')->default(1)->comment('岗位级别');
            $table->string('category', 30)->nullable()->comment('岗位类别');
            $table->unsignedTinyInteger('status')->default(1)->comment('状态');
            $table->timestamps();
            $table->softDeletes();

            $table->index('department_id');
        });

        // 员工表
        Schema::create('employees', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('name', 50)->comment('姓名');
            $table->string('code', 20)->unique()->comment('工号');
            $table->string('gender', 10)->comment('性别');
            $table->string('id_card', 18)->unique()->comment('身份证号');
            $table->string('phone', 20)->comment('手机号');
            $table->string('email', 100)->nullable()->comment('邮箱');
            $table->foreignId('department_id')->constrained()->cascadeOnDelete();
            $table->foreignId('position_id')->constrained()->cascadeOnDelete();
            $table->date('hire_date')->comment('入职日期');
            $table->unsignedTinyInteger('status')->default(2)->comment('状态:1在职,2试用期,3离职,4待入职');
            $table->string('employment_type', 20)->default('full_time')->comment('用工类型');
            $table->decimal('base_salary', 12, 2)->default(0)->comment('基本工资');
            $table->string('address', 200)->nullable()->comment('住址');
            $table->string('emergency_contact', 50)->nullable()->comment('紧急联系人');
            $table->string('emergency_phone', 20)->nullable()->comment('紧急联系电话');
            $table->timestamps();
            $table->softDeletes();

            $table->index('department_id');
            $table->index('position_id');
            $table->index('status');
            $table->index('hire_date');
        });

        // 劳动合同表
        Schema::create('contracts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->unsignedTinyInteger('type')->comment('合同类型');
            $table->date('start_date')->comment('合同开始日期');
            $table->date('end_date')->nullable()->comment('合同结束日期');
            $table->date('sign_date')->nullable()->comment('签订日期');
            $table->unsignedInteger('term_months')->default(0)->comment('合同期限(月)');
            $table->unsignedInteger('renewal_count')->default(0)->comment('续签次数');
            $table->unsignedTinyInteger('status')->default(1)->comment('状态:1生效,2到期,3终止');
            $table->string('file_path', 500)->nullable()->comment('合同文件路径');
            $table->timestamps();
            $table->softDeletes();

            $table->index('employee_id');
            $table->index('status');
            $table->index('end_date');
        });

        // 员工调动记录
        Schema::create('employee_transfers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->foreignId('from_department_id')->constrained('departments');
            $table->foreignId('to_department_id')->constrained('departments');
            $table->foreignId('from_position_id')->constrained('positions');
            $table->foreignId('to_position_id')->constrained('positions');
            $table->date('effective_date')->comment('生效日期');
            $table->string('reason', 500)->nullable()->comment('调动原因');
            $table->unsignedTinyInteger('approval_status')->default(1)->comment('审批状态');
            $table->timestamps();
            $table->softDeletes();

            $table->index('employee_id');
            $table->index('effective_date');
        });

        // 员工离职记录
        Schema::create('employee_resignations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->string('type', 20)->comment('离职类型');
            $table->string('reason', 500)->nullable()->comment('离职原因');
            $table->date('apply_date')->comment('申请日期');
            $table->date('last_work_date')->comment('最后工作日');
            $table->unsignedTinyInteger('approval_status')->default(1)->comment('审批状态');
            $table->text('handover_note')->nullable()->comment('交接说明');
            $table->timestamps();
            $table->softDeletes();

            $table->index('employee_id');
            $table->index('apply_date');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('employee_resignations');
        Schema::dropIfExists('employee_transfers');
        Schema::dropIfExists('contracts');
        Schema::dropIfExists('employees');
        Schema::dropIfExists('positions');
        Schema::dropIfExists('departments');
    }
};
