namespace = mistress_pregnancy


# Override vanilla trigger so we can use it
scripted_trigger override_former_consort_was_father = {
	OR = {
		any_former_spouse = {
			even_if_dead = yes
			this = scope:father
			this = scope:real_father
			trigger_if = {
				limit = {
					is_alive = yes
				}
				NOT = { any_consort = { this = root } }
			}
		}
		any_former_concubinist = {
			even_if_dead = yes
			this = scope:father
			this = scope:real_father
			trigger_if = {
				limit = {
					is_alive = yes
				}
				NOT = { any_consort = { this = root } }
			}
		}
		any_former_concubine = {
			even_if_dead = yes
			this = scope:father
			this = scope:real_father
			trigger_if = {
				limit = {
					is_alive = yes
				}
				NOT = { any_consort = { this = root } }
			}
		}
	}
}

#Mistress pregnant with child - Refreshed!
mistress_pregnancy.1001 = {
	title = mistress_pregnancy.1001.t
	desc = {
		desc = mistress_pregnancy.1001.opening
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:real_father = { is_married = yes }
				}
				desc = mistress_pregnancy.1001.father_is_married
			}
			desc = mistress_pregnancy.1001.father_is_unmarried
		}
	}

	theme = pregnancy
	override_background = { reference = bedchamber }
	left_portrait = {
		character = root
	}
	right_portrait = {
		character = scope:real_father
		triggered_animation = {
			trigger = { has_any_bad_relationship_with_character_trigger = { CHARACTER = root } }
			animation = stress
		}
		animation = happiness
	}
	
	trigger = {
		is_pregnant = yes # Fix to prevent pipeline failures on account of set_num_pregnancy_children being used on an unpregnant character
		
		NOR = {
			# If we are married or have concubines (or are a concubine) this event is not valid, as there is a (assumed) father.
			any_consort = {
				exists = this
			}
			# Same for if a former spouse/concubine got us pregnant.
			override_former_consort_was_father = yes
			has_variable = borte_first_child_var
		}
		has_court_position = mistress_court_position
		is_courtier_of = scope:real_father
	}

	immediate = {
		play_music_cue = "mx_cue_succession"
		hidden_effect = {
			set_num_pregnancy_children = 1
			add_character_flag = {
				flag = unmarried_bastard_pregnancy
				months = 9
			}
		}
		mistress_pregnancy_effect = yes
	}

	option = { # Announce the pregnancy immediately
		name = mistress_pregnancy.1001.a
		bastardy_pregnancy_announcement_effect = yes
		hidden_effect = {
			expose_lover_secret_or_run_consequence_effect = {
				TARGET = scope:real_father
				EXPOSER = scope:mother
			}
		}
		ai_chance = {
			base = 0
			#ai_value_modifier = {
			#	ai_honor = 0.5
			#	ai_boldness = 0.75
			#	ai_compassion = -0.25
			#}
        }
	}

	option = { # Tell the father first
        name = mistress_pregnancy.1001.b
        hidden_effect = {
            add_character_flag = {
                flag = unmarried_mistress_will_tell_father
                days = 22
            }
        }
        custom_tooltip = reveal_illegitimate_pregnancy_to_real_father_tt

        ai_chance = {
            base = 7000
            ai_value_modifier = {
                ai_honor = 0.75
                ai_boldness = -0.25
                ai_compassion = 0.75
            }
        }
    }

	#option = { # Attempt to terminate the pregnancy
	#	name = mistress_pregnancy.1001.c
	#	pregnancy_termination_attempt_effect = yes
	#	ai_chance = {
	#		base = 0
			#ai_value_modifier = {
			#	ai_rationality = -0.75
            #    ai_boldness = -0.25 # the more craven the AI is, the more they will want to do this. Not that abortion is cowardly, but this is the path of least resistance.
			#}
	#		modifier = { # Never hide the truth in faiths with the Polyamory tenet.
	#			factor = 0
	#			faith = { has_doctrine_parameter = no_unfaithfulness_penalty_active }
	#		}
	#	}
	#}
}

