namespace = qyv_ai_crusade

# ============================================================
# YEARLY: CRUSADE PARTICIPATION & CALLING
# ============================================================

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

    trigger = {
        is_ai = yes
        is_ruler = yes
        is_independent_ruler = yes
        piety >= 500
        gold >= 1000
        NOT = { in_debt = yes }
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        # Join existing crusade
        if = {
            limit = { qyv_ai_should_join_crusade = root }
            qyv_ai_join_crusade = yes
        }

        # Lead crusade if qualified
        if = {
            limit = { qyv_ai_should_lead_crusade = root }
            qyv_ai_lead_crusade = yes
        }

        # Contribute to struggling crusade
        if = {
            limit = { qyv_ai_should_contribute_crusade = root }
            qyv_ai_contribute_crusade = yes
        }
    }

    option = { name = "OK" }
}

# Yearly: Faith Head Crusade Calling
character_event = {
    id = qyv_crusade.0200
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_faith_head = yes
        piety >= 2000
    }

    mean_time_to_happen = { months = 24 }

    immediate = {
        if = { limit = { qyv_ai_should_call_crusade = root } qyv_ai_call_crusade = yes }
    }

    option = { name = "OK" }
}

# Yearly: Crusade Contribution Optimization
character_event = {
    id = qyv_crusade.0300
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        any_crusade = {
            participant = root
            war_score < 70
        }
        gold >= 500
        manpower >= 2000
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        if = { limit = { qyv_ai_should_optimize_crusade_contribution = root } qyv_ai_optimize_crusade_contribution = yes }
    }

    option = { name = "OK" }
}

# Yearly: Crusade Reward Claiming
character_event = {
    id = qyv_crusade.0400
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        any_crusade = {
            participant = root
            war_score >= 80
        }
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        if = { limit = { qyv_ai_should_optimize_crusade_rewards = root } qyv_ai_claim_crusade_rewards = yes }
    }

    option = { name = "OK" }
}

# Yearly: Jihad Participation
character_event = {
    id = qyv_crusade.0500
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        is_independent_ruler = yes
        piety >= 500
        gold >= 1000
        NOT = { in_debt = yes }
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        # Join jihad defense
        if = {
            limit = { qyv_ai_should_defend_jihad = root }
            qyv_ai_defend_jihad = yes
        }

        # Join great holy war
        if = {
            limit = { qyv_ai_should_join_jihad = root }
            qyv_ai_join_jihad = yes
        }

        # Join great holy war (high commitment)
        if = {
            limit = { qyv_ai_should_join_great_holy_war = root }
            qyv_ai_join_great_holy_war = yes
        }
    }

    option = { name = "OK" }
}

# Monthly: Crusade Contribution During War
character_event = {
    id = qyv_crusade.0600
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        any_crusade = {
            participant = root
            war_score < 70
        }
        gold >= 500
        manpower >= 2000
    }

    mean_time_to_happen = { months = 1 }

    immediate = {
        if = { limit = { qyv_ai_should_optimize_crusade_contribution = root } qyv_ai_optimize_crusade_contribution = yes }
    }

    option = { name = "OK" }
}

# Monthly: Jihad Preparation
character_event = {
    id = qyv_crusade.0700
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        is_independent_ruler = yes
        any_great_holy_war = {
            target_faith = root.faith
            NOT = { participant = root }
            preparation_phase = yes
        }
        piety >= 500
        gold >= 1000
    }

    mean_time_to_happen = { months = 1 }

    immediate = {
        if = { limit = { qyv_ai_should_prepare_jihad = root } qyv_ai_prepare_jihad = yes }
    }

    option = { name = "OK" }
}

# Yearly: Faith Head Crusade Calling
character_event = {
    id = qyv_crusade.0800
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_faith_head = yes
        piety >= 2000
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        if = { limit = { qyv_ai_should_call_crusade = root } qyv_ai_call_crusade = yes }
    }

    option = { name = "OK" }
}

