# Site 6.1.x updates

function Execute6.1SiteUpdates 
{
	SiteUpdate_6.1_01 6100
	SiteUpdate_6.1_02 6100
	SiteUpdate_6.1_03 6100
	SiteUpdate_6.1_04 6100
	#SiteUpdate_6.1_05 6100 Skip because of SiteUpdate_6.2_04
	SiteUpdate_6.1_06 6100
	SiteUpdate_6.1_07 6110
	SiteUpdate_6.1_08 6110
	SetTemplateVersion "6.1.0.0" -IgnoreWhenHigherVersion
}

# Add lists: Modalities, ReservationPointSystemRules
# Add content type to 'Modalities' list: Modality
# Add content type to 'ReservationPointSystemRules' list: ReservationPointSystemRule
# Add fields to 'ReservationPointSystemRule' content type: wsp_ucc_Modality, wsp_ucc_PointsPerSession
# Add items to 'Modalities' list
# Hide 'Title' field from 'ReservationPointSystemRules' list
# (re)connect wsp_ucc_Modality lookup field
function SiteUpdate_6.1_01($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}

	# Modalities
	$listTitle = "Modalities"
	$ctName = "Modality"
	$added = AddPnPList "Modalities"
	if ($added)
	{
		AddPnPContentType $ctName "0x01006062c9ad16467a42b0bc0890c68fbf9a" "Add a Modality item to the Modalities list"
		AddPnPContentTypesToList $listTitle @($ctName)	
		AddPnPListItem $listTitle @{"Title" = "Audio"}
		AddPnPListItem $listTitle @{"Title" = "Chat"}
		AddForms $updateSite_RelativeURL $listTitle
	}

	# ReservationPointSystemRules
	$listTitle = "ReservationPointSystemRules"
	$ctName = "ReservationPointSystemRule"
	$added = AddPnPList $listTitle -RemoveDefaultView
	if ($added)
	{
		AddPnPFieldFromXml '<Field ID="{70cf1fc6-8600-4ebd-813a-50d8be47a305}" Type="Lookup" Name="wsp_ucc_Modality" DisplayName="Modality" Required="TRUE" Indexed="TRUE" EnforceUniqueValues="TRUE" List="{$listId}" ShowField="Title" Description="The Modality for which this Rule is relevant" Group="AnywhereNow" />' "Modalities" "wsp_ucc_Modality"
		AddPnPFieldFromXml '<Field ID="{7c49dc19-fb91-4924-ab4b-1c3ce047d120}" Type="Number" Name="wsp_ucc_PointsPerSession" DisplayName="PointsPerSession" Required="TRUE" Description="The number of points a session of this type counts for in the point system" Group="AnywhereNow" />' $null "wsp_ucc_PointsPerSession"
		AddPnPContentType $ctName "0x0100bae73a234dccfa40b70bd77185ed9a74" "Add a ReservationPointSystemRule item to the ReservationPointSystemRules list"
		AddPnPFieldsToContentType $ctName @("!wsp_ucc_Modality", "!wsp_ucc_PointsPerSession")
		AddPnPContentTypesToList $listTitle @($ctName)
		AddPnPView $listTitle "AllItems" "AllItems" 30 '<OrderBy><FieldRef Name="ID" Ascending="True" /></OrderBy>' @("wsp_ucc_Modality", "Edit", "wsp_ucc_PointsPerSession") -Default -RemoveIfExists
		AddForms $updateSite_RelativeURL $listTitle
		HideField "Title" $listTitle
	}

	# (re)connect wsp_ucc_Modality lookup field
	FixLookup $updateSite_Id $updateSite_Title "ReservationPointSystemRules" "Modalities" "wsp_ucc_Modality"
}

