<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Person extends Model
{
    use HasFactory;

    protected $table = 'persons';

    protected $fillable = [
        'owner_user_id',
        'nik',
        'name_prefix',
        'name',
        'name_suffix',
        'phone',
        'email',
        'gender',
        'birth_date',
        'birth_place',
        'language',
        'race',
        'citizenship',
        'current_employment',
        'last_education',
        'religion',
        'blood_type',
        'last_weight_kg',
        'last_height_cm',
        'is_deceased',
        'deceased_at',
        'marital_status',
        'main_address_id',
        'domicile_address_id',
        'created_by',
        'updated_by',
        'deleted_by'
    ];

    protected $hidden = [
        'created_at',
        'updated_at',
        'deleted_at',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    public function getFullNameAttribute()
    {
        $arr = [];
        if (!empty($this->name_prefix)) {
            $arr[] = $this->name_prefix;
        }
        $arr[] = $this->name;
        if (!empty($this->name_suffix)) {
            $arr[] = $this->name_suffix;
        }

        return implode(' ', $arr);
    }

    public function addresses()
    {
        return $this->morphMany(Address::class, 'addressable');
    }

    public function currentAddress()
    {
        return $this->belongsTo(Address::class, 'main_address_id')->withDefault([
            'text' => '',
        ]);
    }

    public function domicileAddress()
    {
        return $this->belongsTo(Address::class, 'main_address_id');
    }

    public function metas()
    {
        return $this->morphMany(Meta::class, 'metaable');
    }

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

    public function files()
    {
        return $this->morphMany(File::class, 'fileable');
    }

    public function avatar()
    {
        return $this->morphOne(File::class, 'fileable')->where('type', 'avatar')->latestOfMany();
    }

    public function families()
    {
        return $this->belongsToMany(Person::class, 'family_relations', 'owner_id', 'person_id')->withPivot(['relation_with_owner']);
    }

    public function familyOwner()
    {
        return $this->hasOne(Family::class, 'person_id');
    }

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

    public function practitioner()
    {
        return $this->hasOne(Practitioner::class, 'person_id');
    }

    public function appointmentParticipantables()
    {
        return $this->morphMany(AppointmentParticipant::class, 'participantable');
    }

    public function member()
    {
        return $this->hasOne(Member::class);
    }

    public function setGenderAttribute($value)
    {
        if ($value == "M" || $value == "L") {
            return $this->attributes['gender'] = "male";
        } else if ($value == "F" || $value == "P") {
            return $this->attributes['gender'] = "female";
        } else {
            return $this->attributes['gender'] = $value;
        }
    }

    public function getGenderAttribute()
    {
        if ($this->attributes['gender']  == "male" || $this->attributes['gender']  == "L") {
            return "male";
        } else if ($this->attributes['gender']  == "female" || $this->attributes['gender']  == "P") {
            return "female";
        } else {
            return "other";
        }
    }
}
