﻿riseandfall_check_puppet_regency_end = {
	every_ruler = {
		limit = {
			OR = {
				has_diarchy_type = puppet_regency
				has_character_flag = rf_puppet_regency_persistent
				employs_court_position = riseandfall_puppet_master_court_position
			}
		}
		riseandfall_maintain_current_puppet_regency_se = yes
	}
}

# Expected scope: current character = the one puppet-regency liege being repaired.
# Keep this root-specific so diarchy on_actions cannot recursively scan every ruler.
riseandfall_maintain_current_puppet_regency_se = {
	riseandfall_recover_puppet_regency_se = yes
	# The vanilla Discharge Entrenched Regent interaction marks the liege
	# before deliberately ending the diarchy. Treat that as explicit removal.
	if = {
		limit = {
			has_character_flag = rf_puppet_regency_persistent
			NOT = { has_active_diarchy = yes }
			has_character_flag = trying_to_dismiss_regent
		}
		remove_character_flag = rf_puppet_regency_persistent
		remove_variable = rf_puppet_regency_diarch
		riseandfall_puppet_regency_reconcile_succession_se = yes
	}
	if = {
		limit = { has_diarchy_type = puppet_regency }
		# If the active puppet master became unable to serve this ruler, replace
		# them before syncing the position. Landed vassals may remain diarchs.
		riseandfall_recover_invalid_puppet_master_se = yes
		riseandfall_mark_puppet_regency_active_se = yes
	}
	# Script-driven court position assignment/cleanup. This also revokes a
	# stale office after an explicit diarchy removal.
	riseandfall_sync_puppet_master_court_position = yes
}

riseandfall_mark_puppet_regency_active_se = {
	if = {
		limit = {
			has_diarchy_type = puppet_regency
			has_active_diarchy = yes
		}
		add_character_flag = rf_puppet_regency_persistent
		if = {
			limit = { exists = diarch }
			set_variable = { name = rf_puppet_regency_diarch value = diarch }
		}
		if = {
			limit = { exists = diarchy_successor }
			set_variable = { name = rf_puppet_regency_successor value = diarchy_successor }
		}
	}
}

# Expected scope: root = dying character.
riseandfall_preserve_puppet_master_successor_on_death_se = {
    save_scope_as = rf_dying_puppet_master
    if = {
        limit = {
            exists = employer
            employer = {
                has_diarchy_type = puppet_regency
                has_active_diarchy = yes
                exists = diarch
                diarch ?= scope:rf_dying_puppet_master
            }
        }
        employer = {
            if = {
                limit = { exists = diarchy_successor }
                set_variable = { name = rf_puppet_regency_successor value = diarchy_successor }
            }
            riseandfall_queue_puppet_master_court_position_resync_se = yes
        }
    }
    else_if = {
        limit = {
            exists = liege
            liege = {
                has_diarchy_type = puppet_regency
                has_active_diarchy = yes
                exists = diarch
                diarch ?= scope:rf_dying_puppet_master
            }
        }
        liege = {
            if = {
                limit = { exists = diarchy_successor }
                set_variable = { name = rf_puppet_regency_successor value = diarchy_successor }
            }
            riseandfall_queue_puppet_master_court_position_resync_se = yes
        }
    }
    clear_saved_scope = rf_dying_puppet_master
}

# Rebuild a Puppet Regency that disappeared without its persistent marker being
# cleared. The marker is intentionally not removed when no candidate is found;
# the next safety pulse can try again after the court changes.
riseandfall_recover_puppet_regency_se = {
	if = {
		limit = {
			has_character_flag = rf_puppet_regency_persistent
			NOT = { has_active_diarchy = yes }
			is_landed = yes
			is_independent_ruler = yes
		}
		# Missing-diarchy states during death are transient. Resolve once after
		# vanilla succession and court-position teardown have completed.
		riseandfall_queue_puppet_master_court_position_resync_se = yes
	}
}