# Mistress is a married woman :o - Refreshed!
mistress_pregnancy.1002 = {
	title = mistress_pregnancy.1002.t
	desc = {
		desc = mistress_pregnancy.1002.opening
		first_valid = {
			triggered_desc = {
				trigger = { faith = { has_doctrine_parameter = no_unfaithfulness_penalty_active } }
				desc = mistress_pregnancy.1002.father_is_married
			}
			desc = mistress_pregnancy.1002.father_is_unmarried
		}
	}
	
	theme = pregnancy
	left_portrait = {
		character = root
		animation = worry
	}
	right_portrait = scope:father
	lower_center_portrait = scope:real_father
	
	trigger = {
			# If we are married or have concubines (or are a concubine) this event is not valid, as there is a (assumed) father.
		any_consort = {
			is_male = yes
		}
		pregnancy_real_father = {
			NOT = {	is_consort_of = root }
		}
		
		# OBJECTION! If a former spouse/concubine is both the real and assumed father, it's not actually a bastard!
		NOT = {
			override_former_consort_was_father = yes
		}
		# Same for if a former spouse/concubine got us pregnant.
		has_court_position = mistress_court_position
		is_courtier_of = scope:real_father
	}
	
	immediate = {
		play_music_cue = "mx_cue_succession"
		# If we got pregnant before our marriage, there will be no assumed father set. However, we need one for the event logic to work correctly.
		# Since this is a pretty rare edge case, we just go ahead and set the assumed father to a current living spouse.
		if = {
			limit = {
				NOT = { exists = pregnancy_assumed_father }
			}
			random_consort = {
				save_scope_as = assumed_father
			}
			hidden_effect = {
				set_pregnancy_assumed_father = scope:assumed_father
			}
		}
		
		save_scope_as = mother
		hidden_effect = {
			set_num_pregnancy_children = 1
			if = {
				limit = {
					exists = primary_partner
				}
				primary_partner = {
					save_scope_as = spouse
				}
			}
			else = {
				random_consort = {
					save_scope_as = spouse
				}
			}
			add_character_flag = {
				flag = bastard_pregnancy
				months = 9
			}
		}
		mistress_pregnancy_effect = yes
	}
	
	option = { # Announce the pregnancy immediately
		name = mistress_pregnancy.1002.a
		bastardy_pregnancy_announcement_effect = yes
		hidden_effect = {
			expose_lover_secret_or_run_consequence_effect = {
				TARGET = scope:real_father
				EXPOSER = scope:mother
			}
		}
		ai_chance = {
			base = 0
			#ai_value_modifier = {
			#	ai_honor = 0.5
			#	ai_boldness = 0.75
			#	ai_compassion = -0.25
			#}
		}
	}
	
	option = { # Tell the father first
		name = mistress_pregnancy.1002.b
		hidden_effect = {
			add_character_flag = {
				flag = married_mistress_will_tell_father
				days = 22
			}
		}
		custom_tooltip = reveal_illegitimate_pregnancy_to_real_father_tt
		
		ai_chance = {
			base = 7000
			ai_value_modifier = {
				ai_honor = 0.75
				ai_boldness = -0.25
				ai_compassion = 0.75
			}
		}
	}
}

# Male ruler being notified of fatherhood
# Mistress comes to married ruler
mistress_pregnancy.4001 = {
	type = character_event
	title = mistress_pregnancy.4001.t
	desc = mistress_pregnancy.4001.desc
	theme = pregnancy
	left_portrait = scope:mother
	right_portrait = scope:spouse

	trigger = {
		scope:mother = {
			is_pregnant = yes
			is_courtier_of = root
            has_court_position = mistress_court_position
		}
	}

	immediate = {
		play_music_cue = "mx_cue_succession"
		#The real father knows now
		scope:mother = {
			add_character_flag = {
				flag = pregnancy_real_father_knows
				months = 9
			}
		}
		#For portrait and desc purposes
		primary_spouse = {
			save_scope_as = spouse
		}
	}

	option = { #Let us tell the court!
		name =  mistress_pregnancy.4001.a
		save_scope_value_as = {
            name = real_father_revealing
            value = yes
        }
        bastardy_pregnancy_announcement_effect = yes
	}
}

