# Site 8.0.x updates
function Execute8.0SiteUpdates
{
	SiteUpdate_8.0_01 8000
	SiteUpdate_8.0_02 8000
	#SiteUpdate_8.0_03 8010 Skip because of SiteUpdate_8.1_02
	SiteUpdate_8.0_04 8000
	SiteUpdate_8.0_05 8000
	SiteUpdate_8.0_06 8000
	SiteUpdate_8.0_07 8000
	SiteUpdate_8.0_08 8010
	SiteUpdate_8.0_09 8010
	SetTemplateVersion "8.0.1.0" -IgnoreWhenHigherVersion
}
# List: QualityMonitorConfig, CT: QualityMonitorConfig
# Add field to content type: wsp_ucc_qmc_plugin_name
# Update default view
function SiteUpdate_8.0_01($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}
	$listTitle = "QualityMonitorConfig"
	$ctName = "QualityMonitorConfig"
	AddPnPFieldFromXml '<Field ID="{04c6107d-4c62-4417-a3d0-00876e915776}" Type="Text" Name="wsp_ucc_qmc_plugin_name" DisplayName="Plugin Name" Required="false" Description="If provided, this Quality Monitor Question will be handled by the Plugin instead of the built-in Quality Monitor. If the Plugin is not available at run-time, the question will be handled by the built-in Quality Monitor Question instead." Group="AnywhereNow" />' $null "wsp_ucc_qmc_plugin_name" -IgnoreDuplicateException
	AddPnPFieldsToContentType $ctName @("wsp_ucc_qmc_plugin_name")
	AddPnPView $listTitle "AllItems" "AllItems" 30 '<OrderBy><FieldRef Name="ID" /></OrderBy>' @("Welcome", "Edit", "ValueStart", "ValueEnd", "GoodBye", "WelcomeAudio", "GoodByeAudio", "wsp_ucc_qmc_modality", "wsp_ucc_qmc_order", "wsp_ucc_qmc_plugin_name") -Default -RemoveIfExists
}
# List: Notifications, CT: Notification - REMOVED in MC1117115 modernization (list no longer exists)
# Add field to content type: wsp_ucc_not_business_hour
# Update default view
function SiteUpdate_8.0_02($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}
	# Notifications list no longer exists in modern deployments - skip this update
	# $listTitle = "Notifications"
	# $ctName = "Notification"
	# AddPnPFieldFromXml '<Field ID="{eee25052-0c9e-4b4d-a005-817de7a73f0f}" Type="Boolean" Name="wsp_ucc_not_business_hour" DisplayName="WithinBusinessHours" Required="false" Group="AnywhereNow"><Default>1</Default></Field>' $null "wsp_ucc_not_business_hour" -IgnoreDuplicateException
	# AddPnPFieldsToContentType $ctName @("wsp_ucc_not_business_hour")
	# AddPnPView $listTitle "AllItems" "AllItems" 100 '<OrderBy><FieldRef Name="ID" /></OrderBy>' @("LinkTitle", "wsp_ucc_not_kpi", "wsp_ucc_not_skill", "wsp_ucc_not_compare", "wsp_ucc_not_threshold", "wsp_ucc_not_receiver", "wsp_ucc_not_message", "wsp_ucc_not_sendsettingsiteurl", "wsp_ucc_not_notificationmethod", "wsp_ucc_not_additionalinfo", "wsp_ucc_not_business_hour") -Default -RemoveIfExists
}
# List: SettingValues
# Remove items (if any)
# Import new data
function SiteUpdate_8.0_03($patchVersion)
{
	if ($patchCheck -ge $patchVersion -or $newSite) 
	{
		return
	}
	UpdateSettingValues
}
# List: Settings
# Fix 'AzureKeyTextTranslationService' and 'GoogleKeyTextTranslationService' keys when present
function SiteUpdate_8.0_04($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}
	$listTitle = "Settings"
	$updateItem = Get-PnPListItem -List $listTitle -Query '<View><Query><Where><Contains><FieldRef Name="wsp_ucc_Key" /><Value Type="Text">AzureKeyTextTranslationService</Value></Contains></Where></Query></View>'
	if ($updateItem -ne $null) 
	{
		SetPnPListItem $listTitle $updateItem @{"wsp_ucc_Key" = "AzureKeyTextTranslationService"}
	}
	$updateItem = Get-PnPListItem -List $listTitle -Query '<View><Query><Where><Contains><FieldRef Name="wsp_ucc_Key" /><Value Type="Text">GoogleKeyTextTranslationService</Value></Contains></Where></Query></View>'
	if ($updateItem -ne $null) 
	{
		SetPnPListItem $listTitle $updateItem @{"wsp_ucc_Key" = "GoogleKeyTextTranslationService"}
	}
}
# List: BridgAuth, CT: BridgAuth - REMOVED in MC1117115 modernization (list no longer exists)
# Add fields to content type: wsp_ucc_ba_Agent, wsp_ucc_ba_Known
# Update default view
function SiteUpdate_8.0_05($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}
	# BridgAuth list no longer exists in modern deployments - skip this update
	# $listTitle = "BridgAuth"
	# $ctName = "BridgAuth"
	# AddPnPFieldFromXml '<Field ID="{e7a1840c-8ae6-489d-a9ac-9df652474541}" Type="Text" Name="wsp_ucc_ba_Agent" DisplayName="Agent" Required="false" Group="AnywhereNow" />' $null "wsp_ucc_ba_Agent" -IgnoreDuplicateException
	# AddPnPFieldFromXml '<Field ID="{1efd97e8-c64c-4bff-a85e-e329d1defbe1}" Type="Boolean" Name="wsp_ucc_ba_Known" DisplayName="Known" Required="false" Group="AnywhereNow"><Default>0</Default></Field>' $null "wsp_ucc_ba_Known" -IgnoreDuplicateException
	# AddPnPFieldsToContentType $ctName @("wsp_ucc_ba_Agent", "wsp_ucc_ba_Known")
	# AddPnPView $listTitle "AllItems" "AllItems" 30 '<OrderBy><FieldRef Name="ID" Ascending="True" /></OrderBy>' @("LinkTitle", "wsp_ucc_ba_BridgPhoneNumberId", "wsp_ucc_ba_Agent", "wsp_ucc_ba_Known") -Default -RemoveIfExists
}
# List: Endpoints
# Unique values for title field
function SiteUpdate_8.0_06($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}
	$field1 = GetPnpField "Title" "Endpoints"
	SetFieldUniqueValues @($field1)
}
# List: Settings
# Update 'DisableCallForwarding' description
function SiteUpdate_8.0_07($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}
	$listTitle = "Settings"
	$updateItem = Get-PnPListItem -List $listTitle -Query '<View><Query><Where><Eq><FieldRef Name="wsp_ucc_Key" /><Value Type="Text">DisableCallForwarding</Value></Eq></Where></Query></View>'
	if ($updateItem -ne $null) 
	{
		$descriptionItem = Get-PnPListItem -List "SettingValues" -Query '<View><Query><Where><Eq><FieldRef Name="Title" /><Value Type="Text">DisableCallForwarding</Value></Eq></Where></Query></View>'
		if ($descriptionItem -ne $null) 
		{
			SetPnPListItem $listTitle $updateItem @{"wsp_ucc_Description" = $descriptionItem["wsp_ucc_sv_description"]}
		}
	}
}
# List: Settings
# Fix 'ServiceLevelFormula' setting value when present
function SiteUpdate_8.0_08($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}
	$listTitle = "Settings"
	$updateItem = Get-PnPListItem -List $listTitle -Query '<View><Query><Where><Contains><FieldRef Name="wsp_ucc_Key" /><Value Type="Text">ServiceLevelFormula</Value></Contains></Where></Query></View>'
	if ($updateItem -ne $null) 
	{
		$checkValue = $updateItem["wsp_ucc_Value"]
		if ($checkValue -eq "AcceptedSla1 / (Accepted + (Missed - QuickDropped)")
		{
			SetPnPListItem $listTitle $updateItem @{"wsp_ucc_Value" = "AcceptedSla1 / (Accepted + (Missed - QuickDropped))"}
		}
	}
}
# Lists: Settings, SettingValues, Fields: wsp_ucc_scope, wsp_ucc_sv_scope
# Update choices
function SiteUpdate_8.0_09($patchVersion)
{
	if ($patchCheck -ge $patchVersion) 
	{
		return
	}
	$updateChoices = @("Bot", "CDR", "Chat", "Config", "CRM", "Dialer", "Email", "Flow", "ML Routing", "OutboundDialer", "Playlist", "Prompt", "Quality Monitor", "Recording", "Reservation", "Scheduling", "Speech", "Transcription", "SLA", "Survey", "User", "Voicemail")
	$field1 = GetPnPField "wsp_ucc_scope"
	$field2 = GetPnPField "wsp_ucc_scope" "Settings"
	$field3 = GetPnPField "wsp_ucc_sv_scope"
	$field4 = GetPnPField "wsp_ucc_sv_scope" "SettingValues"
	AddChoiceFieldItems @($field1, $field2, $field3, $field4) $updateChoices
}
