﻿################################################################
# Ruler Reign Marker - on_actions
################################################################
# We extend the SHARED vanilla on_actions by CHAINING our own custom
# on_action via `on_actions = { ... }` - NOT by adding a raw `effect = {}`.
# Vanilla already defines an `effect` on these (on_title_gain,
# on_game_start_after_lobby, on_migration_*), and a SECOND `effect` block
# collides: CK3 logs "more than one 'effect' defined, using most recent"
# and keeps only one, silently dropping the other. `on_actions` is a
# different key, so it merges cleanly and BOTH run. Chained custom
# on_actions inherit the parent scope (root / scope:title / scope:domain).
################################################################

# --- Normal title transfers: inheritance, election, conquest, grant.
#     root = new holder, scope:title = the title. Nomad MIGRATIONS do NOT
#     fire this (batched create_title_and_vassal_change) - see below. ---
on_title_gain = {
	on_actions = { rrm_on_title_gain }
}
rrm_on_title_gain = {
	effect = {
		rrm_stamp_holder_gain_date_effect = yes
	}
}

# --- Title destroyed: NOT a holder gain, so on_title_gain never fires and the
#     "Destroyed" history row gets no timeline slot. That slotless row at the top
#     of the history offsets every older row's END by one slot (each then renders
#     its OWN start, e.g. "20 Feb 932 - 20 Feb 932"). Stamp the destruction date
#     as a slot so the timeline realigns 1:1 with the history rows.
#     root = last holder, scope:landed_title = the title (NOTE: landed_title). ---
on_title_destroyed = {
	on_actions = { rrm_on_title_destroyed }
}
rrm_on_title_destroyed = {
	effect = {
		rrm_stamp_destruction_effect = yes
	}
}

# --- Nomad migration. Transfers titles via the batched
#     create_title_and_vassal_change, which skips on_title_gain, so we stamp
#     here instead. Hook ONLY on_migration_travel_end: it fires for EVERY
#     migration (peaceful "Migrated" AND war "Conquered" - the migrant always
#     travels to settle the new land, proven by a war migration that fired it).
#     We deliberately do NOT also hook on_migration_war_end: a war migration
#     fires war_end (wins + transfers) AND travel_end (settles) the same day, so
#     hooking both stamped the title TWICE - a duplicate timeline slot that
#     shoved every older row's end one slot too new. travel_end alone = exactly
#     one stamp per migration. root = migrating ruler, scope:target_title =
#     target, scope:domain = the county/duchy that becomes their domain. ---
on_migration_travel_end = {
	on_actions = { rrm_on_migration }
}
rrm_on_migration = {
	effect = {
		rrm_stamp_migration_effect = yes
	}
}

# --- Fresh-game bootstrap: stamp rrm_holder_gain_date on every held title
#     that lacks it, so COA tooltips work for pre-existing holders. ---
on_game_start_after_lobby = {
	on_actions = { rrm_on_game_start }
}
rrm_on_game_start = {
	effect = {
		rrm_bootstrap_holder_gain_dates_effect = yes
	}
}

# --- Mid-game install catch-up: on_game_start_after_lobby does not fire when
#     adding the mod to an existing save, so the yearly pulse backfills. ---
on_yearly_playable = {
	on_actions = { rrm_on_yearly_playable }
}
rrm_on_yearly_playable = {
	effect = {
		rrm_bootstrap_held_titles_effect = yes
	}
}
