<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Appointment extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'doctor_id','patient_id','appointment_date','appointment_time',
        'duration_minutes','type','status','reason','notes',
        'fee','is_paid','confirmed_at','cancelled_at','cancellation_reason',
    ];

    protected $casts = [
        'appointment_date' => 'date',
        'confirmed_at'     => 'datetime',
        'cancelled_at'     => 'datetime',
        'is_paid'          => 'boolean',
        'fee'              => 'decimal:2',
    ];

    public function doctor()  { return $this->belongsTo(Doctor::class); }
    public function patient() { return $this->belongsTo(Patient::class); }
    public function queue()   { return $this->hasOne(Queue::class); }
    public function medicalRecord() { return $this->hasOne(MedicalRecord::class); }

    public function scopeToday($q)    { return $q->whereDate('appointment_date', today()); }
    public function scopeUpcoming($q) { return $q->whereDate('appointment_date', '>=', today()); }
    public function scopePending($q)  { return $q->where('status', 'pending'); }
    public function scopeConfirmed($q){ return $q->where('status', 'confirmed'); }
}