# Yearly: Crusade Target Prioritization
character_event = {
    id = qyv_crusade.0900
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        is_faith_head = yes
        any_holy_site = {
            faith = root.faith
            controller = { NOT = { faith = root.faith } }
        }
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        if = { limit = { qyv_ai_crusade_target_priority = root } qyv_ai_prioritize_crusade_target = yes }
    }

    option = { name = "OK" }
}

# Yearly: Crusade Worth Assessment
character_event = {
    id = qyv_crusade.1000
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        piety >= 500
        gold >= 1000
        manpower >= 2000
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        if = { limit = { qyv_ai_crusade_worth_it = root } qyv_ai_assess_crusade_worth = yes }
    }

    option = { name = "OK" }
}

# Yearly: Crusade Leader Support
character_event = {
    id = qyv_crusade.1100
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        any_crusade = {
            leader = {
                is_allied_to = root
                military_strength < root.military_strength
            }
        }
        gold >= 500
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        if = { limit = { qyv_ai_should_support_crusade_leader = root } qyv_ai_support_crusade_leader = yes }
    }

    option = { name = "OK" }
}

# Yearly: Holy War vs Crusade Decision
character_event = {
    id = qyv_crusade.1200
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        is_independent_ruler = yes
        NOT = { is_at_war = yes }
        any_holy_site = {
            faith = root.faith
            controller = { NOT = { faith = root.faith } }
        }
        any_crusade = {
            target_faith = root.faith
            active = no
        }
        piety >= 500
        gold >= 1000
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        if = { limit = { qyv_ai_should_choose_holy_war_vs_crusade = root } qyv_ai_choose_holy_war_vs_crusade = yes }
    }

    option = { name = "OK" }
}

# Monthly: Crusade Support
character_event = {
    id = qyv_crusade.1300
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        any_crusade = {
            leader = {
                is_allied_to = root
                military_strength < root.military_strength
            }
        }
        gold >= 500
    }

    mean_time_to_happen = { months = 1 }

    immediate = {
        if = { limit = { qyv_ai_should_support_crusade_leader = root } qyv_ai_support_crusade_leader = yes }
    }

    option = { name = "OK" }
}

# Yearly: Jihad Preparation
character_event = {
    id = qyv_crusade.1400
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        is_independent_ruler = yes
        any_great_holy_war = {
            target_faith = root.faith
            NOT = { participant = root }
            preparation_phase = yes
        }
        piety >= 500
        gold >= 1000
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        if = { limit = { qyv_ai_should_prepare_jihad = root } qyv_ai_prepare_jihad = yes }
    }

    option = { name = "OK" }
}

# Yearly: Great Holy War Participation
character_event = {
    id = qyv_crusade.1500
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        is_independent_ruler = yes
        any_great_holy_war = {
            target_faith = root.faith
            NOT = { participant = root }
            distance_to_target < 1500
        }
        piety >= 1000
        gold >= 2000
        military_strength >= 15000
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        if = { limit = { qyv_ai_should_join_great_holy_war = root } qyv_ai_join_great_holy_war = yes }
    }

    option = { name = "OK" }
}

# Monthly: Crusade Reward Claiming
character_event = {
    id = qyv_crusade.1600
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        any_crusade = {
            participant = root
            war_score >= 80
        }
    }

    mean_time_to_happen = { months = 1 }

    immediate = {
        if = { limit = { qyv_ai_should_optimize_crusade_rewards = root } qyv_ai_claim_crusade_rewards = yes }
    }

    option = { name = "OK" }
}

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

on_actions = {
    on_yearly_pulse = { events = { qyv_crusade.0100 qyv_crusade.0200 qyv_crusade.0300 qyv_crusade.0400 qyv_crusade.0500 qyv_crusade.0800 qyv_crusade.0900 qyv_crusade.1000 qyv_crusade.1100 qyv_crusade.1200 qyv_crusade.1400 qyv_crusade.1500 } }
    on_monthly_pulse = { events = { qyv_crusade.0600 qyv_crusade.0700 qyv_crusade.1300 qyv_crusade.1600 } }
}