<?php

namespace App\Models;

use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Recall extends Model
{
    use BelongsToTenant;

    protected $fillable = [
        'tenant_id', 'patient_id', 'dentist_id', 'appointment_id',
        'due_date', 'reason', 'status', 'wa_message_id', 'sent_at',
    ];

    protected function casts(): array
    {
        return ['due_date' => 'date', 'sent_at' => 'datetime'];
    }

    public function patient(): BelongsTo
    {
        return $this->belongsTo(Patient::class);
    }

    public function dentist(): BelongsTo
    {
        return $this->belongsTo(Dentist::class);
    }

    public function scopeDue(Builder $q): Builder
    {
        return $q->where('status', 'scheduled')->whereDate('due_date', '<=', today());
    }
}
