<?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('performance_plans', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100)->comment('方案名称');
            $table->string('cycle', 20)->default('monthly')->comment('考核周期');
            $table->date('start_date')->comment('开始日期');
            $table->date('end_date')->comment('结束日期');
            $table->string('scoring_method', 30)->default('kpi')->comment('评分方式');
            $table->unsignedTinyInteger('status')->default(1)->comment('状态');
            $table->timestamps();
            $table->softDeletes();

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

        // 绩效指标
        Schema::create('performance_indicators', function (Blueprint $table) {
            $table->id();
            $table->foreignId('plan_id')->constrained('performance_plans')->cascadeOnDelete();
            $table->string('name', 100)->comment('指标名称');
            $table->decimal('weight', 5, 2)->default(0)->comment('权重(%)');
            $table->string('target_value', 100)->nullable()->comment('目标值');
            $table->text('scoring_rule')->nullable()->comment('评分规则');
            $table->unsignedInteger('sort')->default(0)->comment('排序');
            $table->timestamps();

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

        // 绩效考核
        Schema::create('performance_appraisals', function (Blueprint $table) {
            $table->id();
            $table->foreignId('plan_id')->constrained('performance_plans')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->decimal('self_score', 4, 1)->nullable()->comment('自评分数');
            $table->decimal('manager_score', 4, 1)->nullable()->comment('上级评分');
            $table->decimal('hr_score', 4, 1)->nullable()->comment('HR评分');
            $table->decimal('final_score', 4, 1)->nullable()->comment('最终得分');
            $table->string('level', 10)->nullable()->comment('绩效等级');
            $table->unsignedTinyInteger('status')->default(1)->comment('状态');
            $table->timestamps();

            $table->unique(['plan_id', 'employee_id'], 'uk_plan_employee');
            $table->index('status');
        });

        // 指标评分
        Schema::create('performance_indicator_scores', function (Blueprint $table) {
            $table->id();
            $table->foreignId('appraisal_id')->constrained('performance_appraisals')->cascadeOnDelete();
            $table->foreignId('indicator_id')->constrained('performance_indicators')->cascadeOnDelete();
            $table->decimal('score', 4, 1)->default(0)->comment('得分');
            $table->text('evidence')->nullable()->comment('评分依据');
            $table->timestamps();

            $table->index('appraisal_id');
            $table->index('indicator_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('performance_indicator_scores');
        Schema::dropIfExists('performance_appraisals');
        Schema::dropIfExists('performance_indicators');
        Schema::dropIfExists('performance_plans');
    }
};
