<?php

namespace App\Console\Commands;

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

class ProcessRecurringExpenses extends Command
{
    protected $signature='expenses:process-recurring {--date= : Process recurring expenses for a specific YYYY-MM-DD date}';
    protected $description='Generate recurring expense reminders and pending expense occurrences without posting cash automatically.';

    public function handle(RecurringExpenseService $service, BranchContext $context): int
    {
        return $context->systemWide(function () use ($service): int {
            $date=$this->option('date') ?: now()->toDateString();
            $stats=$service->process((string)$date);
            $this->info("Recurring expenses processed: {$stats['schedules']} schedules, {$stats['reminders']} reminders, {$stats['expenses']} pending expenses generated.");
            return self::SUCCESS;
        });
    }
}
