<?php

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

/**
 * Public employer manpower requisition (§144, §144N).
 *
 * This is a structured demand intake, not a contact form: a submission becomes
 * a reviewable record that can be converted into real ERP job demands.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('employer_requisitions', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('requisition_no', 40);
            // Opaque token the submitter uses to see their confirmation again.
            $table->string('public_token', 64)->unique();

            $table->foreignId('company_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained()->nullOnDelete();
            // Linked once the requisition is matched to a known employer (§144L).
            $table->foreignId('employer_id')->nullable()->constrained()->nullOnDelete();

            // Step 1 — company information
            $table->string('company_name');
            $table->string('country', 2);
            $table->string('city', 100)->nullable();
            $table->string('industry', 120);
            $table->string('website')->nullable();
            $table->string('registration_number', 100)->nullable();
            $table->string('license_number', 100)->nullable();
            $table->text('company_address');

            // Step 2 — contact person
            $table->string('contact_name');
            $table->string('contact_designation', 120);
            $table->string('contact_email');
            $table->string('contact_phone', 32);
            $table->string('contact_whatsapp', 32)->nullable();
            $table->string('preferred_contact_method', 20)->default('email');

            // Step 6 — timeline
            $table->date('required_joining_date')->nullable();
            $table->date('application_deadline')->nullable();
            $table->date('preferred_interview_date')->nullable();
            $table->date('expected_deployment_date')->nullable();
            $table->string('urgency', 20)->default('standard');

            // Step 7 — selection preference
            $table->boolean('interview_required')->default(false);
            $table->string('interview_method', 40)->nullable();
            $table->boolean('employer_visiting')->default(false);
            $table->boolean('cv_shortlisting_required')->default(true);
            $table->boolean('trade_test_required')->default(false);

            $table->text('additional_notes')->nullable();

            $table->string('status', 30)->default('submitted');
            $table->string('priority', 20)->default('normal');
            $table->text('status_reason')->nullable();
            $table->foreignId('assigned_to')->nullable()->constrained('users')->nullOnDelete();

            $table->timestamp('submitted_at');
            $table->timestamp('reviewed_at')->nullable();
            $table->foreignId('reviewed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('approved_at')->nullable();
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('converted_at')->nullable();

            $table->string('source_ip', 45)->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['company_id', 'requisition_no']);
            $table->index(['company_id', 'status', 'submitted_at']);
            $table->index(['assigned_to', 'status']);
        });

        // Step 3–5: one row per requested position, each with its own package.
        Schema::create('employer_requisition_positions', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('employer_requisition_id')->constrained()->cascadeOnDelete();
            $table->foreignId('job_category_id')->nullable()->constrained()->nullOnDelete();
            // Set when this position has been turned into a job demand (§144K).
            $table->foreignId('job_demand_id')->nullable()->constrained()->nullOnDelete();

            $table->string('job_title');
            $table->unsignedInteger('quantity');
            $table->string('gender_preference', 10)->default('any');
            $table->unsignedTinyInteger('minimum_age')->nullable();
            $table->unsignedTinyInteger('maximum_age')->nullable();
            $table->unsignedTinyInteger('minimum_experience_years')->nullable();
            $table->text('qualification')->nullable();
            $table->text('required_skills')->nullable();
            $table->string('trade_test', 30)->default('no');

            // Salary is captured in minor units, like everywhere else (§112).
            $table->unsignedBigInteger('salary_amount')->nullable();
            $table->char('salary_currency', 3)->nullable();
            $table->string('salary_period', 20)->default('monthly');
            $table->unsignedTinyInteger('working_hours_per_day')->nullable();
            $table->unsignedTinyInteger('working_days_per_week')->nullable();
            $table->boolean('overtime_available')->default(false);
            $table->text('overtime_details')->nullable();
            $table->unsignedSmallInteger('contract_duration_months')->nullable();
            $table->unsignedSmallInteger('probation_months')->nullable();
            $table->string('weekly_holiday', 40)->nullable();
            $table->unsignedSmallInteger('annual_leave_days')->nullable();

            // Step 5 — benefits
            $table->boolean('accommodation_provided')->default(false);
            $table->boolean('food_provided')->default(false);
            $table->boolean('food_allowance')->default(false);
            $table->boolean('transport_provided')->default(false);
            $table->boolean('medical_provided')->default(false);
            $table->boolean('insurance_provided')->default(false);
            $table->boolean('airfare_provided')->default(false);
            $table->boolean('visa_cost_covered')->default(false);
            $table->boolean('work_permit_cost_covered')->default(false);
            $table->boolean('uniform_provided')->default(false);
            $table->text('other_benefits')->nullable();

            // Step 8 — documents the employer wants from candidates.
            $table->json('required_documents')->nullable();

            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();

            $table->index('employer_requisition_id');
        });

        Schema::create('employer_requisition_status_histories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employer_requisition_id')->constrained()->cascadeOnDelete();
            $table->string('from_status', 30)->nullable();
            $table->string('to_status', 30);
            $table->text('reason')->nullable();
            // Set when staff asked the employer for more information (§144I).
            $table->text('information_requested')->nullable();
            $table->foreignId('changed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('changed_at');
            $table->timestamps();

            $table->index(['employer_requisition_id', 'changed_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('employer_requisition_status_histories');
        Schema::dropIfExists('employer_requisition_positions');
        Schema::dropIfExists('employer_requisitions');
    }
};
