<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class CompanyRegistration extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'company_registrations';

    protected $fillable = [
        'company_id',
        'submission_type',
        'status',
        'account_status',
        'verification_status',
        'rejection_reason',
        // Company Information
        'company_name',
        'rc_number',
        'country',
        'state',
        'city',
        'phone_number',
        'secondary_phone',
        'email_address',
        'house_address',
        'previous_address',
        // Director Identity Information
        'director_name',
        'director_bvn_encrypted',
        'identity_card_type',
        'identity_card_number',
        'nin_number_encrypted',
        // File paths
        'director_identity_cards_path',
        'cac_document_path',
        'director_passport_photograph_path',
        // Consent
        'consent_given',
        // Tracking
        'submitted_by',
        'approved_by',
        'approved_at',
        'rejected_by',
        'rejected_at',
    ];

    protected function casts(): array
    {
        return [
            'consent_given' => 'boolean',
            'approved_at' => 'datetime',
            'rejected_at' => 'datetime',
        ];
    }

    /**
     * Get the company associated with this registration.
     */
    public function amlScreenings(): MorphMany
    {
        return $this->morphMany(AmlScreeningResult::class, 'screenable')->latest('screened_at');
    }

    public function company(): BelongsTo
    {
        return $this->belongsTo(Company::class);
    }

    /**
     * Get the user who submitted this registration form.
     */
    public function submittedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'submitted_by');
    }

    /**
     * Get the admin who approved this registration.
     */
    public function approvedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'approved_by');
    }

    /**
     * Get the admin who rejected this registration.
     */
    public function rejectedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'rejected_by');
    }

    /**
     * Get all update requests for this registration.
     */
    public function updates()
    {
        return $this->hasMany(CompanyRegistrationUpdate::class, 'company_registration_id');
    }

    /**
     * Get pending update requests for this registration.
     */
    public function pendingUpdates()
    {
        return $this->hasMany(CompanyRegistrationUpdate::class, 'company_registration_id')
            ->where('status', 'pending_approval');
    }

    /**
     * Check if registration is pending approval.
     */
    public function isPending(): bool
    {
        return $this->status === 'pending_approval';
    }

    /**
     * Check if registration is approved.
     */
    public function isApproved(): bool
    {
        return $this->status === 'approved';
    }

    /**
     * Check if registration is rejected.
     */
    public function isRejected(): bool
    {
        return $this->status === 'rejected';
    }

    /**
     * Check if account status is pending.
     */
    public function isAccountPending(): bool
    {
        return $this->account_status === 'pending';
    }

    /**
     * Check if account status is invited.
     */
    public function isAccountInvited(): bool
    {
        return $this->account_status === 'invited';
    }

    /**
     * Check if account status is active.
     */
    public function isAccountActive(): bool
    {
        return $this->account_status === 'active';
    }

    /**
     * Check if verification status is verified.
     */
    public function isVerified(): bool
    {
        return $this->verification_status === 'verified';
    }

    /**
     * Check if verification status is not verified.
     */
    public function isNotVerified(): bool
    {
        return $this->verification_status === 'not_verified';
    }

    /**
     * Check if registration was made via public form.
     */
    public function isPublicForm(): bool
    {
        return $this->submission_type === 'public_form';
    }

    /**
     * Check if registration was made via bulk upload.
     */
    public function isBulkUpload(): bool
    {
        return $this->submission_type === 'bulk_upload';
    }

    /**
     * Check if director BVN has been submitted (encrypted).
     */
    public function hasSubmittedBvn(): bool
    {
        return ! is_null($this->director_bvn_encrypted);
    }

    /**
     * Check if identity card is a NIN (National Identity Number).
     */
    public function isIdentityCardNin(): bool
    {
        return $this->identity_card_type === 'National Identity Number';
    }

    /**
     * Check if encrypted NIN has been submitted.
     */
    public function hasEncryptedNin(): bool
    {
        return ! is_null($this->nin_number_encrypted);
    }

    /**
     * Scope to get only pending registrations.
     */
    public function scopePending($query)
    {
        return $query->where('status', 'pending_approval');
    }

    /**
     * Scope to get only approved registrations.
     */
    public function scopeApproved($query)
    {
        return $query->where('status', 'approved');
    }

    /**
     * Scope to get only rejected registrations.
     */
    public function scopeRejected($query)
    {
        return $query->where('status', 'rejected');
    }

    /**
     * Scope to get only public form registrations.
     */
    public function scopePublicForm($query)
    {
        return $query->where('submission_type', 'public_form');
    }

    /**
     * Scope to get only bulk upload registrations.
     */
    public function scopeBulkUpload($query)
    {
        return $query->where('submission_type', 'bulk_upload');
    }

    /**
     * Scope to get only registrations with pending account status.
     */
    public function scopeAccountPending($query)
    {
        return $query->where('account_status', 'pending');
    }

    /**
     * Scope to get only registrations with invited account status.
     */
    public function scopeAccountInvited($query)
    {
        return $query->where('account_status', 'invited');
    }

    /**
     * Scope to get only registrations with active account status.
     */
    public function scopeAccountActive($query)
    {
        return $query->where('account_status', 'active');
    }

    /**
     * Scope to get only verified registrations.
     */
    public function scopeVerified($query)
    {
        return $query->where('verification_status', 'verified');
    }

    /**
     * Scope to get only not verified registrations.
     */
    public function scopeNotVerified($query)
    {
        return $query->where('verification_status', 'not_verified');
    }
}
