<?php

namespace App\Models;

# use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Collection;

class CareMonth extends Model
{
    protected $table = 'care_month';

    public function patient(){
        return $this->hasOne(Client::class, 'id', 'client_id');
    }

    public function mcp(){
        return $this->hasOne(Pro::class, 'id', 'mcp_pro_id');
    }

    public function cmPro(){
        return $this->hasOne(Pro::class, 'id', 'cm_pro_id');
    }

    public function rmmPro(){
        return $this->hasOne(Pro::class, 'id', 'rmm_pro_id');
    }

    public function rmePro(){
        return $this->hasOne(Pro::class, 'id', 'rme_pro_id');
    }

    public function entries() {
        return $this->hasMany(CareMonthEntry::class, 'care_month_id', 'id');
    }

    public function bills() {
        return $this->hasMany(Bill::class, 'care_month_id', 'id');
    }

    public function getBillsOfType($_type) {
        $bills = $this->bills;
        $targetBills = new Collection();
        foreach ($bills as $bill) {
            if($bill->cm_or_rm === $_type) {
                $targetBills->add($bill);
            }
        }
        return $targetBills;
    }

    public function rmBill(){
        return $this->hasOne(Bill::class, 'id', 'rm_bill_id');
    }
}
