<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\HasOne;

# use Illuminate\Database\Eloquent\Model;

class Client extends Model
{
    protected $table = 'client';

    public function displayName() {
        return $this->name_last . ', '. $this->name_first;
    }

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

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

    public function prosInMeetingWith() {
        return Pro::where('in_meeting_with_client_id', $this->id)->get();
    }

    public function notes() {
        return $this->hasMany(Note::class, 'client_id', 'id')->orderBy('created_at', 'desc');
    }

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

    public function actionItems () {
        return $this->hasMany(ActionItem::class, 'client_id', 'id')
            ->orderBy('action_item_category', 'asc')
            ->orderBy('created_at', 'desc');
    }

    public function infoLines() {
	    return $this->hasMany(ClientInfoLine::class, 'client_id', 'id')->orderBy('created_at', 'desc');
    }

    public function measurements() {
        return $this->hasMany(Measurement::class, 'client_id', 'id')
            ->distinct('label')
            ->where('is_removed', false)
            ->orderBy('label', 'asc')
            ->orderBy('effective_date', 'desc');
    }

    public function smses() {
        return $this->hasMany(ClientSMS::class, 'client_id', 'id')
            ->orderBy('created_at', 'desc');
    }

    public function documents() {
        return $this->hasMany(ClientDocument::class, 'client_id', 'id')
            ->orderBy('created_at', 'desc');
    }

    public function smsNumbers() {
        return $this->hasMany(ClientSMSNumber::class, 'client_id', 'id')
            ->orderBy('created_at', 'desc');
    }

    public function nextMcpAppointment() {
        if($this->mcp) {
            return Appointment::where('client_id', $this->id)
                ->where('pro_id', $this->mcp->id)
                ->where('start_time', '>=', date('Y-m-d'))
                ->orderBy('start_time', 'asc')
                ->first();
        }
        return false;
    }

    public function appointments() {
        return $this->hasMany(Appointment::class, 'client_id', 'id')
            ->orderBy('start_time', 'desc');
    }

}
