<?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('recruitment_interviews', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->index();
            $table->string('reference_code', 64);
            $table->foreignId('application_id')->constrained('recruitment_applications')->restrictOnDelete();
            $table->dateTime('scheduled_at');
            $table->string('mode', 20)->default('in_person');
            $table->string('location', 255)->nullable();
            $table->foreignId('interviewer_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('status', 30)->default('scheduled')->index();
            $table->text('notes')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['tenant_id', 'reference_code']);
            $table->index(['tenant_id', 'status']);
            $table->index(['tenant_id', 'application_id']);
        });

        Schema::create('recruitment_interview_scorecards', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->index();
            $table->string('reference_code', 64);
            $table->foreignId('interview_id')->constrained('recruitment_interviews')->restrictOnDelete();
            $table->unsignedTinyInteger('overall_score');
            $table->string('recommendation', 30);
            $table->text('strengths')->nullable();
            $table->text('weaknesses')->nullable();
            $table->text('notes')->nullable();
            $table->foreignId('scored_by')->nullable()->constrained('users')->nullOnDelete();
            $table->dateTime('scored_at')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['tenant_id', 'reference_code']);
            $table->index(['tenant_id', 'interview_id']);
            $table->index(['tenant_id', 'recommendation']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('recruitment_interview_scorecards');
        Schema::dropIfExists('recruitment_interviews');
    }
};
