<?php

namespace App\Console\Commands;

use App\Services\BranchContext;
use App\Services\LoanReminderService;
use Illuminate\Console\Command;

class DispatchLoanReminders extends Command
{
    protected $signature='notifications:dispatch-reminders {--date= : Dispatch reminders for a specific YYYY-MM-DD date}';
    protected $description='Generate and deliver due loan repayment reminders without duplicate sending.';

    public function handle(LoanReminderService $service, BranchContext $context): int
    {
        return $context->systemWide(function () use ($service): int {
            $date=$this->option('date') ?: now()->toDateString();
            $stats=$service->dispatch((string)$date);
            $this->info("Reminder dispatch complete: {$stats['generated']} generated, {$stats['delivered']} delivered, {$stats['failed']} failed, {$stats['skipped']} skipped.");
            return self::SUCCESS;
        });
    }
}
