<?php

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('evaluation_periods', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->date('start_date');
            $table->date('end_date');
            $table->enum('type', ['annual', 'semi_annual', 'quarterly', 'probationary'])->default('annual');
            $table->enum('status', ['upcoming', 'active', 'closed'])->default('upcoming');
            $table->timestamps();
        });

        Schema::create('evaluation_criteria', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->text('description')->nullable();
            $table->string('category')->nullable();
            $table->integer('max_score')->default(100);
            $table->decimal('weight', 5, 2)->default(1.00);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('performance_evaluations', function (Blueprint $table) {
            $table->id();
            $table->string('eval_number')->unique();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->foreignId('evaluator_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('period_id')->constrained('evaluation_periods')->cascadeOnDelete();
            $table->decimal('total_score', 5, 2)->nullable();
            $table->decimal('weighted_score', 5, 2)->nullable();
            $table->enum('rating', ['excellent', 'very_good', 'satisfactory', 'needs_improvement', 'unsatisfactory'])->nullable();
            $table->text('evaluator_comments')->nullable();
            $table->text('employee_comments')->nullable();
            $table->text('development_goals')->nullable();
            $table->text('training_needs')->nullable();
            $table->enum('status', ['draft', 'submitted', 'employee_acknowledged', 'hr_reviewed', 'completed'])->default('draft');
            $table->timestamp('submitted_at')->nullable();
            $table->timestamp('acknowledged_at')->nullable();
            $table->timestamp('hr_reviewed_at')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->foreignId('hr_reviewer_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
        });

        Schema::create('evaluation_scores', function (Blueprint $table) {
            $table->id();
            $table->foreignId('evaluation_id')->constrained('performance_evaluations')->cascadeOnDelete();
            $table->foreignId('criteria_id')->constrained('evaluation_criteria')->cascadeOnDelete();
            $table->integer('score')->default(0);
            $table->text('comments')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('evaluation_scores');
        Schema::dropIfExists('performance_evaluations');
        Schema::dropIfExists('evaluation_criteria');
        Schema::dropIfExists('evaluation_periods');
    }
};
