param(
  [ValidatePattern('^[a-p]{32}$')]
  [string]$ExtensionId = "moknobknopbaogdblkecaebnlmpdddai"
)

$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest

$hostName = "io.veck.relative_input"
$hostDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path
$manifestPath = Join-Path $hostDirectory "$hostName.json"
$executablePath = Join-Path $hostDirectory "veck-relative-input.exe"

if (-not (Test-Path -LiteralPath $executablePath -PathType Leaf)) {
  throw "Executável nativo não encontrado: $executablePath"
}

$manifest = [ordered]@{
  name = $hostName
  description = "Movimento relativo limitado com vertical seguro para cima para Veck Aim Assist"
  path = $executablePath
  type = "stdio"
  allowed_origins = @("chrome-extension://$ExtensionId/")
}
$manifestJson = $manifest | ConvertTo-Json -Depth 4
[System.IO.File]::WriteAllText($manifestPath, $manifestJson, [System.Text.UTF8Encoding]::new($false))

$registryPaths = @(
  "HKCU:\Software\Google\Chrome\NativeMessagingHosts\$hostName",
  "HKCU:\Software\Chromium\NativeMessagingHosts\$hostName"
)

foreach ($registryPath in $registryPaths) {
  $null = New-Item -Path $registryPath -Force
  Set-Item -Path $registryPath -Value $manifestPath
}

Write-Output "Host nativo registrado para o usuário atual: $manifestPath"
