<#
.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 New-ITDServiceNowServiceCatalogRequest {
    [CmdletBinding()]
    param (
        [string]
        $CategoryItemName,

        [string]
        $RequestedForEmail,

        [hashtable]
        $Values
    )
    
    begin {
        
    }
    
    process {
        Write-Verbose -Message "Retrieve Category Item information"
        $InvokeRestMethodParams = @{
            Method      = 'Get';
            Uri         = ($Script:ServiceNowSession.Uri + "/api/sn_sc/servicecatalog/items?sysparm_limit=5000");
            Headers     = $Script:ServiceNowSession.Headers;
            ContentType = $Script:ServiceNowSession.ContentType;
        }
        
        $CategoryAllItems = (Invoke-RestMethod @InvokeRestMethodParams).result
        
        $CategoryItem = $CategoryAllItems | Where-Object Name -EQ $CategoryItemName
        $CategoryItemSysId = $CategoryItem.sys_id
        
        Write-Verbose -Message "Retrieve RequestedBy and manager information"
        $RequestedForSNUser = Get-ITDServiceNowUser -Email $RequestedForEmail

        $RequiredVariables = @{
            # Requester Information
            v_requested_for            = $RequestedForSNUser.sys_id;
            v_requested_by             = $RequestedForSNUser.sys_id;
            v_user_email               = $RequestedForSNUser.email;
            v_user_phone               = $RequestedForSNUser.phone;
            v_alt_contact              = "";
            
            # Approval Information
            v_approval_department      = "f3c65cef1bfed050bba0113fad4bcb1d"; # Information Technology Dept
            v_approval_department_code = "112.0";
            v_approval_division        = "f40758231b321450bba0113fad4bcb2d"; # Computer Systems
            ###v_approval_division_code   = "32";
            ###v_approval_charge_code     = "";
            v_manager                  = $RequestedForSNUser.manager.value;
            v_user_in_servicenow       = "Yes";

            # unknown
            ###requester_information      = "true";
            ###v_container_requested_for  = "true";
            ###v_user_in_servicenow       = "Yes";
            ###v_container_requested_by   = "true";
            ###approval_information       = "true";
            ###request_commnets           = "true";
            ###request_information        = "true";
        }
        
        $AllVariables = $RequiredVariables
        $Values.Keys | ForEach-Object {
            $AllVariables += @{$_ = $values[$_] }
        }

        $BodyObj = [PSCustomObject]@{
            sysparm_quantity = "1";
            variables        = $AllVariables
        }

        $BodyJson = $BodyObj | ConvertTo-Json
        $InvokeRestMethodParams = @{
            Method      = 'Post';
            Uri         = ($Script:ServiceNowSession.Uri + "/api/sn_sc/servicecatalog/items/$CategoryItemSysId/order_now");
            Headers     = $Script:ServiceNowSession.Headers;
            ContentType = $Script:ServiceNowSession.ContentType;
            Body        = $BodyJson;        
        }

        $InvokeResult = (Invoke-RestMethod @InvokeRestMethodParams).result #create the cart

        #$InvokeResult = (Invoke-RestMethod @InvokeRestMethodParams).result

        # Submit the cart to create the request
        if ($InvokeResult.cart_id) {
            $SubmitOrderParams = @{
                Method      = 'Post'
                Uri         = ($Script:ServiceNowSession.Uri + "/api/sn_sc/servicecatalog/cart/submit_order")
                Headers     = $Script:ServiceNowSession.Headers
                ContentType = $Script:ServiceNowSession.ContentType
                Body        = (@{ cart_id = $InvokeResult.cart_id } | ConvertTo-Json)
            }
            $SubmitResult = (Invoke-RestMethod @SubmitOrderParams).result
        } else {
            $SubmitResult = $InvokeResult
        }
    }
    end {
        Write-Output $SubmitResult
    }
}