# List: OutboundDialerCalls, CT: AutonomousDialerItem
# Add field to content type: wsp_ucc_odc_prewelcomeaudiomsg
# Enable restrict delete relationship behaviour on field
# Copy data from old field: wsp_ucc_odc_prewelcomeaudiomessage
# Remove old field
function SiteUpdate_6.1_02($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}

	$listTitle = "OutboundDialerCalls"
	$ctName = "AutonomousDialerItem"
	$fieldName = "wsp_ucc_odc_prewelcomeaudiomsg"
	AddPnPFieldFromXml '<Field ID="{8b37d841-49b6-4ac5-9815-569cfef3ccc3}" Type="Lookup" Name="wsp_ucc_odc_prewelcomeaudiomsg" DisplayName="Pre-Welcome Audio Message" Group="AnywhereNow" Required="FALSE" EnforceUniqueValues="FALSE" List="{$listId}" ShowField="Title" Mult="FALSE" Sortable="TRUE" />' "AudioFiles" "wsp_ucc_odc_prewelcomeaudiomsg"
	AddPnPFieldsToContentType $ctName @("wsp_ucc_odc_prewelcomeaudiomsg") @("Title", "wsp_ucc_odc_processed", "wsp_ucc_odc_order", "wsp_ucc_odc_prio", "wsp_ucc_odc_prewelcomemessage", "wsp_ucc_odc_prewelcomeaudiomsg", "wsp_ucc_odc_day", "wsp_ucc_odc_datestart", "wsp_ucc_odc_timestart", "wsp_ucc_odc_timeend", "wsp_ucc_odc_dateend", "wsp_ucc_odc_retries")
	AddPnPView "OutboundDialerCalls" "AutonomousDialerView" "AutonomousDialerView" 100 '<Where><Eq><FieldRef Name="ContentType" /><Value Type="Text">AutonomousDialerItem</Value></Eq></Where><OrderBy><FieldRef Name="ID" /></OrderBy>' @("LinkTitle", "wsp_ucc_odc_processed", "wsp_ucc_odc_order", "wsp_ucc_odc_prio", "wsp_ucc_odc_prewelcomemessage", "wsp_ucc_odc_prewelcomeaudiomsg", "wsp_ucc_odc_day", "wsp_ucc_odc_datestart", "wsp_ucc_odc_timestart", "wsp_ucc_odc_dateend", "wsp_ucc_odc_timeend", "wsp_ucc_odc_result", "wsp_ucc_odc_retries") -RemoveIfExists
	$field1 = GetPnPFieldWithListTitle "wsp_ucc_odc_prewelcomeaudiomsg" $listTitle
	EnforceRelationshipBehavior @($null, $field1) "Restrict"

	$listItems = Get-PnPListItem -List $listTitle
	if ($listItems.Count -gt 0) 
	{
		foreach ($item in $listItems)
		{
			$oldAudioMsg = $item["wsp_ucc_odc_prewelcomeaudiomessa"]
			if ($oldAudioMsg -ne $null) 
			{
				SetPnPListItem $listTitle $item @{"wsp_ucc_odc_prewelcomeaudiomsg" = $oldAudioMsg.LookupId}
			}
		}
	}

	RemoveFieldsFromContentType $ctName @("wsp_ucc_odc_prewelcomeaudiomessa") $listTitle
	RemovePnPField "wsp_ucc_odc_prewelcomeaudiomessa" $listTitle
	RemoveFieldsFromContentType $ctName @("wsp_ucc_odc_prewelcomeaudiomessage")
	RemovePnPField "wsp_ucc_odc_prewelcomeaudiomessage" -IgnoreException
}

