﻿ecumenical_council_gather_clergy_effect = {
	scope:activity = {
		every_attending_character = {
			limit = {
				is_clergy = yes
			}
			save_temporary_scope_as = temp_character
			scope:activity = {
				add_to_guest_subset = {
					name = clergy
					target = scope:temp_character
				}
			}
		}
	}
}

ecumenical_council_gather_non_clergy_effect = {
	scope:activity = {
		every_attending_character = {
			limit = {
				is_clergy = no
			}
			save_temporary_scope_as = temp_character
			scope:activity = {
				add_to_guest_subset = {
					name = non_clergy
					target = scope:temp_character
				}
			}
		}
	}
}

# For MP
ecumenical_council_busy_effect = {
	if = {
		limit = {
			is_ai = no
			has_multiple_players = yes
		}
		involved_activity ?= {
			add_to_variable_list = {
				name = ecumenical_council_list_busy
				target = root
			}
		}
	}
}

ecumenical_council_ready_effect = {
	if = {
		limit = {
			is_ai = no
			has_multiple_players = yes
		}
		involved_activity ?= {
			remove_list_variable = {
				name = ecumenical_council_list_busy
				target = root
			}
		}
	}
}

# scope:host on_start runs this, and we name the actor or the Church panel hides the catalyst
# Unlanded clerical puppets are not situation participants
pam_ecumenical_council_host_start_catalyst_effect = {
	if = {
		limit = {
			OR = {
				any_character_situation = {
					situation_type = the_christian_church
					situation_top_has_catalyst = catalyst_hosted_ecumenical_council
				}
				puppeteer ?= {
					any_character_situation = {
						situation_type = the_christian_church
						situation_top_has_catalyst = catalyst_hosted_ecumenical_council
					}
				}
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = {
				catalyst = catalyst_hosted_ecumenical_council
				character = scope:host
			}
		}
	}
	if = {
		limit = {
			NOR = {
				any_held_title = { is_head_of_faith = yes }
				government_has_flag = government_is_ecclesiastical
			}
			OR = {
				any_character_situation = {
					situation_type = the_christian_church
					situation_top_has_catalyst = catalyst_secular_hosted_ecumenical_council
				}
				puppeteer ?= {
					any_character_situation = {
						situation_type = the_christian_church
						situation_top_has_catalyst = catalyst_secular_hosted_ecumenical_council
					}
				}
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = {
				catalyst = catalyst_secular_hosted_ecumenical_council
				character = scope:host
			}
		}
	}
}

# Conclusion payout only. Hosting catalysts fire from on_start via pam_ecumenical_council_host_start_catalyst_effect, not here, so they land when planning is confirmed.
disburse_ecumenical_council_activity_rewards = {
	# for the loc
	save_scope_as = root_scope
	# We give you the regular rewards
	add_piety = medium_piety_gain
	# And the scholar trait
	add_scholar_trait_or_xp_effect = { VALUE = 10 }
	if = {
		limit = { this = scope:host }
		pam_vows_of_poverty_host_completion_piety_effect = yes
	}
}

ecumenical_council_become_innovator_effect = {
	scope:activity.var:current_topic ?= { save_scope_as = ec_current_topic }
	$CHARACTER$ = {
		save_scope_as = ec_switch_stance
		if = {
			limit = {
				scope:activity = {
					is_target_in_variable_list = {
						name = major_conservative
						target = $CHARACTER$
					}
				}
			}
			scope:activity = {
				remove_list_variable = {
					name = major_conservative
					target = $CHARACTER$
				}
			}
		}
		if = {
			limit = {
				is_in_guest_subset = {
					name = conservative
				}
			}
			custom_tooltip = ec_conservative_becomes_innovator_tt
		}
		else = {
			custom_tooltip = ec_becomes_innovator_tt
		}
		#Are you a conservative? Stop that
		if = {
			limit = {
				is_in_guest_subset = {
					name = conservative
				}
			}
			scope:activity = {
				remove_from_guest_subset = {
					name = conservative
					target = $CHARACTER$
				}
				add_to_variable_list = {
					name = blocked_from_conservatives
					target = $CHARACTER$
				}
				if = {
					limit = {
						has_variable_list = locked_into_conservatives
					}
					remove_list_variable = {
						name = locked_into_conservatives
						target = $CHARACTER$
					}
				}
			}

		}
		#Not a innovator? Make them a innovator
		if = {
			limit = {
				NOT = {
					is_in_guest_subset = {
						name = innovator
					}
				}
			}
			scope:activity = {
				add_to_guest_subset = {
					name = innovator
					target = $CHARACTER$
				}
				add_to_variable_list = {
					name = locked_into_innovators
					target = $CHARACTER$
				}
				if = {
					limit = {
						has_variable_list = blocked_from_innovators
					}
					remove_list_variable = {
						name = blocked_from_innovators
						target = $CHARACTER$
					}
				}
			}
		}
		if = {
			limit = {
				NOT = {
					scope:activity = {
						any_in_list = {
							variable = major_innovator
							count = all
							ecumenical_council_innovator_sort_value > $CHARACTER$.ecumenical_council_innovator_sort_value
						}
					}
				}
			}
			scope:activity = {
				ordered_in_list = {
					variable = major_innovator
					order_by = {
						value = ecumenical_council_innovator_sort_value
						multiply = -1
					}
					scope:activity = {
						remove_list_variable = {
							name = major_innovator
							target = prev
						}
					}
				}
				add_to_variable_list = {
					name = major_innovator
					target = $CHARACTER$
				}
			}
		}
	}
}

ecumenical_council_become_conservative_effect = {
	scope:activity.var:current_topic ?= { save_scope_as = ec_current_topic }
	$CHARACTER$ = {
		save_scope_as = ec_switch_stance
		if = {
			limit = {
				scope:activity = {
					is_target_in_variable_list = {
						name = major_innovator
						target = $CHARACTER$
					}
				}
			}
			scope:activity = {
				remove_list_variable = {
					name = major_innovator
					target = $CHARACTER$
				}
			}
		}
		if = {
			limit = {
				is_in_guest_subset = {
					name = innovator
				}
			}
			custom_tooltip = ec_innovator_becomes_conservative_tt
		}
		else = {
			custom_tooltip = ec_becomes_conservative_tt
		}
		if = {
			limit = {
				is_in_guest_subset = {
					name = innovator
				}
			}
			scope:activity = {
				remove_from_guest_subset = {
					name = innovator
					target = $CHARACTER$
				}
				add_to_variable_list = {
					name = blocked_from_innovators
					target = $CHARACTER$
				}
				if = {
					limit = {
						has_variable_list = locked_into_innovators
					}
					remove_list_variable = {
						name = locked_into_innovators
						target = $CHARACTER$
					}
				}
			}
		}
		#Not a innovator? Make them a innovator
		if = {
			limit = {
				NOT = {
					is_in_guest_subset = {
						name = conservative
					}
				}
			}
			scope:activity = {
				add_to_guest_subset = {
					name = conservative
					target = $CHARACTER$
				}
				add_to_variable_list = {
					name = locked_into_conservatives
					target = $CHARACTER$
				}
				if = {
					limit = {
						has_variable_list = blocked_from_conservatives
					}
					remove_list_variable = {
						name = blocked_from_conservatives
						target = $CHARACTER$
					}
				}
			}
		}
		if = {
			limit = {
				NOT = {
					scope:activity = {
						any_in_list = {
							variable = major_conservative
							count = all
							ecumenical_council_conservative_sort_value > $CHARACTER$.ecumenical_council_conservative_sort_value
						}
					}
				}
			}
			scope:activity = {
				ordered_in_list = {
					variable = major_conservative
					order_by = {
						value = ecumenical_council_conservative_sort_value
						multiply = -1
					}
					scope:activity = {
						remove_list_variable = {
							name = major_conservative
							target = prev
						}
					}
				}
				add_to_variable_list = {
					name = major_conservative
					target = $CHARACTER$
				}
			}
		}
	}
}

# PROPOSAL = tenet status flag (first/second debate) or doctrine scope (third debate)
ecumenical_council_set_proposal_effect = {
	scope:activity = {
		add_activity_log_entry = {
			key = ecumenical_council_proposal_set_log
			score = 0
			tags = { completed }
			character = scope:host
			location = scope:host.location
			if = {
				limit = {
					has_current_phase = ecumenical_council_phase_first_debate
				}
				set_variable = {
					name = current_debate_proposal
					value = $PROPOSAL$
				}
				set_variable = {
					name = first_debate_proposal
					value = $PROPOSAL$
				}
			}
			else_if = {
				limit = {
					has_current_phase = ecumenical_council_phase_second_debate
				}
				set_variable = {
					name = current_debate_proposal
					value = $PROPOSAL$
				}
				set_variable = {
					name = second_debate_proposal
					value = $PROPOSAL$
				}
			}
			else = {
				# Keep current_debate_proposal a flag: tenet-status compares are type-unsafe vs a doctrine
				set_variable = {
					name = current_debate_proposal
					value = flag:doctrine_change
				}
				set_variable = {
					name = third_debate_proposal
					value = $PROPOSAL$
				}
			}
			# Preview the proposed change (tenet status or doctrine) in the entry tooltip without applying it.
			show_as_tooltip = {
				ecumenical_council_apply_debate_outcome_effect = yes
			}
		}
	}
}

ecumenical_council_conservative_backlash_effect = {
	scope:activity.var:current_topic ?= {
		save_scope_as = tenet
	}
	if = {
		limit = {
			ecumenical_council_proposal_change_magnitude_value > 0
		}
		hidden_effect = {
			scope:activity = {
				every_guest_subset = {
					name = conservative
					save_temporary_scope_as = ec_backlash_conservative
					add_opinion = {
						target = scope:host
						modifier = ecumenical_council_forced_change_opinion
						opinion = ecumenical_council_forced_change_opinion_penalty_value
					}
					scope:activity = {
						every_guest_subset = {
							name = innovator
							limit = {
								this != scope:host
								this != scope:ec_backlash_conservative
							}
							save_temporary_scope_as = ec_backlash_innovator
							scope:ec_backlash_conservative = {
								add_opinion = {
									target = scope:ec_backlash_innovator
									modifier = ecumenical_council_forced_change_opinion
									opinion = ecumenical_council_forced_change_opinion_penalty_value
								}
							}
						}
					}
				}
			}
		}
		if = {
			limit = {
				scope:activity = {
					NOT = {
						has_variable = ec_backlash_tt_shown
					}
				}
			}
			scope:activity = {
				set_variable = ec_backlash_tt_shown
			}
			custom_tooltip = ecumenical_council_conservative_backlash_tt
		}
	}
}

ecumenical_council_make_core_preview_effect = {
	$PROMOTED$ = {
		save_temporary_scope_as = ec_promoted_tenet
	}
	if = {
		limit = {
			faith = {
				pam_faith_is_at_core_tenet_cap_trigger = yes
			}
		}
		$DEMOTED$ = {
			save_temporary_scope_as = ec_demoted_tenet
		}
		custom_tooltip = ecumenical_council_outcome_preview_make_core_tt
	}
	else = {
		custom_tooltip = ecumenical_council_outcome_preview_adopt_core_tt
	}
}

# Applies the outcome of a passed debate vote to the host's faith.
# scope:activity.var:current_topic holds the tenet/doctrine being debated.
# scope:activity.var:current_debate_proposal holds the proposed change.
ecumenical_council_apply_debate_outcome_effect = {
	# Must stay above set_tenet_status so pre-change standing is readable
	# Tenet demotion and make_core swap are if/else_if so make_core cannot book twice
	if = {
		limit = {
			pam_great_schism_progress_can_change_trigger = yes
			scope:host.faith = faith:christian_faith
		}
		if = {
			# First/Second debate: tenet demotion of a schism-rite tenet counts; promotion books nothing
			limit = {
				# A doctrine ruling's tooltip preview still sees the previous debate's tenet flag
				NOT = { exists = scope:ec_proposal_doctrine }
				OR = {
					scope:activity.var:current_debate_proposal ?= flag:permit
					scope:activity.var:current_debate_proposal ?= flag:prohibit
					scope:activity.var:current_debate_proposal ?= flag:learn
					scope:activity.var:current_debate_proposal ?= flag:make_core
				}
				# Rulings that are dropped below must not book progress either
				exists = scope:activity.var:current_topic
				NOT = {
					scope:host.faith = {
						pam_faith_tenet_is_core_trigger = { TENET = scope:activity.var:current_topic }
					}
				}
			}
			if = {
				limit = {
					OR = {
						rite:roman_rite = { any_rite_tenet = { this = scope:activity.var:current_topic } }
						rite:byzantine_rite = { any_rite_tenet = { this = scope:activity.var:current_topic } }
					}
				}
				scope:activity.var:current_topic = {
					save_temporary_scope_as = tenet # Read by the acceptance script values
				}
			}
			if = {
				limit = {
					exists = scope:tenet
					ecumenical_council_tenet_current_acceptance_value > ecumenical_council_proposal_target_acceptance_value
				}
				pam_change_great_schism_progress_effect = {
					VALUE = pam_great_schism_progress_council_tenet_value
					TT = ecumenical_council_schism_progress_gain_tt
				}
			}
			# make_core already demotes the core you swapped out, so below the maximum there is nothing to record
			else_if = {
				limit = {
					scope:activity.var:current_debate_proposal ?= flag:make_core
					scope:host.faith = {
						pam_faith_is_at_core_tenet_cap_trigger = yes
					}
				}
				if = {
					limit = { exists = scope:activity.var:core_tenet_to_swap_2 }
					scope:activity.var:core_tenet_to_swap_2 = {
						save_temporary_scope_as = pam_gs_swapped_tenet
					}
				}
				else = {
					scope:activity.var:core_tenet_to_swap_1 ?= {
						save_temporary_scope_as = pam_gs_swapped_tenet
					}
				}
				if = {
					limit = {
						exists = scope:pam_gs_swapped_tenet
						OR = {
							rite:roman_rite = { any_rite_tenet = { this = scope:pam_gs_swapped_tenet } }
							rite:byzantine_rite = { any_rite_tenet = { this = scope:pam_gs_swapped_tenet } }
						}
					}
					pam_change_great_schism_progress_effect = {
						VALUE = pam_great_schism_progress_council_tenet_value
						TT = ecumenical_council_schism_progress_gain_tt
					}
				}
			}
		}
		else_if = {
			# Third debate: dropping a schism-rite doctrine counts; adopting one does not
			limit = {
				scope:activity.var:current_debate_proposal ?= flag:doctrine_change
				exists = scope:activity.var:third_debate_proposal
			}
			if = {
				limit = {
					OR = {
						rite:roman_rite = { rite_has_doctrine = scope:activity.var:current_topic }
						rite:byzantine_rite = { rite_has_doctrine = scope:activity.var:current_topic }
					}
				}
				pam_change_great_schism_progress_effect = {
					VALUE = pam_great_schism_progress_doctrine_value
					TT = ecumenical_council_schism_progress_gain_tt
				}
			}
		}
	}
	if = {
		# First/Second debate: the proposal is a tenet status change.
		limit = {
			NOT = { exists = scope:ec_proposal_doctrine }
			OR = {
				scope:activity.var:current_debate_proposal ?= flag:permit
				scope:activity.var:current_debate_proposal ?= flag:prohibit
				scope:activity.var:current_debate_proposal ?= flag:learn
				scope:activity.var:current_debate_proposal ?= flag:make_core
			}
		}
		if = {
			limit = {
				exists = scope:activity.var:core_tenet_to_swap_2
			}
			scope:activity.var:core_tenet_to_swap_2 = {
				save_scope_as = tenet_to_swap
			}
		}
		else = {
			scope:activity.var:core_tenet_to_swap_1 ?= {
				save_scope_as = tenet_to_swap
			}
		}
		ecumenical_council_conservative_backlash_effect = yes
		scope:host.faith = {
			# Applying any ruling to an already-core tenet would empty a core slot for good
			# Drop a missing topic: make_core would still demote and then have nothing to promote
			if = {
				limit = {
					exists = scope:activity.var:current_topic
					NOT = {
						pam_faith_tenet_is_core_trigger = { TENET = scope:activity.var:current_topic }
					}
				}
				switch = {
					trigger = scope:activity.var:current_debate_proposal
					flag:permit = {
						set_tenet_status = { tenet = scope:activity.var:current_topic status = permitted }
						if = {
							limit = { has_tenet_popularity_trigger = yes }
							change_tenet_popularity = { target = scope:activity.var:current_topic change = medium_tenet_popularity_gain }
						}
					}
					flag:prohibit = {
						set_tenet_status = { tenet = scope:activity.var:current_topic status = prohibited }
						if = {
							limit = { has_tenet_popularity_trigger = yes }
							change_tenet_popularity = { target = scope:activity.var:current_topic change = major_tenet_popularity_loss }
						}
						pam_faith_grant_forbidden_personal_tenet_secrets_effect = { TENET = scope:activity.var:current_topic }
					}
					flag:learn = {
						set_tenet_status = { tenet = scope:activity.var:current_topic status = known }
						if = {
							limit = { has_tenet_popularity_trigger = yes }
							change_tenet_popularity = { target = scope:activity.var:current_topic change = minor_tenet_popularity_gain }
						}
					}
					flag:make_core = {
						# We only give up a core tenet at the maximum, because demoting anyway would strip a missing slot
						if = {
							limit = {
								exists = scope:tenet_to_swap
								pam_faith_is_at_core_tenet_cap_trigger = yes
							}
							scope:tenet_to_swap = {
								save_temporary_scope_as = ec_demoted_tenet
							}
							custom_tooltip = {
								text = ecumenical_council_demotes_tenet_tt
								set_tenet_status = { tenet = scope:tenet_to_swap status = permitted }
							}
							if = {
								limit = { has_tenet_popularity_trigger = yes }
								change_tenet_popularity = { target = scope:tenet_to_swap change = medium_tenet_popularity_loss }
							}
						}
						set_tenet_status = { tenet = scope:activity.var:current_topic status = core }
						if = {
							limit = { has_tenet_popularity_trigger = yes }
							change_tenet_popularity = { target = scope:activity.var:current_topic change = major_tenet_popularity_gain }
						}
					}
				}
			}
			pam_faith_cleanup_forbidden_personal_tenet_secrets_effect = yes
		}
		scope:activity = {
			if = {
				limit = {
					NOT = { exists = var:first_debate_resolution }
				}
				set_variable = {
					name = first_debate_resolution
					value = flag:passed
				}
			}
			else = {
				set_variable = {
					name = second_debate_resolution
					value = flag:passed
				}
			}
		}
	}
	else_if = {
		# Third debate: proposal is the doctrine sentinel (see set_proposal effect).
		# Swap the doctrine the rite currently holds for the proposed one.
		limit = {
			OR = {
				exists = scope:ec_proposal_doctrine
				scope:activity.var:current_debate_proposal ?= flag:doctrine_change
			}
		}
		if = {
			limit = { NOT = { exists = scope:ec_proposal_doctrine } }
			scope:activity.var:third_debate_proposal ?= {
				save_scope_as = ec_proposal_doctrine
			}
		}
		if = {
			limit = {
				exists = scope:ec_proposal_doctrine
			}
			scope:activity = {
				save_temporary_scope_as = ec_proposal_activity
			}
			if = {
				limit = {
					OR = {
						scope:ec_proposal_doctrine = doctrine:doctrine_liturgical_language_none
						scope:ec_proposal_doctrine = doctrine:doctrine_liturgical_language_latin
						scope:ec_proposal_doctrine = doctrine:doctrine_liturgical_language_koine
						scope:ec_proposal_doctrine = doctrine:doctrine_liturgical_language_church_slavonic
						scope:ec_proposal_doctrine = doctrine:doctrine_liturgical_language_irish
						scope:ec_proposal_doctrine = doctrine:doctrine_liturgical_language_armenian
						scope:ec_proposal_doctrine = doctrine:doctrine_liturgical_language_classical_georgian
						scope:ec_proposal_doctrine = doctrine:doctrine_liturgical_language_church_romanian
						scope:ec_proposal_doctrine = doctrine:doctrine_liturgical_language_arabic
						scope:ec_proposal_doctrine = doctrine:doctrine_liturgical_language_hebrew
						scope:ec_proposal_doctrine = doctrine:doctrine_liturgical_language_esperanto
					}
				}
				scope:host.rite = {
					custom_tooltip = {
						text = neow_ecumenical_council_adopts_liturgical_language_tt
						hidden_effect = {
							change_rite_doctrine = scope:ec_proposal_doctrine
						}
					}
				}
			}
			else = {
				scope:host.faith = {
					# Hide change_doctrine: its history text resolves FAITH from a scope gone on re-render
					custom_tooltip = {
						text = ecumenical_council_adopts_doctrine_tt
						hidden_effect = {
							change_doctrine = scope:ec_proposal_doctrine
							# Conditional is crisis-wide, not per-rite
							if = {
								limit = {
									scope:ec_proposal_doctrine ?= doctrine:doctrine_clerical_succession_conditional
								}
								every_faith_rite = {
									limit = { NOT = { rite_has_doctrine = doctrine_clerical_succession_conditional } }
									change_rite_doctrine = doctrine_clerical_succession_conditional
								}
							}
						}
					}
				}
			}
			# Orthodox synodal decrees bind all rites in communion with the hosting faith.
			# The ordinary Ecumenical Council retains its original scope elsewhere.
			if = {
				limit = {
					scope:host = { religion = religion:eastern_orthodox_religion }
					exists = scope:ec_proposal_doctrine
				}
				neow_pan_orthodox_synod_promulgate_doctrine_effect = {
					DOCTRINE = scope:ec_proposal_doctrine
				}
			}
		}
		scope:activity = {
			set_variable = {
				name = third_debate_resolution
				value = flag:passed
			}
		}
	}
	# CE3 achievement: Home See Advantage — the host archbishop changed the faith at a council in their own cathedral see
	if = {
		limit = {
			scope:host = {
				is_ai = no
				any_held_title = {
					has_clerical_region = yes
					title_capital_county ?= scope:activity.activity_location.county
				}
			}
		}
		scope:host = {
			add_achievement_global_variable_effect = {
				VARIABLE = ce3_14_home_see_advantage_achievement_unlocked
				VALUE = yes
			}
		}
	}
}

ecumenical_council_personal_tenet_fulfillment_effect = {
	if = {
		limit = { $PROPOSAL$ ?= flag:make_core }
		pam_personal_tenet_made_core_fulfillment_effect = {
			TENET = $TOPIC$
			RITE = $RITE$
		}
		if = {
			limit = { exists = $SWAPPED$ }
			pam_personal_tenet_lost_core_fulfillment_effect = {
				TENET = $SWAPPED$
				RITE = $RITE$
			}
		}
	}
	else_if = {
		limit = {
			OR = {
				$PROPOSAL$ ?= flag:permit
				$PROPOSAL$ ?= flag:prohibit
			}
		}
		pam_personal_tenet_status_change_fulfillment_effect = { TENET = $TOPIC$ }
	}
}

ecumenical_council_current_debate_personal_tenet_fulfillment_effect = {
	if = {
		limit = {
			scope:activity.var:last_debate_passed ?= flag:passed
			exists = scope:activity.var:current_topic
			exists = scope:activity.var:ec_throttled_faith
		}
		if = {
			limit = { exists = scope:activity.var:second_debate_resolution }
			ecumenical_council_personal_tenet_fulfillment_effect = {
				TOPIC = scope:activity.var:current_topic
				PROPOSAL = scope:activity.var:current_debate_proposal
				SWAPPED = scope:activity.var:core_tenet_to_swap_2
				RITE = scope:activity.var:ec_throttled_faith.main_rite
			}
		}
		else = {
			ecumenical_council_personal_tenet_fulfillment_effect = {
				TOPIC = scope:activity.var:current_topic
				PROPOSAL = scope:activity.var:current_debate_proposal
				SWAPPED = scope:activity.var:core_tenet_to_swap_1
				RITE = scope:activity.var:ec_throttled_faith.main_rite
			}
		}
	}
}

# The proposal line for an attendee on SIDE (support or oppose) in the current debate
ecumenical_council_sway_stance_tt_effect = {
	if = {
		limit = {
			scope:activity = { has_current_phase = ecumenical_council_phase_first_debate }
		}
		custom_tooltip = ec_sway_$SIDE$_tt
	}
	else_if = {
		limit = {
			scope:activity = { has_current_phase = ecumenical_council_phase_second_debate }
		}
		custom_tooltip = ec_sway_$SIDE$_tt_2
	}
	else = {
		custom_tooltip = ec_sway_$SIDE$_tt_3
	}
}

# Logs a player attendee taking SIDE (innovator or conservative) in a debate
ecumenical_council_log_stance_effect = {
	scope:activity = {
		add_activity_log_entry = {
			key = ecumenical_council_lineup_log
			score = 0
			character = root
			location = root.location
			ecumenical_council_become_$SIDE$_effect = { CHARACTER = root }
		}
	}
}

# LIST = attendees to sway, and CUSTOM = the loc key for the group line that has the count
ecumenical_council_sway_to_innovators_effect = {
	scope:activity = {
		# Skip the entry when nobody is left to sway
		if = {
			limit = { any_in_list = { list = $LIST$ } }
			# Other players see the sway when it happens, not only in the log
			hidden_effect = {
				every_attending_character = {
					limit = {
						is_ai = no
						this != scope:actor
					}
					send_interface_toast = {
						type = event_toast_effect_neutral
						title = ecumenical_council_guests_swayed_to_innovators_log_title
						left_icon = scope:actor
						show_as_tooltip = {
							ordered_in_list = {
								list = $LIST$
								custom = $CUSTOM$
								max = $MAX$
								order_by = "opinion(scope:actor)"
								ecumenical_council_sway_stance_tt_effect = { SIDE = support }
							}
						}
					}
				}
			}
			add_activity_log_entry = {
				key = ecumenical_council_guests_swayed_to_innovators_log
				score = 0
				character = scope:actor
				location = scope:actor.location
				ordered_in_list = {
					list = $LIST$
					custom = $CUSTOM$
					max = $MAX$
					order_by = "opinion(scope:actor)"
					ecumenical_council_sway_stance_tt_effect = { SIDE = support }
					hidden_effect = {
						save_temporary_scope_as = change
						ecumenical_council_become_innovator_effect = { CHARACTER = scope:change }
					}
				}
			}
		}
		else = {
			custom_tooltip = ec_sway_nobody_tt
		}
	}
}

# Sways a list of attendees at an Ecumenical Council to oppose the host's proposed change (become Conservatives).
ecumenical_council_sway_to_conservatives_effect = {
	scope:activity = {
		# Skip the entry when nobody is left to sway
		if = {
			limit = { any_in_list = { list = $LIST$ } }
			# Other players see the sway when it happens, not only in the log
			hidden_effect = {
				every_attending_character = {
					limit = {
						is_ai = no
						this != scope:actor
					}
					send_interface_toast = {
						type = event_toast_effect_neutral
						title = ecumenical_council_guests_swayed_to_conservatives_log_title
						left_icon = scope:actor
						show_as_tooltip = {
							ordered_in_list = {
								list = $LIST$
								custom = $CUSTOM$
								max = $MAX$
								order_by = "opinion(scope:actor)"
								ecumenical_council_sway_stance_tt_effect = { SIDE = oppose }
							}
						}
					}
				}
			}
			add_activity_log_entry = {
				key = ecumenical_council_guests_swayed_to_conservatives_log
				score = 0
				character = scope:actor
				location = scope:actor.location
				ordered_in_list = {
					list = $LIST$
					custom = $CUSTOM$
					max = $MAX$
					order_by = "opinion(scope:actor)"
					ecumenical_council_sway_stance_tt_effect = { SIDE = oppose }
					hidden_effect = {
						save_temporary_scope_as = change
						ecumenical_council_become_conservative_effect = { CHARACTER = scope:change }
					}
				}
			}
		}
		else = {
			custom_tooltip = ec_sway_nobody_tt
		}
	}
}

# Build innovator/conservative subsets based on each attendee's stance toward the planner-selected debate tenet
pam_sort_guests_into_ecumenical_council_groups_effect = {
	scope:activity = {
		# Clear all lists
		clear_variable_list = major_innovator
		clear_variable_list = major_conservative
		clear_variable_list = locked_into_innovators
		clear_variable_list = locked_into_conservatives
		# Explain the backlash again for each debate that passes
		remove_variable = ec_backlash_tt_shown
		every_guest_subset = {
		 	name = innovator
			scope:activity = {
			 	remove_from_guest_subset = {
			 		name = innovator
			 		target = prev
			 	}
			}
		}
		every_guest_subset = {
		 	name = conservative
			scope:activity = {
			 	remove_from_guest_subset = {
			 		name = conservative
			 		target = prev
			 	}
		 	}
		}
		# The host always supports their own proposal, regardless of player or AI control
		scope:host = {
			add_to_list = innovator
			scope:activity = {
				add_to_guest_subset = {
					name = innovator
					target = prev
				}
			}
		}
		# Build a list of AI guest innovators
		every_attending_character = {
			limit = {
				# Players will decide in the follow up event
				is_ai = yes
				this != scope:host
				# Only eligible voters
				pam_valid_ecumenical_council_voter_trigger = yes
				NOT = {	has_relation_rival = scope:host }
				trigger_if = {
					limit = {
						scope:activity = { has_current_phase = ecumenical_council_phase_first_debate }
					}
					scope:activity.activity_tenet:1 = {
						save_temporary_scope_as = tenet
					}
					OR = {
						scope:host = { has_strong_hook = prev }
						ecumenical_council_attendee_support_value > 0
					}
				}
				trigger_else_if = {
					limit = {
						scope:activity = { has_current_phase = ecumenical_council_phase_second_debate }
					}
					scope:activity.activity_tenet:2 = {
						save_temporary_scope_as = tenet
					}
					OR = {
						scope:host = { has_strong_hook = prev }
						ecumenical_council_attendee_support_value > 0
					}
				}
				trigger_else = {
					scope:activity.var:third_debate_proposal ?= {
						save_temporary_scope_as = doctrine
					}
					OR = {
						scope:host = { has_strong_hook = prev }
						ecumenical_council_doctrine_attendee_support_value > 0
					}
				}
			}
			add_to_list = innovator
			scope:activity = {
				add_to_guest_subset = {
					name = innovator
					target = prev
				}
			}
		}
		# Lump everyone else into the conservative group
		every_attending_character = {
			limit = {
				# Players will decide in the follow up event
				is_ai = yes
				# Only eligible voters
				this != scope:host
				pam_valid_ecumenical_council_voter_trigger = yes
				NOT = { is_in_list = innovator }
			}
			add_to_list = conservative
			scope:activity = {
				add_to_guest_subset = {
					name = conservative
					target = prev
				}
			}
		}
		if = {
			limit = {
				any_in_list = {
					list = innovator
					count >= 3
				}
			}
			ordered_in_list = {
				list = innovator
				max = 3
				order_by = ecumenical_council_innovator_sort_value
				scope:activity = {
					add_to_variable_list = {
						name = major_innovator
						target = prev
					}
				}
			}
		}
		else = {
			if = {
				limit = { list_size:conservative > list_size:innovator }
				# populate the list otherwise we have no random event at all.
				ordered_in_list = {
					list = conservative
					limit = { is_ai = yes }
					max = { # a bit more to prevent all flipped
						value = list_size:conservative
						multiply = 0.1
						max = 10
						min = 1
					}
					order_by = {
						if = {
							limit = {
								scope:activity = { has_current_phase = ecumenical_council_phase_first_debate }
							}
							scope:activity.activity_tenet:1 = {
								save_temporary_scope_as = tenet
							}
							add = ecumenical_council_attendee_support_value
						}
						else_if = {
							limit = {
								scope:activity = { has_current_phase = ecumenical_council_phase_second_debate }
							}
							scope:activity.activity_tenet:2 = {
								save_temporary_scope_as = tenet
							}
							add = ecumenical_council_attendee_support_value
						}
						else = {
							if = {
								limit = { exists = scope:activity.var:third_debate_proposal }
								scope:activity.var:third_debate_proposal = {
									save_temporary_scope_as = doctrine
								}
							}
							add = ecumenical_council_doctrine_attendee_support_value
						}
					}
					remove_from_list = conservative
					add_to_list = innovator
					scope:activity = {
						remove_from_guest_subset = {
							name = conservative
							target = prev
						}
						add_to_guest_subset = {
							name = innovator
							target = prev
						}
					}
				}
			}
			every_in_list = {
				list = innovator
				scope:activity = {
					add_to_variable_list = {
						name = major_innovator
						target = prev
					}
				}
			}
		}
		if = {
			limit = {
				any_in_list = {
					list = conservative
					count >= 3
				}
			}
			ordered_in_list = {
				list = conservative
				max = 3
				order_by = ecumenical_council_conservative_sort_value
				scope:activity = {
					add_to_variable_list = {
						name = major_conservative
						target = prev
					}
				}
			}
		}
		else = {
			every_in_list = {
				list = conservative
				scope:activity = {
					add_to_variable_list = {
						name = major_conservative
						target = prev
					}
				}
			}
		}
		# One AI Conservative argues against the host in every debate: the AI Head of Faith if he opposes, else the top mainline-rite Conservative
		remove_variable = ec_counterplayer
		every_in_list = {
			list = conservative
			limit = { scope:host.faith.religious_head ?= this }
			save_temporary_scope_as = ec_counterplayer_pick
		}
		if = {
			limit = { NOT = { exists = scope:ec_counterplayer_pick } }
			ordered_in_list = {
				list = conservative
				limit = { rite = scope:host.faith.main_rite }
				order_by = ecumenical_council_counterplayer_sort_value
				save_temporary_scope_as = ec_counterplayer_pick
			}
		}
		if = {
			limit = { NOT = { exists = scope:ec_counterplayer_pick } }
			ordered_in_list = {
				list = conservative
				order_by = ecumenical_council_counterplayer_sort_value
				save_temporary_scope_as = ec_counterplayer_pick
			}
		}
		if = {
			limit = { exists = scope:ec_counterplayer_pick }
			set_variable = {
				name = ec_counterplayer
				value = scope:ec_counterplayer_pick
			}
		}
		# Log the sides each debate starts with, since the AI is sorted anew for every proposal
		var:current_topic ?= { save_scope_as = ec_current_topic }
		if = {
			limit = { NOT = { exists = scope:actor } }
			scope:host = { save_scope_as = actor }
		}
		add_activity_log_entry = {
			key = ecumenical_council_lineup_log
			score = 0
			character = scope:host
			location = scope:host.location
			every_in_list = {
				list = innovator
				custom = game_concept_ec_innovators
				ecumenical_council_sway_stance_tt_effect = { SIDE = support }
			}
			every_in_list = {
				list = conservative
				custom = game_concept_ec_conservatives
				ecumenical_council_sway_stance_tt_effect = { SIDE = oppose }
			}
		}
		# Send event for player guests to know what was proposed
		every_attending_character = {
			limit = {
				is_ai = no
				this != scope:host
				pam_valid_ecumenical_council_voter_trigger = yes
			}
			trigger_event = ecumenical_council_events.$NEXT_EVENT$
		}
	}
}

# The Head check also needs the delegate check, because heading is a title property, not a personal rite
# Character scope, inside an Ecumenical Council event
pam_ecumenical_council_save_rite_champions_effect = {
	involved_activity = {
		random_attending_character = {
			limit = {
				pam_ecumenical_council_rite_delegate_trigger = { RITE = roman_rite }
				rite:roman_rite.head_of_rite ?= this
			}
			save_scope_as = ec_roman_champion
		}
		if = {
			limit = { NOT = { exists = scope:ec_roman_champion } }
			ordered_attending_character = {
				limit = { pam_ecumenical_council_rite_delegate_trigger = { RITE = roman_rite } }
				order_by = pam_ecumenical_council_rite_champion_sort_value
				save_scope_as = ec_roman_champion
			}
		}
		random_attending_character = {
			limit = {
				pam_ecumenical_council_rite_delegate_trigger = { RITE = byzantine_rite }
				rite:byzantine_rite.head_of_rite ?= this
			}
			save_scope_as = ec_greek_champion
		}
		if = {
			limit = { NOT = { exists = scope:ec_greek_champion } }
			ordered_attending_character = {
				limit = { pam_ecumenical_council_rite_delegate_trigger = { RITE = byzantine_rite } }
				order_by = pam_ecumenical_council_rite_champion_sort_value
				save_scope_as = ec_greek_champion
			}
		}
	}
}

# Activity scope: reads faith off the activity so it still works once the host is dead
pam_ecumenical_council_send_ruling_letter_effect = {
	if = {
		limit = { exists = var:ec_throttled_faith }
		save_scope_as = ec_council
		var:ec_throttled_faith = { save_scope_as = ec_council_faith }
		# Only players engaged with the Christian Church.
		every_player = {
			limit = {
				faith = scope:ec_council_faith
				NOT = { involved_activity ?= scope:ec_council }
				any_character_situation = {
					this = situation:the_christian_church
				}
			}
			# Cleared first so one ruler's correspondent is not inherited by the next in the loop.
			clear_saved_scope = ec_reporter
			# Chaplain first, then the most learned person answerable to us; the letter names no office.
			if = {
				limit = { exists = cp:councillor_court_chaplain }
				cp:councillor_court_chaplain = { save_scope_as = ec_reporter }
			}
			else_if = {
				limit = {
					any_courtier = {
						is_available_ai_adult = yes
						this != scope:host
					}
				}
				ordered_courtier = {
					order_by = learning
					max = 1
					limit = {
						is_available_ai_adult = yes
						this != scope:host
					}
					save_scope_as = ec_reporter
				}
			}
			else_if = {
				limit = {
					any_vassal = {
						is_available_ai_adult = yes
						this != scope:host
					}
				}
				ordered_vassal = {
					order_by = learning
					max = 1
					limit = {
						is_available_ai_adult = yes
						this != scope:host
					}
					save_scope_as = ec_reporter
				}
			}
			# With nobody to carry the news, the feed message stands on its own.
			if = {
				limit = { exists = scope:ec_reporter }
				trigger_event = ecumenical_council_events.0082
			}
		}
	}
}

# Root backs $RITE$ over $OTHER_RITE$ via $BACKED_CUSTOM$ / $SLIGHTED_CUSTOM$
# Character scope, inside an Ecumenical Council event
pam_ecumenical_council_side_with_rite_effect = {
	involved_activity = {
		every_attending_character = {
			custom = $BACKED_CUSTOM$
			limit = { pam_ecumenical_council_rite_delegate_trigger = { RITE = $RITE$ } }
			add_opinion = {
				target = root
				modifier = pam_rite_quarrel_championed_opinion
			}
		}
		every_attending_character = {
			custom = $SLIGHTED_CUSTOM$
			limit = { pam_ecumenical_council_rite_delegate_trigger = { RITE = $OTHER_RITE$ } }
			add_opinion = {
				target = root
				modifier = pam_rite_quarrel_slighted_opinion
			}
		}
	}
	pam_change_great_schism_progress_effect = {
		VALUE = pam_great_schism_progress_rite_quarrel_side_taken_value
		TT = pam_ecumenical_council_rite_quarrel_side_taken_tt
	}
}

# Call only behind pam_is_head_of_schism_pair_rite_trigger
# Character scope, inside an Ecumenical Council event
pam_ecumenical_council_rule_on_quarrel_effect = {
	pam_change_great_schism_progress_effect = {
		VALUE = pam_great_schism_progress_rite_quarrel_overruled_value
		TT = pam_ecumenical_council_rite_quarrel_ruled_tt
	}
}
