<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Services\CollectionBackgroundJobService;
use Illuminate\Console\Command;

class DispatchCollectionMaintenanceCommand extends Command
{
    protected $signature = 'collections:dispatch-maintenance
        {--health : Dispatch health and eligibility refresh jobs}
        {--recommendations : Dispatch recommendation refresh jobs}
        {--duplicates : Dispatch duplicate scan jobs}';

    protected $description = 'Dispatch queued collection maintenance jobs for health, recommendation, and duplicate workflows.';

    public function handle(CollectionBackgroundJobService $jobs): int
    {
        $runHealth = (bool) $this->option('health');
        $runRecommendations = (bool) $this->option('recommendations');
        $runDuplicates = (bool) $this->option('duplicates');

        if (! $runHealth && ! $runRecommendations && ! $runDuplicates) {
            $runHealth = true;
            $runRecommendations = true;
            $runDuplicates = true;
        }

        $summary = $jobs->dispatchScheduledMaintenance($runHealth, $runRecommendations, $runDuplicates);

        foreach ($summary as $key => $payload) {
            $this->info(sprintf('%s: %d queued.', ucfirst((string) $key), (int) ($payload['count'] ?? 0)));
        }

        return self::SUCCESS;
    }
}