<?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::table('recruitment_applications', function (Blueprint $table) {
            $table->foreignId('employee_id')->nullable()->after('created_by')
                ->constrained('employees')->nullOnDelete();
            $table->dateTime('hired_at')->nullable()->after('employee_id');
        });

        Schema::table('recruitment_candidates', function (Blueprint $table) {
            $table->foreignId('employee_id')->nullable()->after('updated_by')
                ->constrained('employees')->nullOnDelete();
            $table->dateTime('hired_at')->nullable()->after('employee_id');
        });

        Schema::table('recruitment_offer_letters', function (Blueprint $table) {
            $table->foreignId('employee_id')->nullable()->after('created_by')
                ->constrained('employees')->nullOnDelete();
            $table->dateTime('hired_at')->nullable()->after('employee_id');
        });

        Schema::table('recruitment_vacancies', function (Blueprint $table) {
            $table->unsignedInteger('filled_positions')->default(0)->after('openings');
        });

        Schema::create('recruitment_hirings', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->index();
            $table->string('reference_code', 64);
            $table->foreignId('offer_letter_id')->constrained('recruitment_offer_letters')->restrictOnDelete();
            $table->foreignId('application_id')->constrained('recruitment_applications')->restrictOnDelete();
            $table->foreignId('candidate_id')->constrained('recruitment_candidates')->restrictOnDelete();
            $table->foreignId('vacancy_id')->constrained('recruitment_vacancies')->restrictOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->restrictOnDelete();
            $table->foreignId('employment_contract_id')->nullable()
                ->constrained('employment_contracts')->nullOnDelete();
            $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('hired_by')->nullable()->constrained('users')->nullOnDelete();
            $table->dateTime('hired_at');
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['tenant_id', 'reference_code'], 'rhire_tenant_ref_uq');
            $table->unique(['tenant_id', 'offer_letter_id'], 'rhire_tenant_offer_uq');
            $table->index(['tenant_id', 'employee_id'], 'rhire_tenant_emp_idx');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('recruitment_hirings');

        Schema::table('recruitment_vacancies', function (Blueprint $table) {
            $table->dropColumn('filled_positions');
        });

        Schema::table('recruitment_offer_letters', function (Blueprint $table) {
            $table->dropConstrainedForeignId('employee_id');
            $table->dropColumn('hired_at');
        });

        Schema::table('recruitment_candidates', function (Blueprint $table) {
            $table->dropConstrainedForeignId('employee_id');
            $table->dropColumn('hired_at');
        });

        Schema::table('recruitment_applications', function (Blueprint $table) {
            $table->dropConstrainedForeignId('employee_id');
            $table->dropColumn('hired_at');
        });
    }
};
