<#
.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
    $NewITDVMSNowRitmParams = @{
        RequestType           = "New";
        ComputerName          = "itdzmtest100.nd.gov";
        SysadminEmail         = "zmeier@nd.gov";
        CustomerRitm          = "RITM0145886";
        Environment           = "Test";
        AppName               = "Infra-VMware";
        Comments              = "New VM for VMware sandbox, see RITM0145886 for details";
        OperatingSystem       = "Windows";
        TargetOSVersion       = "Windows Server 2022 Datacenter";
        TargetPlatform        = "VMware";
        NumCpu                = 1;
        MemoryGB              = 4;
        Disk1GB               = 50;
        Disk2GB               = 5;
        CIDR                  = '10.11.12.0/23';
        Datacenter            = 'Bismarck';
        LicensingRestrictions = 'No Licensing Restrictions';
        AgencyPrefix          = 'ITD';
        SupportHours          = 'All Day Every Day';
        DRProtection          = 'No DR';
        StartupPriority       = 5;
    }
    New-ITDVMSNowRitm @NewITDVMSNowRitmParams
.EXAMPLE
    New-ITDVMSNowRitm -ImportCsv "C:\temp\NewITDVMSnowRitm.csv"
#>

function New-ITDVMSNowRitm {
    [CmdletBinding()]
    param (
        [Parameter(ParameterSetName = 'Csv')]
        [string]
        $ImportCsv,

        [Parameter(ParameterSetName = 'Single', Mandatory = $true)]
        [ValidateSet('New', 'Upgrade/Code Deployment', 'Removal', 'Other')]
        [string]
        $RequestType,

        [Parameter(ParameterSetName = 'Single', Mandatory = $true)]
        [string]
        $ComputerName,

        [Parameter(ParameterSetName = 'Single', Mandatory = $true)]
        [string]
        $SysadminEmail,

        [Parameter(ParameterSetName = 'Single', Mandatory = $true)]
        [string]
        $Comments,

        [Parameter(ParameterSetName = 'Single', Mandatory = $true)]
        [ValidateSet('Production', 'Test')]
        [string]
        $Environment,

        [Parameter(ParameterSetName = 'Single', Mandatory = $true)]
        [string]
        $AppName,

        [Parameter(ParameterSetName = 'Single')]
        [ValidateSet('Linux', 'Windows')]
        [string]
        $OperatingSystem,

        [Parameter(ParameterSetName = 'Single')]
        [ValidateSet(
            'Windows Server 2019 Datacenter',
            'Windows Server 2022 Datacenter'
        )]
        [string]
        $TargetOSVersion,

        [Parameter(ParameterSetName = 'Single')]
        [ValidateSet('VMware', 'Azure')]
        $TargetPlatform,

        [Parameter(ParameterSetName = 'Single')]
        [int]
        $NumCpu,

        [Parameter(ParameterSetName = 'Single')]
        [int]
        $MemoryGB,

        [Parameter(ParameterSetName = 'Single')]
        [int]
        $Disk1GB,

        [Parameter(ParameterSetName = 'Single')]
        [int]
        $Disk2GB,

        [Parameter(ParameterSetName = 'Single')]
        [string]
        $CIDR,

        [Parameter(ParameterSetName = 'Single')]
        [ValidateSet('Bismarck', 'Mandan')]
        [string]
        $Datacenter,

        [Parameter(ParameterSetName = 'Single')]
        [ValidateSet(
            'No Licensing Restrictions',
            'Microsoft SharePoint Server',
            'Microsoft SharePoint Server (Academic)',
            'Microsoft SQL Developer',
            'Microsoft SQL MSDN',
            'Microsoft SQL Standard',
            'Microsoft SQL Standard (Academic)',
            'Microsoft SQL Standard (Vendor Provided)',
            'Microsoft SQL Enterprise',
            'Microsoft SQL Enterprise (Academic)',
            'IBM Websphere',
            'IBM ODM',
            'Oracle Standard Edition',
            'Oracle Standard Edition One',
            'Powerschool'
        )]
        [string]
        $LicensingRestrictions,

        [Parameter(ParameterSetName = 'Single')]
        [ValidateSet('ITD', 'DHS', 'DOT')]
        [string]
        $AgencyPrefix,

        [Parameter(ParameterSetName = 'Single')]
        [ValidateSet('All Day Every Day', 'All Week 500 to 2300', 'Weekdays 700 1800')]
        [string]
        $SupportHours,

        [Parameter(ParameterSetName = 'Single')]
        [ValidateSet(
            'No DR',
            'VMWare: ABR',
            'VMWARE: RPO 0:15',
            'VMWARE: RPO 0:30',
            'VMWARE: RPO 1:00',
            'VMWARE: RPO 2:00',
            'VMWARE: RPO 4:00',
            'VMWARE: RPO 8:00')]
        [string]
        $DRProtection,

        [Parameter(ParameterSetName = 'Single')]
        [ValidateRange(1, 5)]
        [int]
        $StartupPriority
    )
    
    begin {
        
    }
    
    process {
        switch ($PSCmdlet.ParameterSetName) {
            'Csv' {
                $Csv = Import-Csv $ImportCsv
                ForEach ($item in $csv) {
                    $NewITDVMSNowRitmParams = @{
                        RequestType           = $item.RequestType
                        ComputerName          = $item.ComputerName
                        SysadminEmail         = $item.SysadminEmail
                        Environment           = $item.Environment
                        AppName               = $item.AppName
                        Comments              = $item.Comments
                        OperatingSystem       = $item.OperatingSystem
                        TargetOSVersion       = $item.TargetOSVersion
                        TargetPlatform        = $item.TargetPlatform
                        NumCpu                = $item.NumCpu
                        MemoryGB              = $item.MemoryGB
                        Disk1GB               = $item.Disk1GB
                        Disk2GB               = $item.Disk2GB
                        CIDR                  = $item.CIDR
                        Datacenter            = $item.Datacenter
                        LicensingRestrictions = $item.LicensingRestrictions
                        AgencyPrefix          = $item.AgencyPrefix
                        SupportHours          = $item.SupportHours
                        DRProtection          = $item.DRProtection
                        StartupPriority       = $item.StartupPriority
                    }
                    New-ITDVMSNowRitm @NewITDVMSNowRitmParams
                }
            }
            'Single' {
                Write-Verbose -Message ("Start " + $ComputerName)
                # determine lookup fields
                
                switch ($PSBoundParameters.Keys) {
                    CIDR {
                        $cidr_block = Get-ITDServiceNowRecord -Table 'cmdb_ci_ip_network' -Filter "name=$CIDR"
                        If ($null -eq $cidr_block) { Write-Error -Message "CIDR is invalid" -ErrorAction Stop }
                    }
                    AgencyPrefix {
                        switch ($AgencyPrefix) {
                            'DHS' { $AgencyNum = '325.0' }
                            'DOT' { $AgencyNum = '801.0' }
                            'ITD' { $AgencyNum = '112.0' }
                        }
                        $Agency = Get-ITDServiceNowRecord -Table 'cmn_department' -Filter "id=$AgencyNum"
                    }
                    AppName {
                        $application_info = Get-ITDServiceNowRecord -Table cmdb_ci_service -Filter ("name=$AppName")
                    }
                    # send ints as strings
                    NumCpu {
                        [string]$NumCpuStr = $NumCpu.ToString()
                    }
                    MemoryGB {
                        [string]$MemoryGBStr = $MemoryGB.ToString()
                    }
                    Disk1GB {
                        [string]$Disk1GBStr = $Disk1GB.ToString()
                    }
                    Disk2GB{
                        [string]$Disk2GBStr = $Disk2GB.ToString()
                    }
                }

                $team_lead_sysid = (Get-ITDServiceNowUser -Email $RequestedForEmail).manager.value

                $NewITDServiceNowServiceCatalogRequestParams = @{  # review and update all of these too
                    CategoryItemName  = "Application Server";
                    RequestedForEmail = $SysadminEmail;
                    Values            = @{
                        request_type                 = $RequestType;
                        application_name             = $AppName;
                        environment                  = $Environment;
                        require_hosting_quote        = 'No';
                        server_name                  = $ComputerName;
                        add_change_disaster_recovery = 'No';
                        additional_comments          = $Comments;
                        ####
                        vm_work_needed               = 'Yes';
                        host_name                    = $ComputerName;
                        server_type                  = 'Virtual';
                        operating_system             = $OperatingSystem;
                        target_os_version_windows    = $TargetOSVersion;
                        target_platform              = $TargetPlatform;
                        processors                   = $NumCpuStr;
                        memory_gb                    = $MemoryGBStr;
                        disk_1_os                    = $Disk1GBStr;
                        disk_2_swap_disk             = $Disk2GBStr;
                        data_center                  = $Datacenter;
                        licensing_restrictions       = $LicensingRestrictions;
                        support_hours                = $SupportHours;
                        dr_protection                = $DRProtection;
                        startup_priority             = $StartupPriority;

                        # lookups
                        cidr_block                   = $cidr_block.sys_id;
                        agency_name                  = $agency.sys_id;
                        team_lead                    = $team_lead.sys_id;
                        application_info             = $application_info.sys_id;

                    };
                }
                New-ITDServiceNowServiceCatalogRequest @NewITDServiceNowServiceCatalogRequestParams
                
                #$NewITDServiceNowServiceCatalogRequestParams
                Write-Verbose -Message ("End   " + $ComputerName)
            }
        }
    }
    
    end {
        
    }
}