namespace = qyv_playerlike_ai

# ============================================================
# INTRIGUE & SCHEMES (Monthly pulse)
# ============================================================

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

    trigger = {
        is_ai = yes
        is_ruler = yes
        qyv_ai_strategic_adaptive = root
        OR = {
            qyv_ai_strategic_calculating = root
        }
    }

    mean_time_to_happen = { months = 1 }

    immediate = {
        # Murder scheme
        if = {
            limit = { qyv_ai_can_scheme_murder = root }
            qyv_playerlike_scheme_murder = yes
        }

        # Abduct heir
        if = {
            limit = { qyv_ai_can_scheme_abduct = root }
            qyv_playerlike_scheme_abduct = yes
        }

        # Sway key vassal
        if = {
            limit = { qyv_ai_can_sway = root }
            qyv_playerlike_sway = yes
        }

        # Fabricate hook
        if = {
            limit = { qyv_ai_should_fabricate_hook = root }
            qyv_playerlike_fabricate_hook = yes
        }

        # Use existing hook
        if = {
            limit = { qyv_ai_should_use_hook = root }
            qyv_playerlike_use_hook = yes
        }

        # Blackmail
        if = {
            limit = { qyv_ai_can_blackmail = root }
            qyv_playerlike_blackmail = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# COUNCIL MANAGEMENT (Quarterly)
# ============================================================

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

    trigger = {
        is_ai = yes
        is_ruler = yes
    }

    mean_time_to_happen = { months = 3 }

    immediate = {
        # Fill empty council positions
        if = {
            limit = { qyv_ai_should_appoint_council = root }
            any_council_position = {
                limit = { holder = { NOT = { exists = yes } } }
                any_realm_courtier = {
                    limit = {
                        attribute >= 14
                        attribute = position.attribute
                        NOT = { is_on_council = yes }
                    }
                    add_to_council = { position = position.name }
                }
            }
        }

        # Replace incompetent councilors
        if = {
            limit = { qyv_ai_should_replace_councilor = root }
            any_council_position = {
                limit = {
                    holder = {
                        exists = yes
                        OR = {
                            attribute < 12
                            is_rival_of = root
                            opinion_of_self < -25
                        }
                    }
                }
                holder = { remove_from_council = yes }
                any_realm_courtier = {
                    limit = {
                        attribute >= 14
                        attribute = position.attribute
                        NOT = { is_on_council = yes }
                        NOT = { is_rival_of = root }
                    }
                    add_to_council = { position = position.name }
                }
            }
        }

        # Put powerful vassals on council
        if = {
            limit = { qyv_ai_council_powerful_vassals = root }
            any_council_position = {
                limit = { holder = { NOT = { exists = yes } } }
                scope:council_candidate = { add_to_council = { position = position.name } }
            }
        }
    }

    option = { name = "OK" }
}

# ============================================================
# VASSAL CONTRACT OPTIMIZATION (Yearly)
# ============================================================

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

    trigger = {
        is_ai = yes
        is_ruler = yes
        qyv_ai_strategic_adaptive = root
        OR = { qyv_ai_strategic_calculating = root }
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        # Renegotiate bad contracts
        if = {
            limit = { qyv_ai_should_renegotiate_contract = root }
            qyv_playerlike_renegotiate_contract = yes
        }

        # Offer march to powerful loyal vassals
        if = {
            limit = { qyv_ai_should_offer_special_contract = root }
            qyv_playerlike_offer_special_contract = yes
        }

        # Revoke from disloyal powerful vassals
        if = {
            limit = { qyv_ai_should_revoke_privileges = root }
            qyv_playerlike_revoke_privileges = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# MERCENARY & MILITARY TACTICS (Monthly during war)
# ============================================================

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

    trigger = {
        is_ai = yes
        is_ruler = yes
        is_at_war = yes
    }

    mean_time_to_happen = { months = 1 }

    immediate = {
        # Hire mercs when losing
        if = {
            limit = { qyv_ai_should_hire_mercs = root }
            qyv_playerlike_hire_mercs = yes
        }

        # Disband mercs when broke at peace
        if = {
            limit = { qyv_plr_ai_should_disband_mercs = root }
            qyv_playerlike_disband_mercs = yes
        }

        # Raise all levies
        if = {
            limit = { qyv_ai_should_raise_levies_smart = root }
            qyv_playerlike_raise_levies = yes
        }

        # Add siege equipment
        if = {
            limit = { qyv_plr_ai_should_use_siege_equipment = root }
            qyv_playerlike_siege_equipment = yes
        }

        # Retreat from losing battles
        if = {
            limit = { qyv_plr_ai_should_retreat = root }
            qyv_playerlike_retreat = yes
        }

        # Call allies when losing defensive war
        if = {
            limit = { qyv_plr_ai_should_call_allies = root }
            qyv_playerlike_call_allies = yes
        }

        # White peace when losing offensive
        if = {
            limit = { qyv_plr_ai_should_white_peace = root }
            qyv_playerlike_white_peace = yes
        }

        # Surrender when hopeless defensive
        if = {
            limit = { qyv_ai_should_sue_for_peace = root }
            qyv_playerlike_sue_for_peace = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# CHARACTER FOCUS & LIFESTYLE (Yearly)
# ============================================================

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

    trigger = {
        is_ai = yes
        is_ruler = yes
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        # Pick focus
        if = {
            limit = { qyv_ai_should_pick_focus = root }
            qyv_playerlike_pick_focus = yes
        }

        # Pick lifestyle
        if = {
            limit = { qyv_ai_should_pick_lifestyle = root }
            qyv_playerlike_pick_lifestyle = yes
        }

        # Pick perk
        if = {
            limit = { qyv_ai_should_pick_perk = root }
            qyv_playerlike_pick_perk = yes
        }

        # Manage stress
        if = {
            limit = { qyv_ai_should_manage_stress = root }
            qyv_playerlike_manage_stress = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# WAR STRATEGY - Choose CB (Yearly)
# ============================================================

character_event = {
    id = qyv_playerlike.0600
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        is_independent_ruler = yes
        NOT = { is_at_war = yes }
        qyv_ai_strategic_adaptive = root
        OR = { qyv_ai_strategic_calculating = root }
        gold >= 300
        prestige >= 100
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        if = {
            limit = {
                OR = {
                    qyv_strat_expansion = root
                    qyv_strat_diplomacy = root
                }
            }
            qyv_playerlike_choose_cb = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# DYNASTY & HOUSE MANAGEMENT (Yearly)
# ============================================================

character_event = {
    id = qyv_playerlike.0700
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        # Legitimize talented bastards
        if = {
            limit = { qyv_ai_should_legitimize = root }
            qyv_playerlike_legitimize = yes
        }

        # Create cadet branch
        if = {
            limit = { qyv_ai_should_create_cadet_branch = root }
            qyv_playerlike_create_cadet_branch = yes
        }

        # Use house head authority
        if = {
            limit = { qyv_ai_should_use_house_head = root }
            qyv_playerlike_use_house_head = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# RELIGIOUS & CULTURAL (Yearly)
# ============================================================

character_event = {
    id = qyv_playerlike.0800
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
    }

    mean_time_to_happen = { months = 24 }

    immediate = {
        # Convert to neighbor's faith for protection
        if = {
            limit = { qyv_plr_ai_should_convert_faith = root }
            qyv_playerlike_convert_faith = yes
        }

        # Reform faith
        if = {
            limit = { qyv_plr_ai_should_reform_faith = root }
            qyv_playerlike_reform_faith = yes
        }

        # Convert culture to high-dev province
        if = {
            limit = { qyv_plr_ai_should_convert_culture = root }
            qyv_playerlike_convert_culture = yes
        }

        # Promote culture in low-dev provinces
        if = {
            limit = { qyv_plr_ai_should_promote_culture = root }
            qyv_playerlike_promote_culture = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# ECONOMIC OPTIMIZATION (Quarterly)
# ============================================================

character_event = {
    id = qyv_playerlike.0900
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
    }

    mean_time_to_happen = { months = 3 }

    immediate = {
        # Take loans when needed
        if = {
            limit = { qyv_ai_should_loan_money = root }
            qyv_playerlike_loan_money = yes
        }

        # Repay loans
        if = {
            limit = { qyv_ai_should_repay_loans = root }
            qyv_playerlike_repay_loans = yes
        }

        # Invest in buildings
        if = {
            limit = { qyv_ai_should_invest_in_buildings = root }
            qyv_playerlike_invest_in_buildings = yes
        }

        # Optimize tax/levy contracts
        if = {
            limit = { qyv_ai_should_optimize_tax_levy = root }
            qyv_playerlike_optimize_tax_levy = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# ON_ACTIONS HOOKS
# ============================================================

on_actions = {
    # Schemes run monthly
    on_monthly_pulse = { events = { qyv_playerlike.0100 } }

    # Council quarterly
    on_quarterly_pulse = { events = { qyv_playerlike.0200 qyv_playerlike.0900 } }

    # Contracts, lifestyle, dynasty, religion yearly
    on_yearly_pulse = { events = { qyv_playerlike.0300 qyv_playerlike.0500 qyv_playerlike.0600 qyv_playerlike.0700 qyv_playerlike.0800 } }

    # Military tactics monthly during war
    on_monthly_pulse = { events = { qyv_playerlike.0400 } }

    # War declaration CB choice
    on_ai_consider_declare_war = {
        trigger_if = {
            limit = {
                is_ai = yes
                is_ruler = yes
                is_independent_ruler = yes
                NOT = { is_at_war = yes }
            }
            qyv_playerlike_choose_cb = yes
            ai_will_do = { base = 80 }
        }
    }
}