param (
    [string]$serviceName,
    [string]$option,
    [string]$appPath,
    [string]$command, # just the command like run start or what ever you have in npm.
    [string]$description,
    [string]$remote,
    [string]$server,
    [string]$username,
    [string]$admpass
)

# Example string to run with the parameters in it.
# .\scripts\services.ps1 -serviceName "LSTV3_app" -option "install" -appPath "D:\LST_V3" -description "Logistics Support Tool" -command "run start"

# server migrations get - reminder to add to old version in pkg "start:lst": "cd lstV2 && npm start",
# powershell.exe -ExecutionPolicy Bypass -File .\scripts\services.ps1 -serviceName "LST_app" -option "install" -appPath "D:\LST" -description "Logistics Support Tool" -command "run start"
# powershell.exe -ExecutionPolicy Bypass -File .\scripts\services.ps1 -serviceName "LSTV3_app" -option "install" -appPath "D:\LST_V3" -description "Logistics Support Tool" -command "run start"
# powershell.exe -ExecutionPolicy Bypass -File .\scripts\services.ps1 -serviceName "LSTV2" -option "install" -appPath "D:\LST" -description "Logistics Support Tool" -command "run start:lst"

$nssmPath = $AppPath + "\nssm.exe"
$npmPath = "C:\Program Files\nodejs\npm.cmd"  # Path to npm.cmd

if ($remote -eq "true") {
    # Convert the plain-text password to a SecureString
    $securePass = ConvertTo-SecureString $admpass -AsPlainText -Force
    $credentials = New-Object System.Management.Automation.PSCredential($username, $securePass)

    #     if(-not $username -or -not $admpass){
    #     Write-host "Missing adm account info please try again."
    #     exit 1 
    # }

    $plantFunness = {
        param ($service, $processType, $location)
        # Call your PowerShell script inside plantFunness
        # & "$($location)\dist\server\scripts\services.ps1" -serviceName $service -option $processType -appPath $location

        if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
            Write-Host "Error: This script must be run as Administrator."
            exit 1
        }
        
        if (-not $service -or -not $processType) {
            Write-host "The service name or option is missing please enter one of them and try again."
            exit 1 
        }
        
        if ($processType -eq "start") {
            write-host "Starting $($service)."
            Start-Service $service
        }
        
        if ($processType -eq "stop") {
            write-host "Stoping $($service)."
            Stop-Service $service
        }
        
        if ($processType -eq "restart") {
            write-host "Stoping $($service) to be restarted"
            Stop-Service $service
            Start-Sleep 3 # so we give it enough time to fully stop
            write-host "Starting $($service)"
            Start-Service $service
        }
        
        if ($processType -eq "prodStop") {
            if (-not $location) {
                Write-host "The path to the app is missing please add it in and try again."
                exit 1 
            }
            & $nssmPath stop $service
            write-host "Removing $($service)"
            #& $nssmPath remove $serviceName confirm
            sc.exe config $service start= disabled
        
        }
        
        if ($processType -eq "prodStart") {
            if (-not $location) {
                Write-host "The path to the app is missing please add it in and try again."
                exit 1 
            }
            & $nssmPath start $service
            write-host "Removing $($service)"
            #& $nssmPath remove $serviceName confirm
            sc.exe config $service start= auto
        
        }

    }

    Invoke-Command -ComputerName $server -ScriptBlock $plantFunness -ArgumentList $serviceName, $option, $appPath -Credential $credentials
}
else {

    if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
        Write-Host "Error: This script must be run as Administrator."
        exit 1
    }
    
    if (-not $serviceName -or -not $option) {
        Write-host "The service name or option is missing please enter one of them and try again."
        exit 1 
    }
    
    if ($option -eq "start") {
        write-host "Starting $($serviceName)."
        Start-Service $serviceName
    }
    
    if ($option -eq "stop") {
        write-host "Stoping $($serviceName)."
        Stop-Service $serviceName
    }
    
    if ($option -eq "restart") {
        write-host "Stoping $($serviceName) to be restarted"
        Stop-Service $serviceName
        Start-Sleep 3 # so we give it enough time to fully stop
        write-host "Starting $($serviceName)"
        Start-Service $serviceName
    }
    
    if ($option -eq "delete") {
        if (-not $appPath) {
            Write-host "The path to the app is missing please add it in and try again."
            exit 1 
        }
        & $nssmPath stop $serviceName
        write-host "Removing $($serviceName)"
        & $nssmPath remove $serviceName confirm
    
    }
    
    if ($option -eq "prodStop") {
        if (-not $appPath) {
            Write-host "The path to the app is missing please add it in and try again."
            exit 1 
        }
        & $nssmPath stop $serviceName
        write-host "Removing $($serviceName)"
        #& $nssmPath remove $serviceName confirm
        sc.exe config $serviceName start= disabled
    
    }
    
    if ($option -eq "prodStart") {
        if (-not $appPath) {
            Write-host "The path to the app is missing please add it in and try again."
            exit 1 
        }
        & $nssmPath start $serviceName
        write-host "Removing $($serviceName)"
        #& $nssmPath remove $serviceName confirm
        sc.exe config $serviceName start= auto
    
    }
    
    if ($option -eq "install") {
        if (-not $appPath -or -not $description -or -not $command) {
            Write-host "Please check all parameters are passed to install the app.."
            exit 1 
        }
        
        
        $service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
    
        if (-not $service) {
            write-host $serviceName "is not installed we will install it now"
        
            Write-Host "Installing $serviceName..."
            if ($command.Contains(".exe")) {

                & $nssmPath install $serviceName $command
                
                $fullAppPath = $appPath
                & $nssmPath set $serviceName AppDirectory $fullAppPath
            }
            else {
                & $nssmPath install $serviceName $npmPath $command 
                & $nssmPath set $serviceName AppDirectory $appPath
            }
            

            & $nssmPath set $serviceName Description $description
            & $nssmPath set $serviceName AppStdout "$($appPath)\logs\service.log"
            & $nssmPath set $serviceName AppStderr "$($appPath)\logs\service-error.log"
            #& $nssmPath set $serviceName DependOnService "MSSQLSERVER"
            # Set recovery options
            sc.exe failure $serviceName reset= 0 actions= restart/5000/restart/5000/restart/5000
            & $nssmPath start $serviceName
        }
        else {
            write-host $serviceName "is already installed will push the updated info"
            Write-Host "Updating $serviceName..."
            & $nssmPath stop $serviceName

            if ($command.Contains(".exe")) {
                $fullAppPath = "$appPath\app"
                & $nssmPath set $serviceName AppDirectory $fullAppPath
            }
            else {
                & $nssmPath set $serviceName AppDirectory $appPath
            }
            
            & $nssmPath set $serviceName Description $description
            # & $nssmPath set $serviceName DependOnService "IISADMIN MSSQLSERVER"
            # Set recovery options
            sc.exe failure $serviceName reset= 0 actions= restart/5000/restart/5000/restart/5000
            Start-Sleep 4
            & $nssmPath start $serviceName
        }
    }
}
