<?php

namespace Modules\VetClinic\Entities;

use Illuminate\Database\Eloquent\Model;
use App\Contact;

class VcAppointment extends Model
{
    protected $table = 'vc_appointments';
    protected $guarded = ['id'];

    protected $casts = [
        'appointment_date'        => 'date',
        'next_visit_date'         => 'date',
        'type_ids'                => 'array',
        'reason_ids'              => 'array',
        'next_visit_type_ids'     => 'array',
        'next_visit_reason_ids'   => 'array',
        'exam_results'            => 'array',
        'has_overlap'             => 'boolean',
        'arrived_at'              => 'datetime',
        'consultation_started_at' => 'datetime',
        'consultation_ended_at'   => 'datetime',
    ];

    public function statusLogs()
    {
        return $this->hasMany(VcAppointmentStatusLog::class, 'appointment_id')->orderBy('changed_at');
    }

    public function editRequests()
    {
        return $this->hasMany(VcAppointmentEditRequest::class, 'appointment_id')->latest();
    }

    public function doctorChangeRequests()
    {
        return $this->hasMany(VcDoctorChangeRequest::class, 'appointment_id')->latest();
    }

    public function medicalReports()
    {
        return $this->hasMany(VcMedicalReport::class, 'appointment_id');
    }

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

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

    public function nextVisitDepartment()
    {
        return $this->belongsTo(VcDepartment::class, 'next_visit_department_id');
    }

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

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

    public function location()
    {
        return $this->belongsTo(VcClinicLocation::class, 'location_id');
    }

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

    public function department()
    {
        return $this->belongsTo(VcDepartment::class, 'department_id');
    }

    public function schedule()
    {
        return $this->belongsTo(VcDoctorSchedule::class, 'schedule_id');
    }

    public function appointmentNotes()
    {
        return $this->hasMany(VcAppointmentNote::class, 'appointment_id')->orderByDesc('created_at');
    }

    public function vaccinations()
    {
        return $this->hasMany(VcVaccination::class, 'appointment_id');
    }

    public function prescriptions()
    {
        return $this->hasMany(VcPrescription::class, 'appointment_id');
    }

    public function documents()
    {
        return $this->hasMany(VcAppointmentDocument::class, 'appointment_id')->orderByDesc('created_at');
    }

    public function getStatusColorAttribute()
    {
        return [
            'pending'     => 'warning',
            'waiting'     => 'warning',
            'confirmed'   => 'info',
            'in_progress' => 'primary',
            'completed'   => 'success',
            'cancelled'   => 'danger',
            'no_show'     => 'default',
        ][$this->status] ?? 'default';
    }

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