<?php

namespace Modules\VetClinic\Entities\Services;

use Modules\VetClinic\Entities\VcVaccination;
use Modules\VetClinic\Entities\VcVaccineType;
use Carbon\Carbon;

class VaccinationService
{
    public function calculateNextDue(string $administeredDate, int $validForMonths): string
    {
        return Carbon::parse($administeredDate)->addMonths($validForMonths)->toDateString();
    }

    public function getOverdueCount(int $businessId): int
    {
        return VcVaccination::where('business_id', $businessId)
            ->where('status', 'administered')
            ->where('next_due_date', '<', Carbon::today())
            ->count();
    }

    public function getDueSoon(int $businessId, int $withinDays = 30): \Illuminate\Database\Eloquent\Collection
    {
        $today = Carbon::today();
        return VcVaccination::with(['pet', 'vaccineType', 'owner'])
            ->where('business_id', $businessId)
            ->where('status', 'administered')
            ->whereBetween('next_due_date', [$today, $today->copy()->addDays($withinDays)])
            ->orderBy('next_due_date')
            ->get();
    }

    public function markOverdue(int $businessId): int
    {
        return VcVaccination::where('business_id', $businessId)
            ->where('status', 'administered')
            ->where('next_due_date', '<', Carbon::today())
            ->update(['status' => 'overdue']);
    }
}
