param(
  [int]$UpdateWindowSeconds = 43200,
  [switch]$Help
)

if ($Help) {
  @"
Usage: powershell -NoProfile -ExecutionPolicy Bypass -File scripts/cli-state.ps1 [-UpdateWindowSeconds 43200]

Print install state, update-check window status, and API key status for Windows.
"@
  exit 0
}

$commonScript = Join-Path $PSScriptRoot "_common.ps1"
if (-not (Test-Path -LiteralPath $commonScript)) {
  $commonScript = Join-Path $PSScriptRoot "_common.txt"
}
. $commonScript

if ($UpdateWindowSeconds -lt 0) {
  Fail-TencentNews "-UpdateWindowSeconds must be a non-negative integer."
}

try {
  $platform = Get-TencentNewsPlatformInfo
  $lastCheckEpoch = Read-TencentNewsLastCheckEpoch -Path $platform.lastCheckFile
  $nowEpoch = [DateTimeOffset]::UtcNow.ToUnixTimeSeconds()

  Write-TencentNewsJson @{
    platform            = $platform
    cliExists           = Test-Path -LiteralPath $platform.cliPath
    lastCheckEpoch      = $lastCheckEpoch
    nowEpoch            = $nowEpoch
    updateWindowSeconds = $UpdateWindowSeconds
    needsUpdateCheck    = (($nowEpoch - $lastCheckEpoch) -gt $UpdateWindowSeconds)
    apiKey              = Get-TencentNewsApiKeyState -PlatformInfo $platform
  }
} catch {
  Fail-TencentNews $_.Exception.Message
}
