<?php

namespace App\Console\Commands;

use App\Domain\WhatsApp\AutomationService;
use App\Domain\WhatsApp\WhatsAppManager;
use Illuminate\Console\Command;

class WhatsAppDispatch extends Command
{
    protected $signature = 'whatsapp:dispatch
        {--reminders : 24h + 1h appointment reminders}
        {--reviews : review requests after completed visits}
        {--recalls : recall / next-visit reminders due today}
        {--installments : installment due reminders}
        {--retries : re-send messages whose retry time has arrived}
        {--all : run everything}';

    protected $description = 'Queue scheduled WhatsApp automations.';

    public function handle(AutomationService $auto, WhatsAppManager $wa): int
    {
        $all = $this->option('all');

        if ($all || $this->option('reminders')) {
            $this->line('24h reminders: '.$auto->sendReminders24h());
            $this->line('1h reminders: '.$auto->sendReminders1h());
        }
        if ($all || $this->option('reviews')) {
            $this->line('review requests: '.$auto->sendReviewRequests());
        }
        if ($all || $this->option('recalls')) {
            $this->line('recalls: '.$auto->sendRecalls());
        }
        if ($all || $this->option('installments')) {
            $this->line('installment reminders: '.$auto->sendInstallmentReminders());
        }
        if ($all || $this->option('retries')) {
            $this->line('retries swept: '.$wa->sweepRetries());
        }

        return self::SUCCESS;
    }
}
