<?php

namespace App\Console\Commands;

use App\Services\BackupScheduling\BackupScheduleDispatcher;
use Illuminate\Console\Command;

class TenantpilotDispatchBackupSchedules extends Command
{
    protected $signature = 'tenantpilot:schedules:dispatch {--tenant=* : Limit to managed_environment_id/external_id}';

    protected $description = 'Dispatch due backup schedules (idempotent per schedule minute-slot).';

    public function handle(BackupScheduleDispatcher $dispatcher): int
    {
        $tenantIdentifiers = (array) $this->option('tenant');

        $result = $dispatcher->dispatchDue($tenantIdentifiers);

        $this->info(sprintf(
            'Scanned %d schedule(s), created %d run(s), skipped %d duplicate run(s).',
            $result['scanned_schedules'],
            $result['created_runs'],
            $result['skipped_runs'],
        ));

        return self::SUCCESS;
    }
}
