create_cadet_branch_decision = {
	picture = "gfx/interface/illustrations/decisions/decision_dynasty_house.dds"

	major = yes
	ai_check_interval = 60

	is_shown = {
		NOT = { has_game_rule = no_cadet }
		is_landed = yes
		exists = house
		exists = house.house_head
		is_house_head = no
		primary_title.tier > tier_barony
		NOR = {
			has_government = holy_order_government
			has_government = theocracy_government
			AND = {
				faith = { has_doctrine = doctrine_theocracy_temporal }
				has_council_position = councillor_court_chaplain
			}
		}
	}

	is_valid = {
		house.house_head = {
			government_allows = create_cadet_branches
			custom_description = {
				text = create_cadet_branch_decision_succession_line
				NOT = {
					any_held_title = {
						place_in_line_of_succession = {
							target = root
							value <=3
						}
					}
				}
			}
		}

		custom_description = {
			text = create_cadet_branch_decision_ancestor_in_house
			NOT = {
				any_ancestor = {
					house = root.house
				}
			}
		}
		
		trigger_if = {
			limit = {
				is_married = yes
				is_male = yes
			}
			patrilinear_marriage = yes
		}
		trigger_if = {
			limit = {
				is_married = yes
				is_female = yes
			}
			matrilinear_marriage = yes
		}
		trigger_if = { #Males of female-dominated faith must already be patrilineally married before taking this decision.
			limit = {
				is_married = no
				is_female = no
				faith = {
					has_doctrine = doctrine_gender_female_dominated
				}
			}
			patrilinear_marriage = yes
		}
		trigger_if = { #Females of male-dominated faith must already be matrilineally married before taking this decision.
			limit = {
				is_married = no
				is_female = yes
				faith = {
					has_doctrine = doctrine_gender_male_dominated
				}
			}
			matrilinear_marriage = yes
		}
		trigger_if = {
			limit = {
				has_trait = devoted
			}
			NOT = {
				has_trait = devoted
			}
		}
		trigger_if = {
			limit = {
				has_trait = bastard
			}
			NOT = {
				has_trait = bastard
			}
		}
		trigger_if = {
			limit = { has_game_rule = hard_cadet }
			prestige_level >= 3
			highest_held_title_tier >= tier_duchy
			NOT = { has_trait = content }
		}
	}

	is_valid_showing_failures_only = {
		is_available_adult = yes
		is_landed = yes
	}

	effect = {
		hidden_effect = {
			house.house_head = {
				save_scope_as = former_house_head
			}
			house = {
				save_scope_as = former_house
			}
			add_achievement_flag = { FLAG = achievement_a_house_of_my_own_flag }
		}
		create_cadet_branch = yes
		add_prestige = major_prestige_gain
		hidden_effect = {
			save_scope_as = new_head
			house = {
				save_scope_as = new_house
			}
			every_player = {
				limit = {
					exists = house
					house = scope:new_house
					NOT = { this = root }
				}
				send_interface_toast = {
					type = msg_created_new_house
					title = created_cadet_branch_toast_new_house_desc
					left_icon = scope:new_head
					desc = created_cadet_branch_toast_new_house_other_tt
				}
			}
			send_interface_toast = {
				type = msg_created_new_house
				title = created_cadet_branch_toast_new_house_desc
				left_icon = scope:new_head
				right_icon = scope:former_house_head
				desc = created_cadet_branch_toast_new_house_other_tt
			}
		}
	}
	
	ai_potential = {
		always = yes
	}

	ai_will_do = {
		base = 30
		
		modifier = {
			add = 70
			exists = primary_title
			primary_title.tier > tier_county
		}
		
		modifier = {
			factor = 0
			any_child = {
				is_alive = yes
				count < 3
			}
		}
		
		modifier = {
			factor = 0
			any_player = {
				OR = {
					any_child = {
						OR = {
							is_child_of = root
							is_grandchild_of = root
							is_great_grandchild_of = root
						}
					}
					any_child = {
						even_if_dead = yes
						any_child = {
							OR = {
								is_child_of = root
								is_grandchild_of = root
								is_great_grandchild_of = root
							}
						}
					}
					any_child = {
						even_if_dead = yes
						any_child = {
							even_if_dead = yes
							any_child = {
								OR = {
									is_child_of = root
									is_grandchild_of = root
									is_great_grandchild_of = root
								}
							}
						}
					}
				}
			}
		}
	}
}
