param(
  [string]$InstallRoot = "C:\Tools\ProxiFyre",
  [string]$PackagePath = "",
  [string]$ServiceName = "ProxiFyreService",
  [switch]$PlanOnly,
  [switch]$Force
)

$ErrorActionPreference = "Stop"

function New-Result {
  param(
    [bool]$Success,
    [string]$Action,
    [bool]$Changed,
    [string]$Message,
    [hashtable]$Details = @{}
  )

  [ordered]@{
    success = $Success
    action = $Action
    changed = $Changed
    message = $Message
    details = $Details
  } | ConvertTo-Json -Depth 6
}

function Test-IsAdministrator {
  $identity = [Security.Principal.WindowsIdentity]::GetCurrent()
  $principal = [Security.Principal.WindowsPrincipal]::new($identity)
  $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}

function Backup-File {
  param([string]$Path)
  if (Test-Path -LiteralPath $Path) {
    $backup = "$Path.bak"
    Copy-Item -LiteralPath $Path -Destination $backup -Force
    return $backup
  }
  return $null
}

try {
  $details = @{
    installRoot = $InstallRoot
    packagePath = $PackagePath
    serviceName = $ServiceName
    planOnly = [bool]$PlanOnly
  }

  if ($PlanOnly) {
    New-Result -Success $true -Action "install-proxyfier" -Changed $false -Message "Proxyfier install plan is ready." -Details $details
    exit 0
  }

  if (-not (Test-IsAdministrator)) {
    New-Result -Success $false -Action "install-proxyfier" -Changed $false -Message "Administrator rights are required." -Details $details
    exit 1
  }

  if ([string]::IsNullOrWhiteSpace($PackagePath) -or -not (Test-Path -LiteralPath $PackagePath)) {
    New-Result -Success $false -Action "install-proxyfier" -Changed $false -Message "PackagePath is required and must point to a local ProxiFyre package." -Details $details
    exit 2
  }

  $changed = $false
  if (-not (Test-Path -LiteralPath $InstallRoot)) {
    New-Item -ItemType Directory -Path $InstallRoot -Force | Out-Null
    $changed = $true
  }

  $configPath = Join-Path $InstallRoot "app-config.json"
  $backupPath = Backup-File -Path $configPath
  if ($backupPath) {
    $details.backupPath = $backupPath
  }

  $markerPath = Join-Path $InstallRoot "install-proxyfier.marker.json"
  if ((-not (Test-Path -LiteralPath $markerPath)) -or $Force) {
    @{
      component = "proxyfier"
      packagePath = $PackagePath
      serviceName = $ServiceName
      installedAt = (Get-Date).ToString("o")
    } | ConvertTo-Json -Depth 4 | Set-Content -LiteralPath $markerPath -Encoding UTF8
    $changed = $true
  }

  $details.markerPath = $markerPath
  New-Result -Success $true -Action "install-proxyfier" -Changed $changed -Message "Proxyfier install boundary completed." -Details $details
} catch {
  New-Result -Success $false -Action "install-proxyfier" -Changed $false -Message $_.Exception.Message
  exit 1
}
