# install-as-service.ps1
# Registers WindroseServerManager.Web.exe (in this folder) as a Windows service.
# Run from an elevated PowerShell. Idempotent — safe to re-run after config changes.
#
# Usage:
#   .\install-as-service.ps1
#   .\install-as-service.ps1 -InstallDir "D:\WSM"
#   .\install-as-service.ps1 -ServiceName "MyWSM" -DisplayName "My Windrose Manager"

[CmdletBinding()]
param(
    [string]$InstallDir = "C:\Program Files\WindroseServerManager",
    [string]$ServiceName = "WindroseServerManager",
    [string]$DisplayName = "Windrose Server Manager",
    [string]$Description = "Manages a Windrose dedicated server: RCON polling, log tail, restart automation, Discord webhook routing."
)

$ErrorActionPreference = "Stop"

if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Write-Error "Run this from an elevated (Administrator) PowerShell. Right-click PowerShell -> Run as administrator."
    exit 1
}

$source = $PSScriptRoot
$targetApp = Join-Path $InstallDir "app"
$exe = Join-Path $targetApp "WindroseServerManager.Web.exe"
$exeSrc = Join-Path $source "WindroseServerManager.Web.exe"

if (-not (Test-Path $exeSrc)) {
    Write-Error "Can't find WindroseServerManager.Web.exe in $source. Run this script from inside the unzipped release folder."
    exit 1
}

# Stop existing service if present so we can overwrite the exe.
$existing = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if ($existing -and $existing.Status -ne 'Stopped') {
    Write-Host "Stopping existing $ServiceName service..."
    Stop-Service -Name $ServiceName -Force -ErrorAction SilentlyContinue
    Start-Sleep -Seconds 2
}

Write-Host "Copying $source -> $targetApp"
New-Item -ItemType Directory -Path $targetApp -Force | Out-Null
# Copy everything EXCEPT this script and the uninstall script
Get-ChildItem $source -Exclude 'install-as-service.ps1','uninstall-service.ps1' | Copy-Item -Destination $targetApp -Recurse -Force

# Reminder if no Local.json was placed in $targetApp before re-running
$localCfgInTarget = Join-Path $targetApp "appsettings.Local.json"
if (-not (Test-Path $localCfgInTarget)) {
    Write-Host ""
    Write-Host "NOTE: copy appsettings.Local.minimal.json (or .example.json) -> appsettings.Local.json"
    Write-Host "       in $targetApp and edit it before starting the service."
    Write-Host ""
}

# Create or recreate the service.
if ($existing) {
    Write-Host "Removing existing $ServiceName service..."
    sc.exe delete $ServiceName | Out-Null
    Start-Sleep -Seconds 2
}

Write-Host "Creating service $ServiceName"
$binPath = "`"$exe`""
sc.exe create $ServiceName binPath= $binPath start= auto DisplayName= "$DisplayName" | Out-Null
sc.exe description $ServiceName $Description | Out-Null
sc.exe failure $ServiceName reset= 3600 actions= restart/5000/restart/10000/restart/30000 | Out-Null

Write-Host ""
Write-Host "=== Installed ==="
Write-Host "  Service:     $ServiceName"
Write-Host "  Exe:         $exe"
Write-Host "  Working dir: $targetApp"
Write-Host "  Logs:        $targetApp\logs\wsm-*.log"
Write-Host ""
Write-Host "Next steps:"
Write-Host "  1. Edit:   $targetApp\appsettings.Local.json   (copy from .minimal.json or .example.json if missing)"
Write-Host "  2. Start:  Start-Service $ServiceName"
Write-Host "  3. Open:   http://localhost:8080"
Write-Host ""
Write-Host "Status:    Get-Service $ServiceName"
Write-Host "Stop:      Stop-Service $ServiceName"
Write-Host "Uninstall: .\uninstall-service.ps1"
