<#
.SYNOPSIS
    A short one-line action-based description, e.g. 'Tests if a function is valid'
.DESCRIPTION
    A longer description of the function, its purpose, common use cases, etc.
.NOTES
    Information or caveats about the function e.g. 'This function is not supported in Linux'
.LINK
    Specify a URI to a help page, this will show when Get-Help -Online is used.
.EXAMPLE
    Test-MyTestFunction -Verbose
    Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
#>

function Update-ITDServiceNowRecord {
    [CmdletBinding()]
    param (
        [Parameter(ParameterSetName = 'ItemType')]
        [ValidateSet('Incident', 'Change Request', 'User', 'Catalog Task', 'Request Item')]
        [string]
        $ItemType,

        [Parameter(ParameterSetName = 'Table')]
        [string]
        $Table,

        [string]
        $SysId,

        [string]
        $Number,

        [hashtable]
        $Values
    )
    
    begin {
        

    }
    
    process {
        switch ($PSCmdlet.ParameterSetName) {
            'ItemType' {
                $Table = Get-ITDServiceNowTable -ItemType $ItemType
            }
            'Table' {
                # examples: 'cmdb_ci_win_server', 'cmdb_ci_ip_network', 'cmdb_ci_service_auto', 'sc_item_option_mtom'
                # don't do anything
            }
        }

        $Uri = ($Script:ServiceNowSession.Uri + "/api/now/table/" + $Table + "?")
        #$Uri = ('https://northdakotatest.service-now.com' + "/api/now/table/" + $Table + "?")

        switch ($PSBoundParameters.Keys) {
            SysId {
                Write-Verbose -Message ("sysid passed " + $PSBoundParameters.Item('sysid'))
                $Uri += "sys_id=$SysId"
            }
            Number {
                Write-Verbose -Message ("number passed " + $PSBoundParameters.Item('number'))
                $Uri += "sysparm_query=number=$Number"
            }
        }
        
        Write-Verbose $uri
        $InvokeRestMethodParams = @{
            Method      = 'Get';
            Uri         = $Uri
            #Uri         = ($Script:ServiceNowSession.Uri + "/api/now/table/" + $Table + "?sysparm_query=number=" + $Number + "&sysparm_limit=1");
            Headers     = $Script:ServiceNowSession.Headers;
            ContentType = $Script:ServiceNowSession.ContentType;
        }

        try {
            $RecordSearch = (Invoke-RestMethod @InvokeRestMethodParams).Result
            $Uri = $null
        }
        catch [System.UriFormatException] {
            Write-Error "Invalid URI session.  Did you run New-ITDServiceNowSession before execution?"
        }

        switch (@($RecordSearch).count) {  # @($Result).count
            { $_ -lt 1 } {
                Write-Error -Message "Record not found"
            }
            { $_ -gt 1 } {
                Write-Error -Message "More than one record found"
            }
            { $_ -eq 1 } {
                Write-Verbose -Message 'Exactly one record found'
                $SysId = $RecordSearch.sys_id

                $Uri = ($Script:ServiceNowSession.Uri + "/api/now/table/" + $Table + "/" + $SysId)
                Write-Verbose $Uri
                $InvokeRestMethodParams = @{
                    Method      = 'Patch';
                    Uri         = $Uri
                    Body        = $Values | ConvertTo-Json;
                    Headers     = $Script:ServiceNowSession.Headers;
                    ContentType = $Script:ServiceNowSession.ContentType;
                }

                try {
                    $UpdateResult = (Invoke-RestMethod @InvokeRestMethodParams).Result
                }
                catch [System.UriFormatException] {
                    Write-Error "Invalid URI session.  Did you run New-ITDServiceNowSession before execution?"
                }
            }
        }
    }
    
    end {
        Write-Output $UpdateResult
    }
}