<?php

declare(strict_types=1);

namespace App\Support\OpsUx;

use App\Models\OperationRun;

final class RunDetailPolling
{
    /**
     * Returns a Livewire polling interval string (e.g. "1s") while the run is active.
     * Returns null once the run is terminal.
     */
    public static function interval(OperationRun $run): ?string
    {
        $uxStatus = OperationStatusNormalizer::toUxStatus($run->status, $run->outcome);

        if (! in_array($uxStatus, ['queued', 'running'], true)) {
            return null;
        }

        $ageSeconds = $run->created_at?->diffInSeconds(now()) ?? 0;
        $ageSeconds = abs((int) $ageSeconds);

        if ($ageSeconds < 10) {
            return '1s';
        }

        if ($ageSeconds < 60) {
            return '5s';
        }

        return '10s';
    }
}
