# ============================================================
# HEIR & SUCCESSION OPTIMIZATION EFFECTS
# ============================================================

# Heir Evaluation & Designation
qyv_ai_evaluate_heir = {
    if = {
        limit = { qyv_has_bad_heir = root }
        set_character_flag = qyv_heir_crisis
    }
}

qyv_ai_disinherit_bad_heir = {
    if = {
        limit = {
            any_child = {
                is_heir = yes
                OR = { has_trait = stupid has_trait = imbecile has_trait = incapable }
            }
            has_crown_law = { law = partition }
            OR = { has_law = partition has_law = confederate_partition }
        }
        any_child = {
            limit = { is_heir = yes }
            disinherit = yes
        }
    }
}

qyv_ai_designate_better_heir = {
    if = {
        limit = { qyv_has_better_spare = root }
        set_character_flag = qyv_switching_heir
        any_child = {
            limit = {
                is_heir = no
                age >= 8
                NOT = { is_married = 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 = giant
                    has_trait = fecund
                }
                NOT = { has_trait = slow }
                NOT = { has_trait = stupid }
                NOT = { has_trait = weak }
                NOT = { has_trait = sickly }
            }
            designate_heir = yes
        }
    }
}

qyv_ai_prepare_backup_heirs = {
    any_child = {
        limit = {
            is_heir = no
            age >= 6
            NOT = { has_education_focus = yes }
        }
        if = {
            limit = { martial >= 12 }
            set_education_focus = martial_education
        }
        else_if = {
            limit = { stewardship >= 12 }
            set_education_focus = stewardship_education
        }
        else_if = {
            limit = { diplomacy >= 12 }
            set_education_focus = diplomacy_education
        }
        else_if = {
            limit = { intrigue >= 12 }
            set_education_focus = intrigue_education
        }
        else_if = {
            limit = { learning >= 12 }
            set_education_focus = learning_education
        }
    }
}

# Succession Law Optimization
qyv_ai_optimize_succession_law = {
    if = {
        limit = { qyv_should_switch_succession = root }
        if = {
            limit = { qyv_ai_wants_primogeniture = root }
            set_crown_law = primogeniture_succession
        }
        else_if = {
            limit = { qyv_ai_wants_ultimogeniture = root }
            set_crown_law = ultimogeniture_succession
        }
        else_if = {
            limit = { qyv_ai_wants_house_seniority = root }
            set_crown_law = house_seniority
        }
        else_if = {
            limit = { qyv_ai_wants_elective = root }
            set_crown_law = feudal_elective_succession
        }
    }
}

# Education Focus Optimization
qyv_ai_optimize_education_focus = {
    any_realm_courtier = {
        limit = {
            is_child = yes
            NOT = { has_education_focus = yes }
            age >= 6
        }
        if = {
            limit = { qyv_ai_education_focus_martial = scope:ward }
            set_education_focus = martial_education
        }
        else_if = {
            limit = { qyv_ai_education_focus_stewardship = scope:ward }
            set_education_focus = stewardship_education
        }
        else_if = {
            limit = { qyv_ai_education_focus_diplomacy = scope:ward }
            set_education_focus = diplomacy_education
        }
        else_if = {
            limit = { qyv_ai_education_focus_intrigue = scope:ward }
            set_education_focus = intrigue_education
        }
        else_if = {
            limit = { qyv_ai_education_focus_learning = scope:ward }
            set_education_focus = learning_education
        }
    }
}

# Partition Management (concentrate land on primary heir)
qyv_ai_manage_partition = {
    if = {
        limit = {
            has_law = partition
            OR = { has_law = confederate_partition }
            age >= 50
            any_child = { is_heir = yes }
        }
        # Grant counties to primary heir
        any_owned_province = {
            limit = {
                holding = { is_vassal = no }
                NOT = { holder = { is_heir = yes } }
            }
            if = {
                limit = { holder = { is_child = yes is_heir = no } }
                grant_title = { target = scope:heir title = scope:province.title }
            }
        }
    }
}

# Groom Heir (marriage, traits, education)
qyv_ai_groom_heir = {
    if = {
        limit = {
            any_child = { is_heir = yes }
        }
        scope:heir = {
            # Best education focus
            if = {
                limit = { NOT = { has_education_focus = yes } age >= 6 }
                if = { limit = { martial >= 12 } set_education_focus = martial_education }
                else_if = { limit = { stewardship >= 12 } set_education_focus = stewardship_education }
                else_if = { limit = { diplomacy >= 12 } set_education_focus = diplomacy_education }
                else_if = { limit = { intrigue >= 12 } set_education_focus = intrigue_education }
                else_if = { limit = { learning >= 12 } set_education_focus = learning_education }
            }
            # Find good spouse for genetics
            if = {
                limit = {
                    is_adult = yes
                    NOT = { is_married = yes }
                    NOT = { is_betrothed = yes }
                }
                any_character = {
                    limit = {
                        is_adult = yes
                        is_married = no
                        NOT = { is_close_family_of = scope:heir }
                        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
                        }
                        NOT = { has_trait = slow }
                        NOT = { has_trait = stupid }
                        NOT = { has_trait = weak }
                        NOT = { has_trait = sickly }
                    }
                    qyv_send_marriage_offer = { target = scope:heir.parent third = scope:candidate }
                }
            }
        }
    }
}

# Legitimize Talented Bastards
qyv_ai_legitimize_bastards = {
    any_child = {
        limit = {
            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
            }
            NOT = { has_trait = stupid }
            NOT = { has_trait = weak }
            NOT = { has_trait = sickly }
        }
        legitimize_bastard = yes
    }
}

# Create Cadet Branch for Dynasty Management
qyv_ai_create_cadet_branch = {
    if = {
        limit = {
            is_ruler = yes
            is_dynast = yes
            NOT = { is_house_head = yes }
            prestige >= 2000
        }
        create_cadet_branch = {
            name = "[dynasty.GetName] of [capital_province.GetName]"
            motto = "Fortune favors the bold"
        }
    }
}

# Elective Succession Voting
qyv_ai_vote_elective = {
    if = {
        limit = {
            has_law = feudal_elective_succession
            any_candidate = {
                is_dynasty_member_of = root
                age >= 16
                NOT = { has_trait = stupid }
                NOT = { has_trait = incapable }
            }
        }
        vote_for = scope:candidate
    }
}

# Manage Elective Succession as Voter
qyv_ai_manage_elective_votes = {
    if = {
        limit = {
            liege = { has_law = feudal_elective_succession }
        }
        if = {
            limit = {
                any_candidate = {
                    is_dynasty_member_of = root
                    age >= 16
                    NOT = { has_trait = stupid }
                    NOT = { has_trait = incapable }
                }
            }
            vote_for = scope:candidate
        }
        else_if = {
            limit = {
                any_candidate = {
                    is_friend_of = root
                    age >= 16
                }
            }
            vote_for = scope:candidate
        }
    }
}