# List: Endpoints, CT: ModalityEndpoint
# Add field to contenttype: wsp_ucc_AlwaysOn
# Update default and ModalityEndpointView views
function SiteUpdate_6.1_03($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}

	$listTitle = "Endpoints"
	AddPnPFieldFromXml '<Field ID="{aca9e2bd-636a-4fb7-8c79-9261cb55b6a3}" Type="Boolean" Name="wsp_ucc_AlwaysOn" DisplayName="AlwaysOn" Required="FALSE" Group="AnywhereNow" />' -IgnoreDuplicateException
	AddPnPFieldsToContentType "ModalityEndpoint" @("wsp_ucc_AlwaysOn") @("Title", "wsp_ucc_modality", "wsp_ucc_skill", "wsp_ucc_endpoints_ParentQuestion", "wsp_ucc_Priority", "wsp_ucc_endpoints_enablerouting", "wsp_ucc_AlwaysOn", "wsp_ucc_endpoints_type")
	AddPnPView $listTitle "AllItems" "AllItems" 30 '<GroupBy Collapse="FALSE"><FieldRef Name="wsp_ucc_endpoints_type" /></GroupBy><OrderBy><FieldRef Name="ID" Ascending="TRUE"/></OrderBy>' @("LinkTitle", "wsp_ucc_modality", "wsp_ucc_skill", "wsp_ucc_endpoints_ParentQuestion", "wsp_ucc_Priority", "wsp_ucc_endpoints_enablerouting", "wsp_ucc_AlwaysOn") -Default -RemoveIfExists
	AddPnPView $listTitle "ModalityEndpointView" "ModalityEndpointView" 30 '<Where><Eq><FieldRef Name="ContentType" /><Value Type="Text">ModalityEndpoint</Value></Eq></Where><OrderBy><FieldRef Name="ID" /></OrderBy>' @("LinkTitle", "wsp_ucc_modality", "wsp_ucc_skill", "wsp_ucc_endpoints_ParentQuestion", "wsp_ucc_Priority", "wsp_ucc_endpoints_enablerouting", "wsp_ucc_AlwaysOn") -RemoveIfExists
}

# List: Supervisors, CT: Supervisor
# Add field to contenttype: wsp_ucc_Supervisor_phoneuri
# Update default view
function SiteUpdate_6.1_04($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}

	AddPnPFieldFromXml '<Field ID="{45aefa6e-adef-44c4-a0d8-76297bc70697}" Type="Text" Name="wsp_ucc_Supervisor_phoneuri" DisplayName="PhoneUri" Required="FALSE" Group="AnywhereNow" />' -IgnoreDuplicateException
	AddPnPFieldsToContentType "Supervisor" @("wsp_ucc_Supervisor_phoneuri")
	AddPnPView "Supervisors" "AllItems" "AllItems" 30 '<OrderBy><FieldRef Name="ID" /></OrderBy>' @("wsp_ucc_Supervisor", "Edit", "wsp_ucc_Supervisor_phoneuri") -Default -RemoveIfExists
}

# List: SettingValues
# Remove items (if any)
# Import new data
function SiteUpdate_6.1_05($patchVersion)
{
	if ($patchCheck -ge $patchVersion -or $newSite -eq $true) 
	{
		return
	}

	UpdateSettingValues
}

# List: Agents, CT: Agent
# List: IVRQuestions, CT: IVRQuestionItem
# List: SettingValues, CT: SettingValue
# Update contenttype descriptions
function SiteUpdate_6.1_06($patchVersion)
{
	if ($patchCheck -ge $patchVersion -or $newSite -eq $true) 
	{
		return
	}

	$ctName = "Agent"
	$ctDescription = "Add Agent item to the Agents list"
	SetContentTypeDescription $ctName $ctDescription
	SetContentTypeDescription $ctName $ctDescription "Agents"
	$ctName = "IVRQuestionItem"
	$ctDescription = "Add a IVRQuestion item to the IVRQuestions list"
	SetContentTypeDescription $ctName $ctDescription
	SetContentTypeDescription $ctName $ctDescription "IVRQuestions"
	$ctName = "SettingValue"
	$ctDescription = "Add a SettingValue item to the SettingValues list"
	SetContentTypeDescription $ctName $ctDescription
	SetContentTypeDescription $ctName $ctDescription "SettingValues"
}

# List: Agents
# Remove unique values for wsp_ucc_agent_aci field
function SiteUpdate_6.1_07($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}
	
	$field1 = GetPnpField "wsp_ucc_agent_aci"
	$field2 = GetPnpField "wsp_ucc_agent_aci" "Agents"
	SetFieldUniqueValues @($field1, $field2) $false
	SetFieldUniqueValues @($field1, $field2) $false -RemoveIndex
}

# List: Agents, Businesshours, CallSummary
# Hide 'Title' field for older 6.0 templates
function SiteUpdate_6.1_08($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}
	
	HideField "Title" "Agents"
	HideField "Title" "Businesshours"
	HideField "Title" "CallSummary"
}