package api

import (
	"fmt"
	"net/http"
	"os"

	"github.com/gin-gonic/gin"
)

func handleInstallScriptWindows(c *gin.Context) {
	serverID := c.Query("server_id")
	token := c.Query("token")

	giteaHost := "gitea.hostxtra.co.uk"

	grpcHost := os.Getenv("GRPC_HOST")

	script := fmt.Sprintf(
		"#Requires -RunAsAdministrator\n"+
			"$ErrorActionPreference = \"Stop\"\n"+
			"\n"+
			"$ServerId = \"%s\"\n"+
			"$Token = \"%s\"\n"+
			"$GiteaHost = \"%s\"\n"+
			"$ServerUrl = \"%s\" -replace '^https?://',''\n"+
			"\n"+
			"$rel = Invoke-RestMethod -Uri \"https://$GiteaHost/api/v1/repos/mrhid6/vantage/releases?limit=10\"\n"+
			"$tag = ($rel | Where-Object { $_.tag_name -like 'agent/v*' } | Select-Object -First 1).tag_name\n"+
			"if (-not $tag) { throw \"Could not determine latest agent version\" }\n"+
			"$enc = $tag -replace '/','%%2F'\n"+
			"$base = \"https://$GiteaHost/mrhid6/vantage/releases/download/$enc\"\n"+
			"\n"+
			"$tmp = Join-Path $env:TEMP \"vantage-agent.msi\"\n"+
			"Invoke-WebRequest -Uri \"$base/vantage-agent.msi\" -OutFile $tmp\n"+
			"Invoke-WebRequest -Uri \"$base/checksums-msi.txt\" -OutFile \"$env:TEMP\\checksums-msi.txt\"\n"+
			"\n"+
			"$expected = (Get-Content \"$env:TEMP\\checksums-msi.txt\" | Select-String 'vantage-agent.msi').ToString().Split()[0]\n"+
			"$actual = (Get-FileHash $tmp -Algorithm SHA256).Hash.ToLower()\n"+
			"if ($expected -ne $actual) { throw \"Checksum mismatch\" }\n"+
			"\n"+
			"Start-Process msiexec.exe -Wait -ArgumentList \"/i `\"$tmp`\" /qn SERVERID=$ServerId TOKEN=$Token SERVERURL=$ServerUrl\"\n"+
			"Write-Host \"Vantage agent installed.\"\n",
		serverID, token, giteaHost, grpcHost)

	c.Header("Content-Type", "text/plain; charset=utf-8")
	c.String(http.StatusOK, script)
}

func handleUpdateScriptWindows(c *gin.Context) {
	giteaHost := "gitea.hostxtra.co.uk"

	script := fmt.Sprintf(
		"#Requires -RunAsAdministrator\n"+
			"$ErrorActionPreference = \"Stop\"\n"+
			"\n"+
			"$GiteaHost = \"%s\"\n"+
			"\n"+
			"$rel = Invoke-RestMethod -Uri \"https://$GiteaHost/api/v1/repos/mrhid6/vantage/releases?limit=10\"\n"+
			"$tag = ($rel | Where-Object { $_.tag_name -like 'agent/v*' } | Select-Object -First 1).tag_name\n"+
			"if (-not $tag) { throw \"Could not determine latest agent version\" }\n"+
			"$enc = $tag -replace '/','%%2F'\n"+
			"$base = \"https://$GiteaHost/mrhid6/vantage/releases/download/$enc\"\n"+
			"\n"+
			"$tmp = Join-Path $env:TEMP \"vantage-agent.msi\"\n"+
			"Invoke-WebRequest -Uri \"$base/vantage-agent.msi\" -OutFile $tmp\n"+
			"Invoke-WebRequest -Uri \"$base/checksums-msi.txt\" -OutFile \"$env:TEMP\\checksums-msi.txt\"\n"+
			"\n"+
			"$expected = (Get-Content \"$env:TEMP\\checksums-msi.txt\" | Select-String 'vantage-agent.msi').ToString().Split()[0]\n"+
			"$actual = (Get-FileHash $tmp -Algorithm SHA256).Hash.ToLower()\n"+
			"if ($expected -ne $actual) { throw \"Checksum mismatch\" }\n"+
			"\n"+
			"Start-Process msiexec.exe -Wait -ArgumentList \"/i `\"$tmp`\" /qn /norestart\"\n"+
			"Write-Host \"Vantage agent updated to $tag.\"\n",
		giteaHost)

	c.Header("Content-Type", "text/plain; charset=utf-8")
	c.String(http.StatusOK, script)
}
