<?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('recruitment_plans', function (Blueprint $table) {
            $table->id();
            $table->foreignId('department_id')->constrained()->cascadeOnDelete();
            $table->foreignId('position_id')->constrained()->cascadeOnDelete();
            $table->unsignedInteger('count')->default(1)->comment('招聘人数');
            $table->string('salary_range', 50)->nullable()->comment('薪资范围');
            $table->text('requirements')->nullable()->comment('岗位要求');
            $table->date('deadline')->nullable()->comment('截止日期');
            $table->unsignedTinyInteger('status')->default(1)->comment('状态');
            $table->timestamps();
            $table->softDeletes();

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

        // 简历库
        Schema::create('resumes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('recruitment_plan_id')->nullable()->constrained()->nullOnDelete();
            $table->string('name', 50)->comment('姓名');
            $table->string('phone', 20)->comment('手机号');
            $table->string('email', 100)->nullable()->comment('邮箱');
            $table->string('position_applied', 50)->nullable()->comment('应聘岗位');
            $table->unsignedInteger('source_channel_id')->nullable()->comment('来源渠道');
            $table->string('education', 20)->nullable()->comment('学历');
            $table->unsignedInteger('work_years')->default(0)->comment('工作年限');
            $table->unsignedTinyInteger('status')->default(1)->comment('状态');
            $table->string('resume_file', 500)->nullable()->comment('简历文件');
            $table->timestamps();
            $table->softDeletes();

            $table->index('phone');
            $table->index('status');
            $table->index('source_channel_id');
        });

        // 面试记录
        Schema::create('interviews', function (Blueprint $table) {
            $table->id();
            $table->foreignId('resume_id')->constrained()->cascadeOnDelete();
            $table->foreignId('interviewer_id')->nullable()->constrained('employees')->nullOnDelete();
            $table->unsignedInteger('round')->default(1)->comment('面试轮次');
            $table->string('type', 20)->default('technical')->comment('面试类型');
            $table->dateTime('scheduled_at')->comment('面试时间');
            $table->string('result', 20)->nullable()->comment('面试结果');
            $table->decimal('score', 3, 1)->nullable()->comment('评分');
            $table->text('feedback')->nullable()->comment('面试反馈');
            $table->timestamps();
            $table->softDeletes();

            $table->index('resume_id');
            $table->index('interviewer_id');
            $table->index('scheduled_at');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('interviews');
        Schema::dropIfExists('resumes');
        Schema::dropIfExists('recruitment_plans');
    }
};
