param (
    [string]$App_Path = "C:\Users\matthes01\Documents\lst", # with this it makes it optional to pass a new parameter over.
    [string]$Token = "test3",
    [string]$Remote_Path = "E$\LST",
    [string]$BuildController = "yes",
    [string]$PlantToUpdate = "all"
)

# imports of the other functions
. ".\update-controller-zip"  
. ".\update-controller-bumpBuild" 
. ".\update-controller-server" 

# example string to pass over, you must be in the script dir when you run this script. or it will fail to find the linked scripts

# If we do not pass plant to update over it will auto do all plants if we want a specific plant we need to do like below 
# .\update-controllers.ps1 -App_Path "C:\Users\matthes01\Documents\lst" -Token "ushou1" -BuildController yes -PlantToUpdate "ushou1vms006" -Remote_Path "E$\LST"
# .\update-controllers.ps1 -App_Path "C:\Users\matthes01\Documents\lst" -Token "test3" -BuildController yes

$Plants = @(
    @{ Server = "usmcd1vms036"; Token = "test3"; Remote_Path = "E$\LST" } 
    @{ Server = "usmar1vms006"; Token = "usmar1" ; Remote_Path = "E$\LST" } 
    @{ Server = "usbet1vms006"; Token = "usbet1"; Remote_Path = "E$\LST" } 
    @{ Server = "usbow1vms006"; Token = "usbow1" ; Remote_Path = "E$\LST" } 
    @{ Server = "usbow2vms006"; Token = "usbow2" ; Remote_Path = "E$\LST" } 
    @{ Server = "usday1vms006"; Token = "usday1" ; Remote_Path = "E$\LST" } 
    @{ Server = "usflo1vms006"; Token = "usflo1" ; Remote_Path = "E$\LST" } 
    @{ Server = "usiow1vms006"; Token = "usiow1" ; Remote_Path = "D$\LST\lst" } 
    @{ Server = "usiow1vms006"; Token = "usiow2" ; Remote_Path = "D$\LST\lst_2" } 
    @{ Server = "uslim1vms006"; Token = "uslim1" ; Remote_Path = "E$\LST" } 
    @{ Server = "usmcd1vms006"; Token = "usmcd1" ; Remote_Path = "E$\LST" } 
    @{ Server = "usslc1vms006"; Token = "usslc1" ; Remote_Path = "E$\LST" }
    @{ Server = "usstp1vms006"; Token = "usstp1" ; Remote_Path = "D$\LST" }
    @{ Server = "usksc1vms006"; Token = "usksc1" ; Remote_Path = "E$\LST" }
    @{ Server = "ushou1vms006"; Token = "ushou1" ; Remote_Path = "E$\LST" }  #15
    @{ Server = "usshe1vms006"; Token = "usshe1" ; Remote_Path = "E$\LST" }#16
    @{ Server = "usweb1vms006"; Token = "usweb1" ; Remote_Path = "E$\LST" }#17
    @{ Server = "usjci1vms006"; Token = "usjci1" ; Remote_Path = "E$\LST" }

)

$EnvFile = Join-Path $App_Path ".env"
if (-Not (Test-Path $EnvFile)) {
    Write-Error "File not found: $EnvFile"
    exit 1
}

# Read and parse the .env file into a dictionary
$envDict = @{}

Get-Content $EnvFile | ForEach-Object {
    if ($_ -match "^\s*#") { return }   # skip comments
    if ([string]::IsNullOrWhiteSpace($_)) { return } # skip blank lines
    $parts = $_ -split '=', 2
    if ($parts.Count -eq 2) {
        $envDict[$parts[0].Trim()] = $parts[1].Trim()
    }
}

# Read the .env file and set each key as a variable
Get-Content $EnvFile | ForEach-Object {
    if ($_ -match "^\s*#") { return }   # skip comments
    if ([string]::IsNullOrWhiteSpace($_)) { return } # skip blank lines

    $parts = $_ -split '=', 2
    if ($parts.Count -eq 2) {
        $key = $parts[0].Trim()
        $value = $parts[1].Trim()

        # Set as PowerShell variable
        Set-Variable -Name $key -Value $value -Scope Global
    }
}

# create the new zip build.
# ---------------------------------------------------------------------------------
#check it file is there
$BuildNumber = "1"
$buildfile = Join-Path $App_Path ".controller-build"

if (-Not (Test-Path $buildfile)) {
    Write-Error "File not found: $buildfile, creating it."
    New-Item -Path $buildfile -ItemType File | Out-Null
    $BuildNumber = "1"
    Set-Content -Path $BuildFile -Value $BuildNumber
}
else {
    $content = Get-Content $BuildFile -Raw
    if (![string]::IsNullOrWhiteSpace($content)) {
        $BuildNumber = $content.Trim()
    }
        
}


# do the same check on the .includeControls file
$ControlIncludes = Join-Path $App_Path ".includeControls"
if (-Not (Test-Path $ControlIncludes)) {
    Write-Error "File not found: $ControlIncludes, exiting as we dont want to zip up the entire app."
    exit 1
}


# last one to make sure we have is the build folder
$BuildFolder = Join-Path $App_Path "controllerBuilds"

if (-Not (Test-Path $BuildFolder)) {
    write-host "Build folder missing creating it"
    New-Item -Path $BuildFolder -ItemType Directory | Out-Null
}

# only rebuild if we push yes over
if ($BuildController -eq "yes") {
    # now we can create the zip


    Zip-Includes -IncludesFile $ControlIncludes -BuildNumber $BuildNumber -BuildFolder $BuildFolder -AppRoot $App_Path
    Start-Sleep -Seconds 3
     
}
else {
    $BuildNumber = ([int]$BuildNumber - 1).ToString()
}
#------------------------------------------------------------------------------------

# now that the server is up lets update the server with the current build
# do the update to the plant.
if ($PlantToUpdate -ne "all") {

    # Checking to make sure the server is up and online
    Write-Output "Checking if $($Token) is online to update."
    $pingResult = Test-Connection -ComputerName $PlantToUpdate -Count 2 -Quiet

    if (-not $pingResult) {
        Write-Output "Server $($PlantToUpdate), token $($Token) is NOT reachable. Exiting script."
        exit 1 # Terminate the script with a non-zero exit code
    }

    Write-Output "Server $($PlantToUpdate), token $($Token) is online."

    Update-Server -ADM_USER $ADM_USER -ADM_PASS $ADM_PASS -AppRoot $App_Path -Destination $Remote_Path -Server $PlantToUpdate -Token $Token -ControllerBuild $BuildController
}
else {
    foreach ($plant in $Plants) {

        # Checking to make sure the server is up and online
        Write-Output "Checking if $($plant.Token) is online to update."
        $pingResult = Test-Connection -ComputerName $plant.Server -Count 2 -Quiet

        if (-not $pingResult) {
            Write-Output "Server $($plant.Server), token $($plant.Token) is NOT reachable. Exiting script."
            exit 1 # Terminate the script with a non-zero exit code
        }

        Write-Output "Server $($plant.Server), token $($plant.Token) is online."
        Write-Output "Plant to update: $($plant.Token)"
        Update-Server -ADM_USER $ADM_USER -ADM_PASS $ADM_PASS -AppRoot $App_Path -Destination $plant.Remote_Path -Server $plant.Server -Token $plant.Token
    }
}


# bump the build number 
if ($BuildController -eq "yes") {
    $BuildNumber = Bump-Build -AppRoot $App_Path
}

# function to create the zip controller-1.zip

