<?php

namespace Modules\ClinicWhatsApp\Providers;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\ServiceProvider;
use Modules\ClinicWhatsApp\Console\ClinicWaRecalls;
use Modules\ClinicWhatsApp\Console\ClinicWaResultNotify;

/**
 * Thin bridge connecting ClinicCore recalls to the WhatsApp module's WaSender.
 * Command-only — registers the recall command and a daily schedule. Touches no
 * ClinicCore or WhatsApp files.
 */
class ClinicWhatsAppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        if ($this->app->runningInConsole()) {
            $this->commands([ClinicWaRecalls::class, ClinicWaResultNotify::class]);
        }
        $this->registerSchedule();
    }

    public function register()
    {
        //
    }

    protected function registerSchedule(): void
    {
        $this->callAfterResolving(Schedule::class, function (Schedule $schedule) {
            if (config('app.env') !== 'live') return;
            // Send next-day appointment + due-vaccination recalls each morning.
            $schedule->command('clinicwa:send-recalls')->dailyAt('08:30');
            // Notify patients when lab results arrive (timely, throughout the day).
            $schedule->command('clinicwa:notify-lab-results')->hourly();
        });
    }
}
