
appoint_mistress_position_effect = {
	scope:actor = {
		appoint_court_position = {
			recipient = scope:recipient
			court_position = mistress_court_position
		}
	}
	scope:recipient = {
		basic_gained_court_position_effect = yes
	}
}

make_mistress_effect = {
	scope:employee = {
		every_spouse = {
			if = {
				limit = {
					NOR = {
						has_relation_rival = scope:liege
						has_relation_potential_rival = scope:liege
					}
					scope:liege = { is_alive = yes }
				}
				set_relation_potential_rival = scope:liege
			}
		}
		
		every_secret = {
			limit = {
				secret_type = secret_lover
				secret_target = scope:liege
			}
			remove_secret = yes
		}
		
		# If both female and male adultery are illegal then this is scandalous by default
		if = {
			limit = {
				scope:liege = {
					faith = {
						has_doctrine_parameter = adultery_female_crime
						has_doctrine_parameter = adultery_male_crime
					}
				}
			}
			make_scandalous_mistress_effect = yes
		}
		else_if = {  #if the mistress is an unconventional choice...
			limit = {
				sex_same_as = scope:liege
				scope:liege = {
					faith = { NOT = { has_doctrine_parameter = homosexuality_accepted } }
				}
			}
			make_scandalous_mistress_effect = yes
		}
		else_if = {
			limit = {
				sex_same_as = scope:liege
				scope:liege = {
					faith = {
						has_doctrine_parameter = homosexuality_accepted
						OR = {
							has_doctrine_parameter = adultery_male_accepted
							has_doctrine_parameter = adultery_female_accepted
							has_doctrine_parameter = no_unfaithfulness_penalty_active
						}
					}
				}
			}
			make_neutral_mistress_effect = yes
		}
		else_if = {
			limit = {
				NOT = { sex_same_as = scope:liege }
				scope:liege = {
					faith = {
						OR = {
							has_doctrine_parameter = adultery_male_accepted
							has_doctrine_parameter = adultery_female_accepted
							has_doctrine_parameter = no_unfaithfulness_penalty_active
						}
					}
				}
			}
			make_neutral_mistress_effect = yes
		}
		else = { make_bad_mistress_effect = yes }
	}
	
	scope:liege = {
		every_secret = {
			limit = {
				secret_type = secret_lover
				secret_target = scope:employee
			}
			remove_secret = yes
		}
	}
}

# Taking a mistress is still considered not great in general
make_bad_mistress_effect = {
	scope:liege = {
		every_spouse = {
			add_opinion = {
				modifier = took_mistress_opinion
				target = scope:liege
				opinion = -15
			}
			add_opinion = {
				modifier = spouses_mistress_opinion
				target = scope:employee
				opinion = -15
			}
		}
		add_character_modifier = bad_mistress
		add_piety = minor_piety_loss
	}
	scope:employee = {
		every_spouse = {
			add_opinion = {
				modifier = took_my_spouse_as_mistress_opinion
				target = scope:liege
				opinion = -15
			}
			add_opinion = {
				modifier = spouse_becomes_mistress_opinion
				target = scope:employee
				opinion = -15
			}
		}
		add_piety = minor_piety_loss
	}
}

# Taking a mistress is just...meh to most people but their spouse might still not appreciate it
make_neutral_mistress_effect = {
	scope:liege = {
		every_spouse = {
			add_opinion = {
				modifier = took_mistress_opinion
				target = scope:liege
				opinion = -5
			}
			
			add_opinion = {
				modifier = spouses_mistress_opinion
				target = scope:employee
				opinion = -5
			}
		}
	}
	scope:employee = {
		every_spouse = {
			add_opinion = {
				modifier = took_my_spouse_as_mistress_opinion
				target = scope:liege
				opinion = -5
			}
			add_opinion = {
				modifier = spouse_becomes_mistress_opinion
				target = scope:employee
				opinion = -5
			}
		}
	}
}

