<?php

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

/**
 * Employers / overseas partners (§14). Job demands, candidate submissions and
 * receivables all hang off this record, so it carries the agreement and
 * compliance state the recruitment pipeline depends on.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('employers', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('employer_code', 32);

            $table->foreignId('company_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained()->nullOnDelete();
            // Staff member who owns the relationship.
            $table->foreignId('account_manager_id')->nullable()->constrained('users')->nullOnDelete();

            $table->string('company_name');
            $table->string('legal_name')->nullable();
            $table->string('country', 2);
            $table->string('city', 100)->nullable();
            $table->text('address')->nullable();

            $table->string('registration_number', 100)->nullable();
            $table->string('license_number', 100)->nullable();
            $table->date('license_expiry')->nullable();
            $table->string('industry', 120)->nullable();
            $table->string('website')->nullable();
            $table->string('email')->nullable();
            $table->string('phone', 32)->nullable();
            $table->string('whatsapp', 32)->nullable();

            $table->string('status', 20)->default('prospect');
            $table->string('compliance_status', 20)->default('pending');
            $table->text('compliance_notes')->nullable();
            $table->date('blacklisted_at')->nullable();
            $table->text('status_reason')->nullable();

            $table->string('agreement_reference', 100)->nullable();
            $table->date('agreement_start_date')->nullable();
            $table->date('agreement_expiry_date')->nullable();
            // Days allowed before an employer invoice falls overdue.
            $table->unsignedSmallInteger('credit_terms_days')->nullable();
            $table->char('billing_currency', 3)->nullable();

            // 1–5 relationship rating maintained by the account manager.
            $table->unsignedTinyInteger('rating')->nullable();
            $table->text('preferred_jobs')->nullable();
            $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', 'employer_code']);
            $table->index(['company_id', 'status']);
            $table->index(['company_id', 'country']);
            $table->index('company_name');
        });

        Schema::create('employer_contacts', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('employer_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->string('designation')->nullable();
            $table->string('email')->nullable();
            $table->string('phone', 32)->nullable();
            $table->string('whatsapp', 32)->nullable();
            $table->string('language', 40)->nullable();
            $table->boolean('is_primary')->default(false);
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['employer_id', 'is_primary']);
        });
    }

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