import type { Client } from 'ssh2'
import { execCommand } from './common.js'

export async function collectUptimeMetrics(
  client: Client,
): Promise<{ seconds: number | null; formatted: string | null }> {
  try {
    const { stdout } = await execCommand(client, 'cat /proc/uptime')
    const seconds = Number(stdout.trim().split(/\s+/)[0])
    if (!Number.isFinite(seconds)) return { seconds: null, formatted: null }
    const days = Math.floor(seconds / 86400)
    const hours = Math.floor((seconds % 86400) / 3600)
    const minutes = Math.floor((seconds % 3600) / 60)
    return { seconds, formatted: `${days}d ${hours}h ${minutes}m` }
  } catch {
    return { seconds: null, formatted: null }
  }
}
