namespace = SCS

# Marriage wrapper: sync ROOT and all current spouses, because on_marriage scope
# can vary by context/mod and may only hit one side.
character_event = {
    id = SCS.0
    hide_window = yes
    is_triggered_only = yes

    immediate = {
        character_event = { id = SCS.1 }
        any_spouse = {
            character_event = { id = SCS.1 }
        }
    }
}

# Main sync event for one character.
character_event = {
    id = SCS.1
    hide_window = yes
    is_triggered_only = yes

    trigger = {
        is_alive = yes
        age = 16
    }

    immediate = {
        # First remove stale spouse-derived weak claims.
        character_event = { id = SCS.2 }

        # Normal direction: male character receives weak claims from female spouse(s).
        if = {
            limit = {
                is_female = no
                is_married = yes
            }
            any_spouse = {
                limit = {
                    is_alive = yes
                    is_female = yes
                }
                any_claim = {
                    save_event_target_as = scs_claim_title
                    if = {
                        limit = {
                            ROOT = {
                                NOT = { has_claim = event_target:scs_claim_title }
                            }
                        }
                        add_weak_claim = ROOT
                        ROOT = { set_character_flag = scs_received_spouse_claims }
                    }
                }
            }
        }

        # Reverse direction: female character receives weak claims from male spouse(s),
        # if equality/matriarchal/custom conditions allow it.
        if = {
            limit = {
                is_female = yes
                is_married = yes
                scs_reverse_spouse_claims_trigger = yes
            }
            any_spouse = {
                limit = {
                    is_alive = yes
                    is_female = no
                }
                any_claim = {
                    save_event_target_as = scs_claim_title
                    if = {
                        limit = {
                            ROOT = {
                                NOT = { has_claim = event_target:scs_claim_title }
                            }
                        }
                        add_weak_claim = ROOT
                        ROOT = { set_character_flag = scs_received_spouse_claims }
                    }
                }
            }
        }
    }
}

# Remove stale weak claims that were probably granted by this mod.
# Safety rule: only removes weak claims, and skips titles where ROOT also has a strong claim.
# CK2's remove_claim is not source-aware; see README before using on valuable saves.
character_event = {
    id = SCS.2
    hide_window = yes
    is_triggered_only = yes

    trigger = {
        is_alive = yes
        has_character_flag = scs_received_spouse_claims
    }

    immediate = {
        any_claim = {
            save_event_target_as = scs_claim_title
            if = {
                limit = {
                    ROOT = {
                        has_weak_claim = event_target:scs_claim_title
                        NOT = { has_strong_claim = event_target:scs_claim_title }
                        NOR = {
                            AND = {
                                is_female = no
                                any_spouse = {
                                    is_alive = yes
                                    is_female = yes
                                    has_claim = event_target:scs_claim_title
                                }
                            }
                            AND = {
                                is_female = yes
                                scs_reverse_spouse_claims_trigger = yes
                                any_spouse = {
                                    is_alive = yes
                                    is_female = no
                                    has_claim = event_target:scs_claim_title
                                }
                            }
                        }
                    }
                }
                remove_claim = ROOT
            }
        }
    }
}

# Death wrapper: while the dead character may still have spouse links in the on_death
# context, ask surviving spouses to clean up. If not, SCS.20 will catch it later.
character_event = {
    id = SCS.5
    hide_window = yes
    is_triggered_only = yes

    immediate = {
        any_spouse = {
            character_event = { id = SCS.1 days = 1 }
        }
    }
}

# Low-impact yearly ruler maintenance: keeps claims synced when a spouse gains/loses
# claims during an existing marriage.
character_event = {
    id = SCS.10
    hide_window = yes
    is_triggered_only = yes
    only_rulers = yes

    trigger = {
        is_alive = yes
        age = 16
    }

    immediate = {
        character_event = { id = SCS.1 }
        any_spouse = {
            character_event = { id = SCS.1 }
        }
    }
}

# Cleanup pulse for anyone who already received modded claims but is no longer
# reliably reached by ruler/on_action maintenance. This is intentionally narrow.
character_event = {
    id = SCS.20
    hide_window = yes

    trigger = {
        is_alive = yes
        has_character_flag = scs_received_spouse_claims
    }

    mean_time_to_happen = {
        months = 6
    }

    immediate = {
        character_event = { id = SCS.1 }
    }
}
