<?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('hr_training_courses', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('code', 50);
            $table->string('name');
            $table->text('description')->nullable();
            $table->decimal('default_hours', 8, 2)->default(0);
            $table->decimal('default_cost', 14, 2)->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->unique(['tenant_id', 'code']);
        });
        Schema::create('hr_training_sessions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('course_id')->constrained('hr_training_courses')->cascadeOnDelete();
            $table->string('trainer')->nullable();
            $table->string('location')->nullable();
            $table->dateTime('starts_at');
            $table->dateTime('ends_at');
            $table->unsignedInteger('capacity')->nullable();
            $table->enum('status', ['planned', 'open', 'completed', 'cancelled'])->default('planned');
            $table->timestamps();
        });
        Schema::create('hr_training_enrollments', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('session_id')->constrained('hr_training_sessions')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->enum('status', ['enrolled', 'attended', 'completed', 'failed', 'cancelled'])->default('enrolled');
            $table->decimal('hours', 8, 2)->default(0);
            $table->decimal('evaluation_score', 5, 2)->nullable();
            $table->string('certificate_path')->nullable();
            $table->decimal('cost', 14, 2)->default(0);
            $table->timestamps();
            $table->unique(['session_id', 'employee_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('hr_training_enrollments');
        Schema::dropIfExists('hr_training_sessions');
        Schema::dropIfExists('hr_training_courses');
    }
};
