namespace = qyv_ai_succession

# ============================================================
# QUARTERLY: HEIR EVALUATION & DESIGNATION
# ============================================================

character_event = {
    id = qyv_succ.0100
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
    }

    mean_time_to_happen = { months = 3 }

    immediate = {
        # Evaluate heir quality
        if = {
            limit = { qyv_ai_evaluate_heir = root }
            qyv_ai_evaluate_heir = yes
        }

        # Disinherit bad heir
        if = {
            limit = { qyv_has_bad_heir = root }
            qyv_ai_disinherit_bad_heir = yes
        }

        # Designate better heir
        if = {
            limit = { qyv_has_better_spare = root }
            qyv_ai_designate_better_heir = yes
        }

        # Prepare backup heirs
        if = {
            limit = {
                any_child = {
                    is_heir = no
                    age >= 6
                    NOT = { has_education_focus = yes }
                }
            }
            qyv_ai_prepare_backup_heirs = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# QUARTERLY: SUCCESSION LAW OPTIMIZATION
# ============================================================

character_event = {
    id = qyv_succ.0200
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        is_independent_ruler = yes
        NOT = { has_government = theocracy }
    }

    mean_time_to_happen = { months = 3 }

    immediate = {
        # Optimize succession law
        if = {
            limit = { qyv_ai_optimize_succession_law = root }
            qyv_ai_optimize_succession_law = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# QUARTERLY: EDUCATION FOCUS OPTIMIZATION
# ============================================================

character_event = {
    id = qyv_succ.0300
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
    }

    mean_time_to_happen = { months = 3 }

    immediate = {
        # Optimize education focus for all children
        if = {
            limit = { qyv_ai_optimize_education_focus = root }
            qyv_ai_optimize_education_focus = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# YEARLY: PARTITION MANAGEMENT & HEIR GROOMING
# ============================================================

character_event = {
    id = qyv_succ.0400
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        age >= 40
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        # Manage partition - concentrate land on heir
        if = {
            limit = { qyv_ai_manage_partition = root }
            qyv_ai_manage_partition = yes
        }

        # Groom heir - education, marriage
        if = {
            limit = { any_child = { is_heir = yes } }
            qyv_ai_groom_heir = yes
        }

        # Legitimize talented bastards
        if = {
            limit = {
                any_child = {
                    is_bastard = yes
                    OR = {
                        has_trait = intelligent
                        has_trait = quick
                        has_trait = genius
                        has_trait = attractive
                        has_trait = handsome
                        has_trait = beautiful
                        has_trait = hale
                        has_trait = robust
                        has_trait = fecund
                    }
                }
            }
            qyv_ai_legitimize_bastards = yes
        }

        # Create cadet branch if eligible
        if = {
            limit = {
                is_ruler = yes
                is_dynast = yes
                NOT = { is_house_head = yes }
                prestige >= 2000
            }
            qyv_ai_create_cadet_branch = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# YEARLY: ELECTIVE SUCCESSION MANAGEMENT
# ============================================================

character_event = {
    id = qyv_succ.0500
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        OR = {
            has_law = feudal_elective_succession
            liege = { has_law = feudal_elective_succession }
        }
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        # Vote for dynasty member in own election
        if = {
            limit = { has_law = feudal_elective_succession }
            qyv_ai_vote_elective = yes
        }

        # Vote for dynasty member in liege's election
        if = {
            limit = { liege = { has_law = feudal_elective_succession } }
            qyv_ai_manage_elective_votes = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# ON_ACTIONS
# ============================================================

on_actions = {
    on_quarterly_pulse = { events = { qyv_succ.0100 qyv_succ.0200 qyv_succ.0300 } }
    on_yearly_pulse = { events = { qyv_succ.0400 qyv_succ.0500 } }
}