riseandfall_restore_puppet_regency_after_teardown_se = {
	if = {
		limit = {
			has_character_flag = rf_puppet_regency_persistent
			NOT = { has_active_diarchy = yes }
			is_landed = yes
			is_independent_ruler = yes
		}
		save_scope_as = rf_puppet_liege
		clear_saved_scope = rf_puppet_regency_recovery_diarch

		# The successor captured from the native diarchy line before death has
		# priority over the former diarch reference and any generic courtier.
		if = {
			limit = {
				has_variable = rf_puppet_regency_successor
				var:rf_puppet_regency_successor = {
					is_alive = yes
					is_adult = yes
					is_imprisoned = no
					is_incapable = no
					OR = {
						is_ruler = no
						is_vassal_of = scope:rf_puppet_liege
					}
				}
			}
			var:rf_puppet_regency_successor = { save_scope_as = rf_puppet_regency_recovery_diarch }
		}
		if = {
			limit = {
				NOT = { exists = scope:rf_puppet_regency_recovery_diarch }
				has_variable = rf_puppet_regency_diarch
				var:rf_puppet_regency_diarch = {
					is_alive = yes
					is_adult = yes
					is_imprisoned = no
					is_incapable = no
					OR = {
						is_ruler = no
						is_vassal_of = scope:rf_puppet_liege
					}
				}
			}
			var:rf_puppet_regency_diarch = { save_scope_as = rf_puppet_regency_recovery_diarch }
		}
		if = {
			limit = { NOT = { exists = scope:rf_puppet_regency_recovery_diarch } }
			ordered_courtier = {
				limit = {
					is_alive = yes
					is_adult = yes
					is_imprisoned = no
					is_incapable = no
				}
				order_by = {
					add = intrigue
					add = diplomacy
					add = stewardship
					add = martial
					add = learning
				}
				max = 1
				save_scope_as = rf_puppet_regency_recovery_diarch
			}
		}
		if = {
			limit = { exists = scope:rf_puppet_regency_recovery_diarch }
			try_start_diarchy = puppet_regency
			if = {
				limit = { has_active_diarchy = yes }
				set_diarch = scope:rf_puppet_regency_recovery_diarch
				set_diarchy_type = puppet_regency
				set_diarchy_swing = 90
				set_variable = { name = rf_puppet_regency_diarch value = scope:rf_puppet_regency_recovery_diarch }
				remove_variable = rf_puppet_regency_successor
			}
		}
		clear_saved_scope = rf_puppet_regency_recovery_diarch
		clear_saved_scope = rf_puppet_liege
	}
}

# Intentional removal path. Clear the marker before ending the diarchy so the
# periodic recovery cannot recreate a deliberately removed Puppet Regency.
riseandfall_end_puppet_regency_forcefully_se = {
	if = {
		limit = { has_diarchy_type = puppet_regency }
		riseandfall_clear_native_ruler_puppet_se = yes
		remove_character_flag = rf_puppet_regency_persistent
		remove_variable = rf_puppet_regency_diarch
		remove_variable = rf_puppet_regency_successor
		end_diarchy = yes
		riseandfall_puppet_regency_reconcile_succession_se = yes
	}
}

riseandfall_recover_invalid_puppet_master_se = {
	save_scope_as = rf_puppet_liege
    if = {
        limit = {
            has_diarchy_type = puppet_regency
            has_active_diarchy = yes
            OR = {
                NOT = { exists = diarch }
                diarch = {
                    OR = {
                        is_alive = no
                        AND = {
                            is_ruler = yes
                            NOT = { is_vassal_of = scope:rf_puppet_liege }
                        }
                        is_imprisoned = yes
                        is_adult = no
                        is_incapable = yes
                    }
                }
            }
        }
        # Diarch death briefly exposes an empty/invalid diarch before vanilla
        # advances its succession line. Do not overwrite that transition.
        riseandfall_queue_puppet_master_court_position_resync_se = yes
    }
	clear_saved_scope = rf_puppet_liege
}

# Resolve an invalid Puppet Master only after diarchy and court-position death
# processing has settled. Expected scope: root = puppet-regency liege.
riseandfall_resolve_invalid_puppet_master_after_teardown_se = {
	save_scope_as = rf_puppet_liege
    if = {
        limit = {
            has_diarchy_type = puppet_regency
            has_active_diarchy = yes
            OR = {
                NOT = { exists = diarch }
                diarch = {
                    OR = {
                        is_alive = no
                        AND = {
                            is_ruler = yes
                            NOT = { is_vassal_of = scope:rf_puppet_liege }
                        }
                        is_imprisoned = yes
                        is_adult = no
                        is_incapable = yes
                    }
                }
            }
        }
        clear_saved_scope = rf_replacement_puppet_master

        # Preserve CK3's actual diarchy succession order before considering a
        # generic courtier fallback.
        if = {
            limit = {
                exists = diarchy_successor
                diarchy_successor = {
                    is_alive = yes
                    is_adult = yes
                    is_imprisoned = no
                    is_incapable = no
                    OR = {
                        is_ruler = no
                        is_vassal_of = scope:rf_puppet_liege
                    }
                }
            }
            diarchy_successor = { save_scope_as = rf_replacement_puppet_master }
        }

		if = {
			limit = {
				NOT = { exists = scope:rf_replacement_puppet_master }
				has_variable = rf_puppet_regency_successor
				var:rf_puppet_regency_successor = {
					is_alive = yes
					is_adult = yes
					is_imprisoned = no
					is_incapable = no
					OR = {
						is_ruler = no
						is_vassal_of = scope:rf_puppet_liege
					}
				}
			}
			var:rf_puppet_regency_successor = { save_scope_as = rf_replacement_puppet_master }
		}

        if = {
            limit = { NOT = { exists = scope:rf_replacement_puppet_master } }
            ordered_courtier = {
                limit = {
                    is_alive = yes
                    is_adult = yes
                    is_imprisoned = no
                    is_incapable = no
                    OR = {
                        is_ruler = no
                        is_vassal_of = scope:rf_puppet_liege
                    }
                }
                order_by = {
                    add = intrigue
                    add = diplomacy
                    add = stewardship
                    add = martial
                    add = learning
                }
                max = 1
                save_scope_as = rf_replacement_puppet_master
            }
        }

        if = {
            limit = { exists = scope:rf_replacement_puppet_master }
            set_diarch = scope:rf_replacement_puppet_master
            set_diarchy_type = puppet_regency
            set_diarchy_swing = 90
            set_variable = { name = rf_puppet_regency_diarch value = scope:rf_replacement_puppet_master }
        }

        clear_saved_scope = rf_replacement_puppet_master
    }
	clear_saved_scope = rf_puppet_liege
}

