<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\CompanyRegistration>
 */
class CompanyRegistrationFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $companyName = fake()->company();
        $email = fake()->unique()->companyEmail();
        $identityCardType = fake()->randomElement(['National ID', 'National Identity Number', 'International Passport', 'Driver\'s License']);
        $isNin = $identityCardType === 'National Identity Number';

        return [
            'company_id' => null,
            'submission_type' => fake()->randomElement(['public_form', 'bulk_upload']),
            'status' => 'pending_approval',
            'account_status' => 'pending',
            'verification_status' => 'not_verified',
            'rejection_reason' => null,
            // Company Information
            'company_name' => $companyName,
            'rc_number' => 'RC' . fake()->numerify('######'),
            'country' => 'Nigeria',
            'state' => fake()->randomElement(['Lagos', 'Abuja', 'Rivers', 'Oyo', 'Kano']),
            'city' => fake()->city(),
            'phone_number' => '0' . fake()->numerify('##########'),
            'secondary_phone' => fake()->boolean(30) ? '0' . fake()->numerify('##########') : null,
            'email_address' => $email,
            'house_address' => fake()->address(),
            'previous_address' => fake()->boolean(30) ? fake()->address() : null,
            // Director Identity Information
            'director_name' => fake()->name(),
            // Encrypted BVN — packed as iv:ciphertext:tag
            'director_bvn_encrypted' => base64_encode(random_bytes(12)) . ':' . base64_encode(fake()->numerify('###########')) . ':' . base64_encode(random_bytes(16)),
            '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
            'director_identity_cards_path' => null,
            'cac_document_path' => null,
            'director_passport_photograph_path' => 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' => 'approved',
            'approved_at' => now(),
        ]);
    }

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