#!powershell

#AnsibleRequires -CSharpUtil Ansible.Basic
#AnsibleRequires -PowerShell Ansible.ModuleUtils.CommandUtil

$module = [Ansible.Basic.AnsibleModule]::Create($args, @{
    options = @{
        name = @{
            type = "list"
            elements = "str"
            required = $true
        }
        state = @{
            type = "str"
            default = "present"
            choices = ("absent", "latest", "present")
        }
    }
    supports_check_mode = $false
})

$names = $module.Params.name
$state = $module.Params.state
$winget = Get-ExecutablePath "winget"
$noPackageString = "No installed package found matching input criteria."

if ($name -and $id) {
    $module.FailJson("name `"$name`" and id `"$id`" must not both be provided")
}

function Test-Installed {
    $command = "`"$winget`" list `"$name`""
    $result = Run-Command -command $command
    if ($result.rc -eq 0) {
        return $true
    }
    return $false
}

function Test-UpgradeAvailable {
    if (!(Test-Installed)) {
        return $true
    }
    $command = "`"$winget`" list --upgrade-available `"$name`""
    $result = Run-Command -command $command
    if ($result.stdout.Contains($noPackageString)) {
        return $false
    }
    return $true
}

foreach ($name in $names) {
    switch ($state) {
        "absent" {
            if (Test-Installed) {
                $command = "`"$winget`" uninstall `"$name`""
                $result = Run-Command -command $command
                if ($result.rc -ne 0) {
                    $module.Result.rc = $result.rc
                    $module.Result.stdout = $result.stdout
                    $module.FailJson("Failed to uninstall package `"$name`"")
                }
                $module.Result.stdout = $result.stdout
                $module.Result.changed = $true
            }
        }

        "latest" {
            if (Test-UpgradeAvailable) {
                $command = "`"$winget`" install --accept-package-agreements --accept-source-agreements `"$name`""
                $result = Run-Command -command $command
                if ($result.rc -ne 0) {
                    $module.Result.rc = $result.rc
                    $module.Result.stdout = $result.stdout
                    $module.FailJson("Failed to install package `"$name`"")
                }
                $module.Result.stdout = $result.stdout
                $module.Result.changed = $true
            }
        }

        "present" {
            if (!(Test-Installed)) {
                $command = "`"$winget`" install --accept-package-agreements --accept-source-agreements `"$name`""
                $result = Run-Command -command $command
                if ($result.rc -ne 0) {
                    $module.Result.rc = $result.rc
                    $module.Result.stdout = $result.stdout
                    $module.FailJson("Failed to install package `"$name`"")
                }
                $module.Result.stdout = $result.stdout
                $module.Result.changed = $true
            }
        }
    }
}

$module.Result.rc = 0
$module.ExitJson()