<?php

namespace Modules\ClinicCore\Entities;

use Illuminate\Database\Eloquent\Model;

class ClcAppointmentEditRequest extends Model
{
    protected $table = 'clc_appointment_edit_requests';
    protected $guarded = ['id'];

    protected $casts = [
        'reviewed_at' => 'datetime',
        'consumed_at' => 'datetime',
    ];

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

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

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

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

    public function scopePending($query)
    {
        return $query->where('status', 'pending');
    }

    /** An approved request that has not yet been used for its single edit. */
    public function scopeUsable($query)
    {
        return $query->where('status', 'approved')->whereNull('consumed_at');
    }
}
