<?php

namespace App\Services;

use App\Enums\AppointmentStatus;
use App\Models\Appointment;
use App\Models\Patient;

class PatientMetricsService
{
    public function refresh(int $patientId): void
    {
        $completed = Appointment::query()
            ->where('patient_id', $patientId)
            ->where('status', AppointmentStatus::COMPLETED->value);

        $count = (clone $completed)->count();
        $lastVisit = (clone $completed)->max('scheduled_start_at');

        Patient::query()->whereKey($patientId)->update([
            'total_visits' => $count,
            'last_visit_at' => $lastVisit,
        ]);
    }
}
