<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Services\Draw\DrawTickService;

final class LotteryDrawTickCommand extends Command
{
    protected $signature = 'lottery:draw-tick';

    protected $description = '封盘、开奖 RNG、补齐期号缓冲（每分钟调度入口）';

    public function handle(DrawTickService $tickService): int
    {
        $report = $tickService->tick();

        $statusSum = array_sum($report['status_updates'] ?? []);
        $this->info(sprintf(
            'Status rows updated: %d | RNG runs: %d | Planned draws created: %d',
            $statusSum,
            $report['rng_rung'],
            $report['planned']['created'] ?? 0,
        ));

        foreach ($report['rng_errors'] as $err) {
            $this->warn($err);
        }

        return self::SUCCESS;
    }
}
