<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('employee_registrations', function (Blueprint $table) {
            $table->id();

            // Foreign Keys
            $table->foreignId('company_id')->constrained('companies')->onDelete('cascade');
            $table->foreignId('company_employee_id')->nullable()->constrained('company_employees')->onDelete('set null');

            // Status & Tracking
            $table->enum('submission_type', ['public_form', 'bulk_upload'])->default('public_form');
            $table->enum('status', ['pending_approval', 'approved', 'rejected'])->default('pending_approval');
            $table->enum('account_status', ['pending', 'invited', 'active'])->default('pending');
            $table->enum('verification_status', ['not_verified', 'verified'])->default('not_verified');
            $table->text('rejection_reason')->nullable();

            // BIO-DATA INFORMATION
            $table->string('title', 20)->nullable(); // Mr, Mrs, Miss, Dr, etc.
            $table->string('first_name', 100);
            $table->string('last_name', 100);
            $table->string('middle_name', 100)->nullable();
            $table->enum('gender', ['Male', 'Female', 'Other'])->nullable();
            $table->date('date_of_birth');
            $table->string('nationality', 100)->default('Nigeria');
            $table->string('phone_number', 20);
            $table->string('confirm_phone_number', 20);
            $table->string('foreign_number', 20)->nullable();
            $table->string('email_address', 255);
            $table->string('confirm_email_address', 255);
            $table->string('country', 100)->default('Nigeria');
            $table->string('state', 100);
            $table->string('city', 100);
            $table->string('chn', 50)->nullable(); // Clearing House Number
            $table->text('house_address');
            $table->text('previous_address')->nullable();

            // BANK INFORMATION
            // Encrypted BVN (AES-GCM from frontend)
            $table->text('bvn_encrypted')->nullable(); // Ciphertext
            $table->string('bvn_iv', 64)->nullable(); // Initialization Vector
            $table->string('bvn_tag', 64)->nullable(); // Authentication Tag
            $table->string('bank_name', 100)->nullable();
            $table->string('bank_account_number', 20)->nullable();
            $table->string('tin_or_pin', 50)->nullable(); // Tax Identification Number or Payer Identification Number
            $table->boolean('bank_details_consent')->default(false);

            // IDENTITY INFORMATION
            $table->string('identity_card_type', 50); // National Identity Number, International Passport, Driver's License, Voter's Card, etc.
            $table->string('identity_card_number', 50)->nullable(); // Plain text for non-NIN cards

            // NIN Details (encrypted if identity_card_type is NIN)
            $table->text('nin_number_encrypted')->nullable(); // Ciphertext
            $table->string('nin_number_iv', 64)->nullable(); // Initialization Vector
            $table->string('nin_number_tag', 64)->nullable(); // Authentication Tag

            // File paths
            $table->string('identity_card_path')->nullable();
            $table->string('scanned_signature_path')->nullable();
            $table->string('passport_photograph_path')->nullable();
            $table->string('nin_document_path')->nullable(); // NEW: NIN document upload

            // Consent
            $table->boolean('consent_given')->default(false);

            // Tracking
            $table->foreignId('submitted_by')->nullable()->constrained('users')->onDelete('set null');
            $table->foreignId('approved_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamp('approved_at')->nullable();
            $table->foreignId('rejected_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamp('rejected_at')->nullable();

            $table->timestamps();
            $table->softDeletes();

            // Indexes
            $table->index('company_id');
            $table->index('status');
            $table->index('submission_type');
            $table->index('account_status');
            $table->index('verification_status');
            $table->index('email_address');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('employee_registrations');
    }
};
