<?php

declare(strict_types=1);

namespace App\Models\Market;

use App\Models\Model;
use App\Models\User;

class UserInfo extends Model
{
    protected ?string $table = 'user_infos';

    protected array $fillable = [
        'hashkey',
        'user_id',
        'firstname',
        'middlename',
        'lastname',
        'suffix',
        'gender',
        'dob',
        'priority_sector',
        'messenger_id',
        'viber_number',
        'tiktok_username',
        'region',
        'province',
        'city',
        'barangay',
        'civil_status',
        'children_count',
        'dependent_count',
        'education_level',
        'course',
        'school',
        'year_last_attended',
        'livelihood_source',
        'last_company',
        'employer_name',
        'last_position',
        'occupation',
        'last_employment_year',
        'monthly_income',
        'tin',
        'philhealth_id',
        'gov_id',
        'id_type',
        'id_number',
        'beneficiary_type',
        'emergency_contact_name',
        'emergency_contact_address',
        'emergency_contact_phone',
        'emergency_contact_relation',
        'emergency_contact_user_id',
        'fullname',
        'landline',
        'mobile',
        'email',
        'alt_email',
        'alt_landline',
        'alt_mobile',
        'facebook_url',
        'bank_details',
        'bank_account_no',
        'addresses',
        'other_details',
        'is_active',
        'created_by',
        'updated_by',
    ];

    protected array $casts = [
        'bank_details' => 'json',
        'addresses' => 'json',
        'other_details' => 'json',
        'is_active' => 'boolean',
        'dob' => 'date',
        'monthly_income' => 'float',
        'children_count' => 'integer',
        'dependent_count' => 'integer',
    ];

    /**
     * Get the virtual age attribute.
     */
    public function getAgeAttribute(): ?int
    {
        if (!$this->dob) {
            return null;
        }
        return $this->dob->diffInYears(now());
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function emergencyContactUser()
    {
        return $this->belongsTo(User::class, 'emergency_contact_user_id');
    }

    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function updater()
    {
        return $this->belongsTo(User::class, 'updated_by');
    }
}
