﻿on_death_relation_oathbound = { # Triggers from person that died. So we use scope:target to get the actor (us). -- Oathholder dies essentially.
	on_actions = {
		the_forced_oathbound_heir

		the_permanent_oath_oathholder_perspective
		the_permanent_oath_oathholder_perspective_not_valid

		oathholder_dies_notif

		oathholders_task_cleanup
		oathholders_cleanup
	}
}

the_forced_oathbound_heir = { # Ambitious characters only.
	trigger = {
		AND = {
			NOT = { 
				scope:target = { has_character_flag = oathbound_heir }
				scope:target = { has_character_modifier = permanent_oath }
			}
			scope:target.primary_heir = { is_child_of = scope:target }

			has_trait = ambitious
			is_ai = yes
			
			# This is root, var:val_oathholder.
			primary_heir = { is_child_of = root }			
		}
	}
	effect = {
		scope:target = {
			add_character_flag = {
				flag = oathbound_heir
				days = 100
			}

			oathholder_heir_assigned = { CHARACTER = root.primary_heir }

			trigger_event = oathbound_heir_events.0001
		}
	}
}

the_permanent_oath_oathholder_perspective = {
	trigger = {
		scope:target = { has_character_modifier = permanent_oath } # Oathbound has permanent_oath
		primary_heir.dynasty = root.dynasty
	}
	effect = {
		scope:target = {
            add_character_modifier = { modifier = permanent_oath }
            add_trait = oathkeeper
            add_trait = loyal
            remove_trait = disloyal

            add_opinion = {
                target = root.primary_heir 
                modifier = oath_holder_opinion
            }
            set_relation_oathholder = {
                target = root.primary_heir
            }
            
            # Switch into the heir's scope
            root.primary_heir = {
                oathkeeper_assigned = { CHARACTER = scope:target }

				create_character_memory = {
					type = gained_oathbound_contract_memory
					participants = {
						new_servant = scope:target
					}
				}
                                
                add_character_modifier = { modifier = oathholder_life }
				if = {
					limit = { is_female = yes }
					add_trait = oathholder_female
				}
				else = {
					add_trait = oathholder_male
				}

                add_hook = {
                    target = scope:target
                    type = obligation_hook
                }
            }
			
			oathholder_assigned = { CHARACTER = var:val_oathholder.primary_heir}
			create_character_memory = {
                type = swore_oathbound_contract_memory
                participants = {
                    new_boss = root.primary_heir
                }
            }
			trigger_event = {
				id = oathbound_permanent_events.0003
				days = 1
			}
		}
	}
}

the_permanent_oath_oathholder_perspective_not_valid = {
	trigger = {
		scope:target = { has_character_modifier = permanent_oath } # Oathbound has permanent_oath
		NOT = {
			primary_heir.dynasty = root.dynasty
		}
		#OR = {
		#	NOT = {
		#		primary_heir = { is_child_of = root } # Oathholders heir is not their child.
		#		primary_heir = { is_sibling_of = root }
		#		primary_heir = { is_grandchild_of = root }
		#	}
		#}	
	}
	effect = {
		scope:target = {
			trigger_event = {
				id = oathbound_permanent_events.0004
				#days = 1
			}
		}
	}
}

oathholder_dies_notif = {
	effect = {
		scope:target = {
			trigger_event = oathbound_notif.0001
		}
	}
}

oathholders_cleanup = {
    effect = {
        root = {
            remove_variable = val_oathkeeper
            
            remove_variable = val_oathholder
            remove_variable = val_oathholder_heir
        }

        scope:target = {
            if = {
                limit = {
                    has_relation_oathholder = root 
                }
                remove_relation_oathholder = root
            }
            if = {
                limit = {
                    exists = var:val_oathholder
                    var:val_oathholder = root
                }
                remove_variable = val_oathholder
            }
			if = {
				limit = {
					has_trait = oathkeeper
					NOT = { has_character_modifier = permanent_oath }
				}
				remove_trait = oathkeeper
			}
			if = {
                limit = {
                    has_character_flag = oathbound_on_travel_job 
                }
                remove_character_flag = oathbound_on_travel_job
            }
			if = {
                limit = {
                    has_character_flag = oathbound_control_job 
                }
                remove_character_flag = oathbound_control_job
            }
        }
    }
}

oathholders_task_cleanup = {
    effect = {
        scope:target = {
            
            # --- 1. TRAVEL JOB CLEANUP (Enforce Control) --- #
            if = {
                limit = {
                    OR = {
                        has_character_flag = oathbound_on_travel_job 
                        has_character_flag = oathbound_control_job 
                    }
                }
                var:oathbound_control_task_county_var = {
                    remove_county_modifier = oathbound_enforcing_order_weak
                    remove_county_modifier = oathbound_enforcing_order_normal
                    remove_county_modifier = oathbound_enforcing_order_strong
                }
                remove_character_flag = oathbound_on_travel_job
                remove_character_flag = oathbound_control_job
                remove_variable = oathbound_control_task_county_var
            }

            ## --- 2. STATIONARY JOB CLEANUP (Sway Vassal) --- #
            if = {
                limit = {
                    OR = {
                        has_character_flag = oathbound_on_stationary_job
                        has_character_flag = oathbound_sway_job
                    }
                }

                # Oathholder is dead but oathbound is alive — end the scheme, it died with its patron.
                every_scheme = {
                    limit = {
                        scheme_type = oathbound_sway_vassals
                        scheme_target_character = {
                            this = root.var:oathbound_sway_task_target_var
                        }
                    }
                    end_scheme = yes
                }

                # Clean up the Player's Flags and Variables
                remove_character_flag = oathbound_on_stationary_job
                remove_character_flag = oathbound_sway_job
                remove_variable = oathbound_sway_task_target_var
            }

            # --- 3. STATIONARY JOB CLEANUP (Kill Rival) --- #
            if = {
                limit = {
                    OR = {
                        has_character_flag = oathbound_on_stationary_job
                        has_character_flag = oathbound_kill_job
                    }
                }

                # Oathholder is dead but oathbound is alive — end the scheme, it died with its patron.
                every_scheme = {
                    limit = {
                        scheme_type = murder
                        scheme_target_character = {
                            this = root.var:oathbound_kill_task_target_var
                        }
                    }
                    end_scheme = yes
                }

                # Clean up the Player's Flags and Variables
                remove_character_flag = oathbound_on_stationary_job
                remove_character_flag = oathbound_kill_job
                remove_variable = oathbound_kill_task_target_var
            }
            # --- 4. TRAVEL JOB CLEANUP (Duel Rival) --- #
            if = {
                limit = {
                    OR = {
                        has_character_flag = oathbound_on_travel_job
                        has_character_flag = oathbound_duel_job
                    }
                }
                remove_character_flag = oathbound_on_travel_job
                remove_character_flag = oathbound_duel_job
                remove_variable = oathbound_duel_task_target_var
            }
        }
    }
}