<?php

namespace App\Models;

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

class Practitioner extends Model
{
    use HasFactory;

    protected $fillable = [
        'person_id'
    ];
    
    public $appends = [
        'meta',
        'code'
    ];

    public function getMetaAttribute()
    {
        $orgMeta = [];
        foreach ($this->metas as $meta) {
            $orgMeta[$meta->type] = $meta->value;
        }

        return (object) $orgMeta;
    }

    public function getCodeAttribute()
    {
        return 'DOC'.str_pad($this->id, 4, '0', STR_PAD_LEFT);
    }

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

    public function practitionerRoles()
    {
        return $this->hasMany(PractitionerRole::class, 'practitioner_id');
    }

    public function claim_history_care()
    {
        return $this->hasMany(ClaimHistoryCare::class, 'practitioner_id');
    }

    
}
