# Samy.Onboarding.ps1
# Onboarding HTTP handlers and rename computer

function Invoke-SetSVSPowerPlan {
    param($Context)

    Set-SVSPowerPlan

    Write-LogHybrid "PowerPlan set" Success OnBoard
    Send-Text  $Context "PowerPlan applied"
}

function Invoke-InstallSVSMSP {
    param($Context)
    Write-LogHybrid "HTTP trigger: Invoke-InstallSVSMSP" Info OnBoard
    try {
        Install-SVSMSP -InstallToolkit
        Send-Text $Context "SVSMSP Module installed/updated."
    }
    catch {
        Write-LogHybrid "Error in Install-SVSMSP: $_" Error OnBoard
        Send-Text $Context "ERROR: $_"
    }
}

function Invoke-InstallCyberQP {
    param($Context)

    Install-CyberQP

    Write-LogHybrid "CyberQP installed" Success OnBoard
    Send-Text $Context "CyberQP installed"
}

function Invoke-InstallThreatLocker {
    param($Context)

    Install-ThreatLocker

    Write-LogHybrid "ThreatLocker installed" Success OnBoard
    Send-Text $Context "ThreatLocker installed"
}

function Invoke-InstallRocketCyber {
    param($Context)

    Install-RocketCyber

    Write-LogHybrid "RocketCyber installed" Success OnBoard
    Send-Text $Context "RocketCyber installed"
}

function Invoke-InstallHelpDesk {
    param($Context)

    Install-svsHelpDesk

    Write-LogHybrid "SVS HelpDesk installed" Success OnBoard
    Send-Text $Context "SVS HelpDesk installed"
}

function Invoke-SetEdgeDefaultSearchEngine {
    param($Context)

    try {
        Write-LogHybrid "Configuring Edge default search provider" Info OnBoard
        set-EdgeDefaultSearchEngine
        Write-LogHybrid "Edge default search set to Google" Success OnBoard
        Send-Text $Context "Edge default search provider configured."
    }
    catch {
        Write-LogHybrid "Failed to set Edge default search: $($_.Exception.Message)" Error OnBoard
        Send-Text $Context "ERROR: $($_.Exception.Message)"
    }
}

function Invoke-RenameComputer {
    param($Context)

    try {
        if ($Context.Request.HttpMethod -ne 'POST') {
            $Context.Response.StatusCode = 405
            Send-Text $Context 'Use POST'
            return
        }

        $rawBody = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
        if (-not $rawBody) {
            $Context.Response.StatusCode = 400
            Send-Text $Context 'Missing request body.'
            return
        }

        try {
            $body = $rawBody | ConvertFrom-Json
        }
        catch {
            $Context.Response.StatusCode = 400
            Send-Text $Context 'Invalid JSON body.'
            return
        }

        $newName = $body.newName

        if (-not (Test-ComputerName -Name $newName)) {
            Write-LogHybrid "RenameComputer: invalid computer name '$newName'." Error OnBoard -LogToEvent
            $Context.Response.StatusCode = 400
            Send-JSON $Context @{
                Success = $false
                Error   = "Invalid computer name. Must be 1-15 characters and use only letters, numbers, and hyphens."
            }
            return
        }

        Write-LogHybrid "RenameComputer: renaming computer to '$newName'." Info OnBoard -LogToEvent

        try {
            Rename-Computer -NewName $newName -Force -ErrorAction Stop
        }
        catch {
            Write-LogHybrid "RenameComputer: rename failed: $($_.Exception.Message)" Error OnBoard -LogToEvent
            $Context.Response.StatusCode = 500
            Send-JSON $Context @{
                Success = $false
                Error   = $_.Exception.Message
            }
            return
        }

        Write-LogHybrid "RenameComputer: rename complete, reboot required for new name to apply." Success OnBoard -LogToEvent

        Send-JSON $Context @{
            Success = $true
            NewName = $newName
            Note    = "Rename successful. A reboot is required for the new name to take effect."
        }
    }
    catch {
        Write-LogHybrid "Invoke-RenameComputer fatal error: $($_.Exception.Message)" Error OnBoard -LogToEvent
        $Context.Response.StatusCode = 500
        Send-Text $Context "Internal error during computer rename."
    }
}
