<?php

namespace App\Models;

# use Illuminate\Database\Eloquent\Model;

class Measurement extends Model
{
    protected $table = 'measurement';

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

    public function clientBDTMeasurement()
    {
        return $this->hasOne(ClientBDTMeasurement::class, 'id', 'client_bdt_measurement_id');
    }

    public function numCPMEntries() {
        return ClientProgramMonthEntry::where('measurement_id', $this->id)->where('is_cancelled', false)->count();
    }

    public function minutesEntered($pro) {
        $entries = ClientProgramMonthEntry::where('measurement_id', $this->id)
            ->where('pro_id', $pro->id)
            ->where('is_cancelled', false)
            ->get();
        $minutes = 0;
        foreach ($entries as $entry) {
            $minutes += $entry->time_in_minutes;
        }
        return $minutes;
    }

    public function entriesByPro($pro) {
        return ClientProgramMonthEntry::where('measurement_id', $this->id)
            ->where('pro_id', $pro->id)
            ->where('is_cancelled', false)
            ->get();
    }

    public function childMeasurements() {
        return Measurement::where('parent_measurement_id', $this->id)
            ->where('is_removed', false)
            ->get();
    }
}
