<?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_candidates', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('reference_code', 64);
            $table->string('first_name', 120);
            $table->string('last_name', 120);
            $table->string('email', 255)->nullable();
            $table->string('phone', 40)->nullable();
            $table->string('nationality', 100)->nullable();
            $table->string('source', 80)->nullable();
            $table->text('notes')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['tenant_id', 'reference_code']);
            $table->index(['tenant_id', 'email']);
            $table->index(['tenant_id', 'last_name', 'first_name']);
        });

        Schema::create('recruitment_candidate_cvs', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->foreignId('candidate_id')->constrained('recruitment_candidates')->cascadeOnDelete();
            $table->string('reference_code', 64);
            $table->string('file_path');
            $table->string('original_name', 255);
            $table->unsignedInteger('file_size')->default(0);
            $table->string('mime_type', 120)->nullable();
            $table->boolean('is_primary')->default(false);
            $table->text('notes')->nullable();
            $table->foreignId('uploaded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();

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

        Schema::create('recruitment_job_requests', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('reference_code', 64);
            $table->foreignId('department_id')->constrained('departments')->restrictOnDelete();
            $table->foreignId('position_id')->nullable()->constrained('positions')->nullOnDelete();
            $table->string('title', 200);
            $table->unsignedSmallInteger('openings')->default(1);
            $table->text('justification')->nullable();
            $table->string('status', 32)->default('draft');
            $table->foreignId('requested_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('reviewed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('reviewed_at')->nullable();
            $table->text('review_notes')->nullable();
            $table->timestamps();
            $table->softDeletes();

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

        Schema::create('recruitment_vacancies', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('reference_code', 64);
            $table->foreignId('job_request_id')->constrained('recruitment_job_requests')->restrictOnDelete();
            $table->foreignId('department_id')->constrained('departments')->restrictOnDelete();
            $table->foreignId('position_id')->nullable()->constrained('positions')->nullOnDelete();
            $table->string('title', 200);
            $table->text('description')->nullable();
            $table->unsignedSmallInteger('openings')->default(1);
            $table->string('status', 32)->default('draft');
            $table->timestamp('published_at')->nullable();
            $table->date('closes_at')->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', 'job_request_id']);
        });

        Schema::create('recruitment_applications', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('reference_code', 64);
            $table->foreignId('vacancy_id')->constrained('recruitment_vacancies')->restrictOnDelete();
            $table->foreignId('candidate_id')->constrained('recruitment_candidates')->restrictOnDelete();
            $table->foreignId('candidate_cv_id')->nullable()->constrained('recruitment_candidate_cvs')->nullOnDelete();
            $table->string('status', 32)->default('received');
            $table->string('source', 80)->nullable();
            $table->text('cover_letter')->nullable();
            $table->timestamp('applied_at')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['tenant_id', 'reference_code']);
            $table->unique(['vacancy_id', 'candidate_id', 'deleted_at'], 'recruitment_apps_vacancy_candidate_uq');
            $table->index(['tenant_id', 'status']);
            $table->index(['tenant_id', 'candidate_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('recruitment_applications');
        Schema::dropIfExists('recruitment_vacancies');
        Schema::dropIfExists('recruitment_job_requests');
        Schema::dropIfExists('recruitment_candidate_cvs');
        Schema::dropIfExists('recruitment_candidates');
    }
};
