<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * Job demands / recruitment requisitions (§15) — the order an employer places
 * with the agency. Applicants are matched against these, so the record carries
 * the full employment terms as well as pipeline quantities.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('job_demands', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('demand_code', 32);

            $table->foreignId('company_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('employer_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->foreignId('owner_id')->nullable()->constrained('users')->nullOnDelete();

            $table->string('job_title');
            $table->string('country', 2);
            $table->string('city', 100)->nullable();
            $table->string('industry', 120)->nullable();
            $table->string('occupation', 120)->nullable();

            $table->unsignedInteger('quantity_required');
            $table->string('gender_requirement', 10)->default('any');
            $table->unsignedTinyInteger('minimum_age')->nullable();
            $table->unsignedTinyInteger('maximum_age')->nullable();

            // Money is stored in minor units with an explicit currency (§112).
            $table->unsignedBigInteger('salary_amount')->nullable();
            $table->char('salary_currency', 3)->nullable();
            $table->string('salary_period', 20)->default('monthly');
            $table->text('overtime_details')->nullable();
            $table->unsignedTinyInteger('working_hours_per_day')->nullable();
            $table->unsignedTinyInteger('working_days_per_week')->nullable();
            $table->string('weekly_holiday', 40)->nullable();

            $table->boolean('accommodation_provided')->default(false);
            $table->boolean('food_provided')->default(false);
            $table->boolean('transport_provided')->default(false);
            $table->boolean('medical_provided')->default(false);
            $table->boolean('insurance_provided')->default(false);
            $table->string('airfare_terms', 60)->nullable();
            $table->text('benefits_notes')->nullable();

            $table->unsignedSmallInteger('contract_duration_months')->nullable();
            $table->unsignedSmallInteger('probation_months')->nullable();
            $table->text('qualification')->nullable();
            $table->unsignedTinyInteger('required_experience_years')->nullable();
            $table->text('language_requirements')->nullable();
            $table->text('skills')->nullable();
            $table->text('employment_terms')->nullable();

            // What the agency charges the applicant, and its own processing cost.
            $table->unsignedBigInteger('recruitment_fee_amount')->nullable();
            $table->unsignedBigInteger('processing_cost_amount')->nullable();
            $table->char('fee_currency', 3)->nullable();

            $table->date('application_deadline')->nullable();
            $table->date('interview_date')->nullable();
            $table->date('deployment_target_date')->nullable();

            $table->string('status', 25)->default('draft');
            $table->text('status_reason')->nullable();
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('approved_at')->nullable();

            // Pipeline counters, maintained by the candidate-assignment module.
            $table->unsignedInteger('shortlisted_count')->default(0);
            $table->unsignedInteger('submitted_count')->default(0);
            $table->unsignedInteger('interviewed_count')->default(0);
            $table->unsignedInteger('selected_count')->default(0);
            $table->unsignedInteger('visa_approved_count')->default(0);
            $table->unsignedInteger('deployed_count')->default(0);

            $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(['company_id', 'demand_code']);
            $table->index(['company_id', 'status']);
            $table->index(['employer_id', 'status']);
            $table->index(['country', 'status']);
            $table->index('job_title');
        });
    }

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