riseandfall_clear_puppet_succession_pending_se = {
    remove_character_flag = rf_puppet_succession_pending
    remove_character_flag = rf_puppet_succession_heir_was_landed
    remove_character_flag = rf_puppet_succession_predecessor_admin
    remove_character_flag = rf_puppet_succession_event_queued
    remove_variable = rf_puppet_succession_predecessor
    remove_variable = rf_puppet_succession_diarch
    remove_variable = rf_puppet_succession_years
}

riseandfall_accept_puppet_succession_status_quo_se = {
    save_scope_as = rf_puppet_successor
    clear_saved_scope = rf_succession_puppet_master

    if = {
        limit = { has_variable = rf_puppet_succession_diarch }
        var:rf_puppet_succession_diarch = { save_scope_as = rf_succession_puppet_master }
    }

    if = {
        limit = {
            exists = scope:rf_succession_puppet_master
            scope:rf_succession_puppet_master = {
                is_alive = yes
                is_adult = yes
                is_imprisoned = no
                is_incapable = no
                OR = {
                    is_ruler = no
                    is_vassal_of = scope:rf_puppet_successor
                }
            }
        }
        if = {
            limit = { NOT = { has_active_diarchy = yes } }
            try_start_diarchy = puppet_regency
        }
        if = {
            limit = { has_active_diarchy = yes }
            scope:rf_succession_puppet_master = {
                if = {
                    limit = {
                        is_ruler = no
                        NOT = { employer = scope:rf_puppet_successor }
                    }
                    set_employer = scope:rf_puppet_successor
                }
            }
            set_diarch = scope:rf_succession_puppet_master
            set_diarchy_type = puppet_regency
            set_diarchy_swing = 90
        }
    }
    else = {
        # If the old puppet master cannot serve the new ruler, let the diarchy
        # system find a valid replacement and keep the persistent state alive.
        if = {
            limit = { NOT = { has_active_diarchy = yes } }
            try_start_diarchy = puppet_regency
        }
        if = {
            limit = { has_active_diarchy = yes }
            set_diarchy_type = puppet_regency
            set_diarchy_swing = 90
            riseandfall_recover_invalid_puppet_master_se = yes
        }
    }

    if = {
        limit = { has_active_diarchy = yes }
        add_character_flag = rf_puppet_regency_persistent
        if = {
            limit = { exists = diarch }
            set_variable = { name = rf_puppet_regency_diarch value = diarch }
        }
    }

    if = {
        limit = { has_variable = rf_puppet_succession_years }
        set_variable = { name = riseandfall_puppet_regency_years value = var:rf_puppet_succession_years }
    }

    riseandfall_sync_puppet_master_court_position = yes
    clear_saved_scope = rf_succession_puppet_master
    clear_saved_scope = rf_puppet_successor
}

riseandfall_resist_puppet_succession_status_quo_se = {
    if = {
        limit = {
            has_variable = rf_puppet_succession_diarch
            realm_size > 1
        }
        var:rf_puppet_succession_diarch = { save_scope_as = challenger }
        if = {
            limit = {
                exists = scope:challenger
                scope:challenger = { is_alive = yes }
            }
            riseandfall_clear_puppet_succession_pending_se = yes
            riseandfall_palace_coup_start_rebellion = yes
        }
        else = {
            riseandfall_accept_puppet_succession_status_quo_se = yes
            riseandfall_clear_puppet_succession_pending_se = yes
        }
    }
    else = {
        riseandfall_accept_puppet_succession_status_quo_se = yes
        riseandfall_clear_puppet_succession_pending_se = yes
    }
}
