<?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_offer_letters', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->index();
            $table->string('reference_code', 64);
            $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('department_id')->nullable()->constrained('departments')->nullOnDelete();
            $table->foreignId('position_id')->nullable()->constrained('positions')->nullOnDelete();
            $table->string('employment_type', 40);
            $table->foreignId('employment_status_id')->nullable()->constrained('employment_statuses')->nullOnDelete();
            $table->date('start_date');
            $table->unsignedSmallInteger('probation_period_days')->nullable();
            $table->decimal('salary', 14, 2);
            $table->char('currency', 3)->default('USD');
            $table->text('allowances')->nullable();
            $table->text('benefits')->nullable();
            $table->string('working_hours', 120)->nullable();
            $table->text('notes')->nullable();
            $table->string('status', 30)->default('draft')->index();
            $table->dateTime('expires_at')->nullable();
            $table->dateTime('acceptance_deadline_at')->nullable();
            $table->dateTime('submitted_at')->nullable();
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->dateTime('approved_at')->nullable();
            $table->foreignId('sent_by')->nullable()->constrained('users')->nullOnDelete();
            $table->dateTime('sent_at')->nullable();
            $table->unsignedSmallInteger('sent_count')->default(0);
            $table->dateTime('accepted_at')->nullable();
            $table->dateTime('rejected_at')->nullable();
            $table->dateTime('expired_at')->nullable();
            $table->dateTime('withdrawn_at')->nullable();
            $table->text('decision_notes')->nullable();
            // Future-ready: versioning / templates / delivery / e-sign (nullable until used)
            $table->unsignedSmallInteger('version')->default(1);
            $table->foreignId('parent_offer_id')->nullable()->constrained('recruitment_offer_letters')->nullOnDelete();
            $table->boolean('is_current')->default(true);
            $table->string('template_key', 80)->nullable();
            $table->string('template_version', 40)->nullable();
            $table->string('pdf_path', 500)->nullable();
            $table->string('signed_pdf_path', 500)->nullable();
            $table->dateTime('email_sent_at')->nullable();
            $table->string('email_to', 255)->nullable();
            $table->string('email_message_id', 191)->nullable();
            $table->string('email_status', 40)->nullable();
            $table->string('esign_provider', 60)->nullable();
            $table->string('esign_envelope_id', 191)->nullable();
            $table->string('esign_status', 40)->nullable();
            $table->dateTime('signed_at')->nullable();
            $table->string('signed_by_name', 191)->nullable();
            $table->json('signature_meta')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['tenant_id', 'reference_code'], 'roffer_tenant_ref_uq');
            $table->index(['tenant_id', 'status'], 'roffer_tenant_status_idx');
            $table->index(['tenant_id', 'application_id'], 'roffer_tenant_app_idx');
            $table->index(['tenant_id', 'candidate_id'], 'roffer_tenant_cand_idx');
            $table->index(['tenant_id', 'acceptance_deadline_at'], 'roffer_tenant_deadline_idx');
        });

        Schema::create('recruitment_offer_letter_status_events', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->index();
            $table->foreignId('offer_letter_id')->constrained('recruitment_offer_letters')->cascadeOnDelete();
            $table->string('from_status', 30)->nullable();
            $table->string('to_status', 30);
            $table->string('action', 40);
            $table->text('notes')->nullable();
            $table->foreignId('actor_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->json('meta')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['tenant_id', 'offer_letter_id'], 'roffer_evt_tenant_offer_idx');
        });

        Schema::create('recruitment_offer_letter_attachments', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->index();
            $table->foreignId('offer_letter_id')->constrained('recruitment_offer_letters')->cascadeOnDelete();
            $table->string('file_path', 500);
            $table->string('original_name', 255);
            $table->unsignedBigInteger('file_size')->default(0);
            $table->string('mime_type', 120)->nullable();
            $table->foreignId('uploaded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['tenant_id', 'offer_letter_id'], 'roffer_att_tenant_offer_idx');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('recruitment_offer_letter_attachments');
        Schema::dropIfExists('recruitment_offer_letter_status_events');
        Schema::dropIfExists('recruitment_offer_letters');
    }
};
