﻿############################################################
# Rise and Fall: Fix Bordergore scripted effects
# Called by a character interaction where a liege fixes vassal-held counties
# outside the target vassal's de jure title chains.
############################################################

riseandfall_fix_bordergore_se = {
    # CONTRACT (inputs/outputs/data/error)
    # Inputs: scope:actor (liege), scope:recipient (vassal)
    # Outputs: Nonde jure counties (direct or subrealm) moved to liege; aggregate notification if any moved.
    # Data shape: title holder changes via create_title_and_vassal_change / change_title_holder_include_vassals.
    # Error modes: Missing scopes (no action), no nonde jure counties (no message), invalid token (engine log error).

    scope:actor = { save_scope_as = liege }

    # 1. Directly held counties outside the recipient's primary region and held de-jure titles.
    # Snapshot the titles before changing their holders. Resolving a title/vassal change fires
    # on_vassal_change immediately; iterating a live held-title collection while that happens
    # can invalidate the character context used by vanilla's primary_title trigger.
    scope:recipient = {
        every_held_title = {
            # A holder's primary title defines their core de-jure region.
            limit = {
                tier = tier_county
                exists = de_jure_liege
                holder = scope:recipient
                NOT = { holder = scope:liege }
                # Do not take titles from human players
                NOT = { holder = { is_ai = no } }
                riseandfall_county_is_outside_holder_primary_dejure_trigger = yes
            }
            add_to_temporary_list = riseandfall_bordergore_direct_counties
        }
    }

    # Keep a character as the enclosing scope while each title transfer fires vanilla on_actions.
    scope:liege = {
        every_in_list = {
            list = riseandfall_bordergore_direct_counties
            # The recipient is already the liege's direct vassal. If every county
            # is non-de-jure, preserve the intact ruler instead of unlanding them.
            if = {
                limit = {
                    holder = {
                        any_held_title = {
                            tier = tier_county
                            NOT = { is_in_list = riseandfall_bordergore_direct_counties }
                        }
                    }
                }
                create_title_and_vassal_change = { type = granted save_scope_as = change }
                change_title_holder_include_vassals = { holder = scope:liege change = scope:change take_baronies = no }
                resolve_title_and_vassal_change = scope:change
                scope:liege = { set_variable = { name = riseandfall_fix_bordergore_made_changes value = 1 } }
            }
        }
    }

    # (duchy-transfer logic intentionally removed; only counties are transferred)

    # 2. Subrealm counties outside the target's primary region and personally held de-jure titles.
    scope:recipient = {
        every_sub_realm_county = {
            # Base filters: sub-realm county object exists, has de jure info, not liege/recipient capitals, not held by human
            limit = {
                exists = this
                exists = de_jure_liege
                NOT = { holder = scope:liege }
                NOT = { holder = { is_ai = no } }
                NOT = { holder = scope:recipient }
                NOT = { this = scope:liege.capital_province.county }
                NOT = { this = scope:recipient.capital_province.county }
            }
            save_scope_as = target_county

            # Build the final transfer set before changing any holder or liege.
            if = {
                limit = {
                    AND = {
                        riseandfall_county_is_outside_recipient_primary_dejure_trigger = yes
                        NOT = { AND = { has_variable = Riseandfall_giveaway_blocked scope:liege = { is_ai = no } } }
                    }
                }
                add_to_temporary_list = riseandfall_bordergore_subrealm_counties
            }
        }
    }

    scope:liege = {
        every_in_list = {
            list = riseandfall_bordergore_subrealm_counties
            save_scope_as = target_county

            # If every county held by this sub-vassal is in the transfer set, move
            # the intact ruler directly under the top liege instead of unlanding them.
            if = {
                limit = {
                    holder = {
                        liege != scope:liege
                        NOT = {
                            any_held_title = {
                                tier = tier_county
                                NOT = { is_in_list = riseandfall_bordergore_subrealm_counties }
                            }
                        }
                    }
                }
                create_title_and_vassal_change = { type = granted save_scope_as = change }
                holder = {
                    change_liege = {
                        liege = scope:liege
                        change = scope:change
                    }
                }
                resolve_title_and_vassal_change = scope:change
                scope:liege = { set_variable = { name = riseandfall_fix_bordergore_made_changes value = 1 } }
            }
            # After an intact ruler transfer, continue cleaning counties outside
            # that ruler's own primary region while preserving at least one core county.
            if = {
                limit = {
                    riseandfall_county_is_outside_holder_primary_dejure_trigger = yes
                    holder = {
                        liege = scope:liege
                        any_held_title = {
                            tier = tier_county
                            riseandfall_county_is_within_holder_primary_dejure_trigger = yes
                        }
                    }
                }
                create_title_and_vassal_change = { type = granted save_scope_as = change }
                scope:target_county = { change_title_holder_include_vassals = { holder = scope:liege change = scope:change take_baronies = no } }
                resolve_title_and_vassal_change = scope:change
                scope:liege = { set_variable = { name = riseandfall_fix_bordergore_made_changes value = 1 } }
            }
            # Mixed holders still under the target keep valid counties and lose
            # only counties outside the target's primary region.
            else_if = {
                limit = { holder = { liege != scope:liege } }
                create_title_and_vassal_change = { type = granted save_scope_as = change }
                scope:target_county = { change_title_holder_include_vassals = { holder = scope:liege change = scope:change take_baronies = no } }
                resolve_title_and_vassal_change = scope:change
                scope:liege = { set_variable = { name = riseandfall_fix_bordergore_made_changes value = 1 } }
            }
        }
    }

    # 3. Aggregate notification.
    scope:liege = {
        if = {
            limit = { has_variable = riseandfall_fix_bordergore_made_changes }
            send_interface_message = {
                type = send_interface_message_good
                title = riseandfall_fix_bordergore_notification_title
                custom_tooltip = { text = riseandfall_fix_bordergore_notification_desc_aggregate }
                left_icon = scope:liege
            }
            remove_variable = riseandfall_fix_bordergore_made_changes
        }
    }
}
