<?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('employees', function (Blueprint $table) {
            $table->dateTime('onboarding_completed_at')->nullable()->after('confirmation_date');
        });

        Schema::create('recruitment_onboarding_cases', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->index();
            $table->string('reference_code', 64);
            $table->foreignId('hiring_id')->nullable()
                ->constrained('recruitment_hirings')->nullOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->restrictOnDelete();
            $table->foreignId('application_id')->nullable()
                ->constrained('recruitment_applications')->nullOnDelete();
            $table->foreignId('candidate_id')->nullable()
                ->constrained('recruitment_candidates')->nullOnDelete();
            $table->foreignId('vacancy_id')->nullable()
                ->constrained('recruitment_vacancies')->nullOnDelete();
            $table->string('status', 32)->default('in_progress');
            $table->unsignedTinyInteger('progress_percent')->default(0);
            $table->foreignId('started_by')->nullable()->constrained('users')->nullOnDelete();
            $table->dateTime('started_at')->nullable();
            $table->foreignId('completed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->dateTime('completed_at')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['tenant_id', 'reference_code'], 'ronb_tenant_ref_uq');
            $table->unique(['tenant_id', 'employee_id'], 'ronb_tenant_employee_uq');
            $table->index(['tenant_id', 'status'], 'ronb_tenant_status_idx');
        });

        Schema::create('recruitment_onboarding_tasks', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->index();
            $table->foreignId('onboarding_case_id')
                ->constrained('recruitment_onboarding_cases')->cascadeOnDelete();
            $table->string('section', 32);
            $table->string('title');
            $table->text('description')->nullable();
            $table->string('status', 32)->default('pending');
            $table->boolean('is_required')->default(true);
            $table->foreignId('assigned_to')->nullable()->constrained('users')->nullOnDelete();
            $table->date('due_date')->nullable();
            $table->dateTime('completed_at')->nullable();
            $table->foreignId('completed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->text('notes')->nullable();
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->json('meta')->nullable();
            $table->timestamps();

            $table->index(['onboarding_case_id', 'section'], 'ronbt_case_section_idx');
            $table->index(['tenant_id', 'status'], 'ronbt_tenant_status_idx');
        });

        Schema::create('recruitment_onboarding_attachments', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->index();
            $table->foreignId('onboarding_task_id')
                ->constrained('recruitment_onboarding_tasks')->cascadeOnDelete();
            $table->string('disk', 32)->default('local');
            $table->string('path');
            $table->string('original_name');
            $table->string('mime', 120)->nullable();
            $table->unsignedBigInteger('size')->default(0);
            $table->foreignId('uploaded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('recruitment_onboarding_attachments');
        Schema::dropIfExists('recruitment_onboarding_tasks');
        Schema::dropIfExists('recruitment_onboarding_cases');
        Schema::table('employees', function (Blueprint $table) {
            $table->dropColumn('onboarding_completed_at');
        });
    }
};
