$ErrorActionPreference = 'Stop'

$client = Get-Process client -ErrorAction SilentlyContinue | Select-Object -First 1
$connections = @()
if ($client) {
    $connections = @(
        Get-NetTCPConnection -OwningProcess $client.Id -ErrorAction SilentlyContinue |
            Where-Object State -eq 'Established' |
            Select-Object LocalAddress, LocalPort, RemoteAddress, RemotePort, State
    )
}

$logs = @(
    Get-ChildItem "$env:LOCALAPPDATA\Tibia", 'C:\Program Files (x86)\Tibia' `
        -Recurse -File -ErrorAction SilentlyContinue |
        Where-Object {
            $_.Extension -in '.log', '.txt' -or
            $_.Name -match 'log|crash|battle'
        } |
        Sort-Object LastWriteTime -Descending |
        Select-Object -First 20 FullName, Length, LastWriteTime
)

[ordered]@{
    client = if ($client) {
        [ordered]@{
            id = $client.Id
            title = $client.MainWindowTitle
            responding = $client.Responding
        }
    } else {
        $null
    }
    connections = $connections
    processes = @(
        Get-Process obs64, BEService -ErrorAction SilentlyContinue |
            Select-Object Name, Id, MainWindowTitle
    )
    logs = $logs
} | ConvertTo-Json -Depth 5
