<?php

namespace Modules\VetClinic\Entities;

use Illuminate\Database\Eloquent\Model;

class VcEvaluation extends Model
{
    protected $table = 'vc_evaluations';
    protected $guarded = ['id'];

    protected $casts = [
        'avg_rating'         => 'decimal:2',
        'flagged_for_review' => 'boolean',
        'reviewed_at'        => 'datetime',
    ];

    public function responses()
    {
        return $this->hasMany(VcEvaluationResponse::class, 'evaluation_id');
    }

    public function appointment()
    {
        return $this->belongsTo(VcAppointment::class, 'appointment_id');
    }

    public function doctor()
    {
        return $this->belongsTo(VcDoctor::class, 'doctor_id');
    }

    public function pet()
    {
        return $this->belongsTo(VcPet::class, 'pet_id');
    }

    public function evaluator()
    {
        return $this->belongsTo(\App\User::class, 'evaluator_user_id');
    }

    public function reviewer()
    {
        return $this->belongsTo(\App\User::class, 'reviewed_by');
    }

    public function contact()
    {
        return $this->belongsTo(\App\Contact::class, 'contact_id');
    }

    public function scopeForBusiness($query, $businessId)
    {
        return $query->where('business_id', $businessId);
    }

    public function scopeFlagged($query)
    {
        return $query->where('flagged_for_review', true);
    }
}