# Mistress comes to unmarried ruler
mistress_pregnancy.4002 = {
	type = character_event
	title = mistress_pregnancy.4002.t
	desc = mistress_pregnancy.4002.desc
	theme = pregnancy
	left_portrait = scope:mother

	trigger = {
		scope:mother = {
			is_pregnant = yes
			is_courtier_of = root
            has_court_position = mistress_court_position
		}
	}

	immediate = {
		play_music_cue = "mx_cue_succession"
		#The real father knows now
		scope:mother = {
			add_character_flag = {
				flag = pregnancy_real_father_knows
				months = 9
			}
		}
	}

	option = { #Let us tell the court!
		name =  mistress_pregnancy.4002.a
		save_scope_value_as = {
            name = real_father_revealing
            value = yes
        }
        bastardy_pregnancy_announcement_effect = yes
	}
}

# Married ruler, married mistress
mistress_pregnancy.4003 = {
	type = character_event
	title = mistress_pregnancy.4003.t
	desc = mistress_pregnancy.4003.desc
	theme = pregnancy
	left_portrait = scope:mother
	right_portrait = scope:spouse
	
	trigger = {
		scope:mother = {
			is_married = yes
			is_pregnant = yes
			has_court_position = mistress_court_position
		}
	}
	
	immediate = {
		play_music_cue = "mx_cue_succession"
		#The real father knows now
		scope:mother = {
			add_character_flag = {
				flag = pregnancy_real_father_knows
				months = 9
			}
		}
		#For portrait and desc purposes
		primary_spouse = {
			save_scope_as = spouse
		}
	}
	
	option = { #Let us tell the court!
		name =  mistress_pregnancy.4003.a
		save_scope_value_as = {
			name = real_father_revealing
			value = yes
		}
		bastardy_pregnancy_announcement_effect = yes
	}
}

# Unmarried ruler, married mistress
mistress_pregnancy.4004 = {
	type = character_event
	title = mistress_pregnancy.4004.t
	desc = mistress_pregnancy.4004.desc
	theme = pregnancy
	left_portrait = scope:mother
	
	trigger = {
		scope:mother = {
			is_married = yes
			is_pregnant = yes
			has_court_position = mistress_court_position
		}
	}
	
	immediate = {
		play_music_cue = "mx_cue_succession"
		#The real father knows now
		scope:mother = {
			add_character_flag = {
				flag = pregnancy_real_father_knows
				months = 9
			}
		}
	}
	
	option = { #Let us tell the court!
		name =  mistress_pregnancy.4004.a
		save_scope_value_as = {
			name = real_father_revealing
			value = yes
		}
		bastardy_pregnancy_announcement_effect = yes
	}
}

#Misc Helper Event
mistress_pregnancy.5001 = {
	hidden = yes
	trigger = {
		is_pregnant = yes
		AND = {
		    has_court_position = mistress_court_position
            is_courtier_of = scope:real_father
        }
		OR = {
			has_character_flag = unmarried_bastard_pregnancy
			has_character_flag = bastard_pregnancy
		}
		NOR = {
			has_character_flag = pregnancy_real_father_of_bastard_is_known_flag
			has_character_flag = pregnancy_spouse_knows_of_bastardy
		}
	}

	immediate = {
		#If something has gone terribly wrong, apply the Pregnant trait to make sure it's there:
		if = {
			limit = {
				is_pregnant = yes #Double check that the Pregnant trait will never be added to someone who is not pregnant
				NOT = { has_trait = pregnant }
			}
			add_trait = pregnant
		}
		if = {
			limit = { #Mother has decided to tell
				has_character_flag = unmarried_mistress_will_tell_father
			}

            scope:real_father = {
                if = {
                    limit = {
                        #checking spouse
                        any_spouse = { count = 0 }
                    }
                    trigger_event = mistress_pregnancy.4002
                }
                else = {
                    trigger_event = mistress_pregnancy.4001
                }
            }
        }
		else_if = {
			limit = { #Mother has decided to tell
				has_character_flag = married_mistress_will_tell_father
			}
			scope:real_father = {
				if = {
					limit = {
						#checking spouse
						any_spouse = { count = 0 }
					}
					trigger_event = mistress_pregnancy.4004
				}
				else = {
					trigger_event = mistress_pregnancy.4003
				}
			}
		}
        else = {
            bastard_real_father_suspicion_chance_effect = yes
        }
	}
}