<?php

namespace Modules\VetClinic\Entities;

use Illuminate\Database\Eloquent\Model;

class VcReminderLog extends Model
{
    protected $table = 'vc_reminder_logs';

    protected $fillable = [
        'business_id',
        'vaccination_id',
        'contact_id',
        'pet_id',
        'channel',
        'template_type',
        'sent_to',
        'status',
        'error_message',
        'sent_at',
    ];

    protected $casts = [
        'sent_at' => 'datetime',
    ];

    public function vaccination()
    {
        return $this->belongsTo(VcVaccination::class, 'vaccination_id');
    }

    /**
     * Count how many reminders of a given channel have been sent for a vaccination.
     */
    public static function countFor(int $vaccinationId, string $channel = 'email'): int
    {
        return static::where('vaccination_id', $vaccinationId)
            ->where('channel', $channel)
            ->where('status', 'sent')
            ->count();
    }

    /**
     * Check if a reminder was sent today for a given vaccination+channel.
     */
    public static function sentToday(int $vaccinationId, string $channel = 'email'): bool
    {
        return static::where('vaccination_id', $vaccinationId)
            ->where('channel', $channel)
            ->whereDate('sent_at', today())
            ->exists();
    }
}
