<?php

namespace Database\Factories;

use App\Models\EmployeeRegistration;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\EmployeeRegistration>
 */
class EmployeeRegistrationFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $email = fake()->unique()->safeEmail();
        $phone = '0' . fake()->numerify('##########');
        $identityCardType = fake()->randomElement(['National ID', 'National Identity Number', 'International Passport', 'Driver\'s License', 'Voter\'s Card']);
        $isNin = $identityCardType === 'National Identity Number';

        return [
            'company_id' => null,
            'company_employee_id' => null,
            'submission_type' => fake()->randomElement(['public_form', 'bulk_upload']),
            'status' => EmployeeRegistration::STATUS_PENDING,
            'account_status' => EmployeeRegistration::ACCOUNT_STATUS_PENDING,
            'verification_status' => EmployeeRegistration::VERIFICATION_STATUS_NOT_VERIFIED,
            'rejection_reason' => null,
            // BIO-DATA INFORMATION
            'title' => fake()->randomElement(['Mr', 'Mrs', 'Miss', 'Dr', 'Engr']),
            'first_name' => fake()->firstName(),
            'last_name' => fake()->lastName(),
            'middle_name' => fake()->boolean(70) ? fake()->firstName() : null,
            'gender' => fake()->randomElement(['Male', 'Female']),
            'date_of_birth' => fake()->date('Y-m-d', '-25 years'),
            'nationality' => 'Nigeria',
            'phone_number' => $phone,
            'confirm_phone_number' => $phone,
            'foreign_number' => fake()->boolean(20) ? '+1' . fake()->numerify('##########') : null,
            'email_address' => $email,
            'confirm_email_address' => $email,
            'country' => 'Nigeria',
            'state' => fake()->randomElement(['Lagos', 'Abuja', 'Rivers', 'Oyo', 'Kano']),
            'city' => fake()->city(),
            'chn' => fake()->boolean(30) ? 'CHN' . fake()->numerify('##########') : null,
            'house_address' => fake()->address(),
            'previous_address' => fake()->boolean(30) ? fake()->address() : null,
            // BANK INFORMATION
            // Encrypted BVN — packed as iv:ciphertext:tag
            'bvn_encrypted' => base64_encode(random_bytes(12)) . ':' . base64_encode(fake()->numerify('###########')) . ':' . base64_encode(random_bytes(16)),
            'bank_name' => fake()->randomElement(['First Bank', 'GTBank', 'Access Bank', 'Zenith Bank', 'UBA']),
            'bank_account_number' => fake()->numerify('##########'),
            'tin_or_pin' => fake()->boolean(50) ? fake()->numerify('##########') : null,
            'bank_details_consent' => true,
            // IDENTITY INFORMATION
            'identity_card_type' => $identityCardType,
            'identity_card_number' => $isNin ? null : fake()->bothify('??########'),
            // Encrypted NIN — packed as iv:ciphertext:tag (only for NIN type)
            'nin_number_encrypted' => $isNin ? base64_encode(random_bytes(12)) . ':' . base64_encode(fake()->numerify('###########')) . ':' . base64_encode(random_bytes(16)) : null,
            // File paths
            'identity_card_path' => null,
            'scanned_signature_path' => null,
            'passport_photograph_path' => null,
            'nin_document_path' => $isNin ? null : null,
            // Consent
            'consent_given' => true,
            // Tracking
            'submitted_by' => null,
            'approved_by' => null,
            'approved_at' => null,
            'rejected_by' => null,
            'rejected_at' => null,
        ];
    }

    /**
     * Indicate that the submission is approved.
     */
    public function approved(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => EmployeeRegistration::STATUS_APPROVED,
            'approved_at' => now(),
        ]);
    }

    /**
     * Indicate that the submission is verified.
     */
    public function verified(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => EmployeeRegistration::STATUS_VERIFIED,
            'approved_at' => $attributes['approved_at'] ?? now(),
        ]);
    }

    /**
     * Indicate that the submission is rejected.
     */
    public function rejected(?string $reason = null): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => EmployeeRegistration::STATUS_REJECTED,
            'rejection_reason' => $reason ?? 'Incomplete documentation',
            'rejected_at' => now(),
        ]);
    }

    /**
     * Indicate that the account has been invited.
     */
    public function invited(): static
    {
        return $this->state(fn (array $attributes) => [
            'account_status' => EmployeeRegistration::ACCOUNT_STATUS_INVITED,
        ]);
    }

    /**
     * Indicate that the account is active.
     */
    public function active(): static
    {
        return $this->state(fn (array $attributes) => [
            'account_status' => EmployeeRegistration::ACCOUNT_STATUS_ACTIVE,
        ]);
    }
}