# Clutching pearls!
make_scandalous_mistress_effect = {
	scope:liege = {
		every_spouse = {
			add_opinion = {
				modifier = took_scandalous_mistress_opinion
				target = scope:liege
				opinion = -25
			}
			add_opinion = {
				modifier = spouses_scandalous_mistress_opinion
				target = scope:employee
				opinion = -25
			}
		}
		add_character_modifier = scandalous_mistress
		add_piety = medium_piety_loss
	}
	scope:employee = {
		every_spouse = {
			add_opinion = {
				modifier = took_my_spouse_as_mistress_opinion
				target = scope:liege
				opinion = -25
			}
			add_opinion = {
				modifier = spouse_becomes_mistress_opinion
				target = scope:employee
				opinion = -25
			}
		}
		add_piety = medium_piety_loss
	}
}


# Pregnancy Stuffs

mistress_pregnancy_effect = {
	add_trait_force_tooltip = pregnant
	hidden_effect = {
		set_known_bastard_on_pregnancy = yes #The bastard status is already known since the mother is unmarried.
		add_character_flag = {
            flag = mistress_pregnancy_effect
            days = 22
        }
        trigger_event = {
            id = mistress_pregnancy.5001
            days = 5
        }
	}
}

#Overriding Vanilla Definition
extramarital_sex_exposure_consequences_with_target_effect = { #Note: only adds stuff for one of the sex partners, it must be run twice! It is intentionally kept this way so you can apply it in two different places in an event chain etc.
	save_temporary_scope_as = sex_character
	
	#Applies traits, piety level loss, unfaithfulness opinions
	$TARGET$ = { save_scope_as = sex_partner }
	if = { # Check to make absolutely sure it's extramarital
		limit = {
			NOT = { scope:sex_character = { is_consort_of = scope:sex_partner } }
		}
		limit = {
			NOR = {
				scope:sex_character = {
					OR = {
						is_consort_of = scope:sex_partner
						AND = {
							has_court_position = mistress_court_position
							is_courtier_of = scope:sex_character
							OR = {
								has_relation_soulmate = scope:sex_character
								has_relation_lover = scope:sex_character
							}
						}
					}
				}
				scope:sex_partner = {
					AND = {
						has_court_position = mistress_court_position
						is_courtier_of = scope:sex_character
						OR = {
							has_relation_soulmate = scope:sex_character
							has_relation_lover = scope:sex_character
						}
					}
				}
			}
		}
		
		extramarital_sex_exposure_consequences_effect = { SPOUSE_EVENT = $SPOUSE_EVENT$ }
		
		#Slept with spouse opinions & potential rivalry
		$TARGET$ = {
			every_spouse = {
				limit = { NOT = { this = scope:sex_character } } #Extra safety
				save_scope_as = sex_partner_spouse
				
				hidden_effect = {
					#Remove duplicate opinion from discovery
					if = {
						limit = {
							has_opinion_modifier = {
								target = scope:sex_character
								modifier = slept_with_spouse_discovered_opinion
							}
						}
						remove_opinion = {
							target = scope:sex_character
							modifier = slept_with_spouse_discovered_opinion
						}
					}
				}
				
				if = {
					limit = {
						NOT = { faith = { has_doctrine_parameter = no_unfaithfulness_penalty_active }  }
					}
					if = { #(only visible for directly relevant exposure)
						limit = {
							always = yes #TODO_CD: commented out until TIT-16673 has been resolved due to errors
							#OR = {
							#	scope:sex_character = root
							#	scope:sex_partner = root
							#	scope:sex_partner_spouse = root
							#}
						}
						add_opinion = {
							target = scope:sex_character
							modifier = slept_with_spouse_exposed_opinion
						}
					}
					else = {
						hidden_effect = {
							add_opinion = {
								target = scope:sex_character
								modifier = slept_with_spouse_exposed_opinion
							}
						}
					}
					hidden_effect = {
						#Rivalry
						if = {
							limit = {
								is_ai = yes
								scope:sex_character = { is_ai = yes }
								ai_vengefulness >= medium_positive_ai_value
								can_set_relation_nemesis_trigger = { CHARACTER = scope:sex_character }
							}
							set_relation_nemesis = {
								reason = nemesis_extramarital_sex_exposure
								copy_reason = rival
								target = scope:sex_character
								involved_character = scope:sex_partner
							}
						}
						else = {
							scope:sex_partner = { save_scope_as = relationship_reason_involved_character }
							progress_towards_rival_effect = { CHARACTER = scope:sex_character OPINION = 0 REASON = rival_slept_with_spouse }
							clear_saved_scope = relationship_reason_involved_character
						}
					}
				}
			}
		}
	}
}
