<?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 claims() {
        return $this->hasMany(Claim::class, 'cm_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');
    }

    public function company()
    {
        return $this->hasOne(Company::class, 'id', 'company_id');
    }

    public function companyProPayer()
    {
        return $this->hasOne(CompanyProPayer::class, 'id', 'company_pro_payer_id');
    }

    public function companyLocation()
    {
        return $this->hasOne(CompanyLocation::class, 'id', 'company_location_id');
    }
}
