/**
 * get-installed-software — list installed Win32 + WoW64 applications.
 * Asset-self. Useful for verifying app-version fields on Configuration
 * records and for spotting unexpected installs.
 */

import { type RmmScript, parseJsonOutput } from './types';

export const getInstalledSoftware: RmmScript = {
  id: 'get-installed-software',
  name: 'Installed software',
  description:
    'Win32 + WoW64 uninstall registry — DisplayName, DisplayVersion, Publisher, InstallDate.',
  target_type: 'asset_self',
  expected_runtime_seconds: 20,
  version: 1,
  body: `
$ErrorActionPreference = 'Stop'
$paths = @(
  'HKLM:\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*'
  'HKLM:\\Software\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*'
)
$apps = foreach ($p in $paths) {
  Get-ItemProperty $p -ErrorAction SilentlyContinue |
    Where-Object { $_.DisplayName } |
    Select-Object DisplayName, DisplayVersion, Publisher, InstallDate
}
@{
  hostname = $env:COMPUTERNAME
  captured_at_utc = (Get-Date).ToUniversalTime().ToString("o")
  count = ($apps | Measure-Object).Count
  apps = $apps | Sort-Object DisplayName -Unique
} | ConvertTo-Json -Depth 4 -Compress
`.trim(),
  parseOutput: (stdout) => parseJsonOutput(stdout),
};
