# ============================================================
# CRUSADE/JIHAD COORDINATION TRIGGERS
# ============================================================

# Crusade Participation
qyv_ai_should_join_crusade = {
    trigger = {
        is_ai = yes
        is_ruler = yes
        is_independent_ruler = yes
        any_crusade = {
            target_faith = root.faith
            NOT = { participant = root }
            distance_to_target < 1000
        }
        piety >= 500
        gold >= 1000
        NOT = { in_debt = yes }
    }
}

qyv_ai_should_lead_crusade = {
    trigger = {
        is_ai = yes
        is_ruler = yes
        is_independent_ruler = yes
        any_crusade = {
            target_faith = root.faith
            NOT = { leader = root }
            distance_to_target < 1000
        }
        prestige >= 1000
        piety >= 1000
        military_strength >= 10000
    }
}

qyv_ai_should_contribute_crusade = {
    trigger = {
        is_ai = yes
        is_ruler = yes
        any_crusade = {
            participant = root
            war_score < 50
        }
        gold >= 500
        manpower >= 1000
    }
}

# Jihad Defense
qyv_ai_should_defend_jihad = {
    trigger = {
        is_ai = yes
        is_ruler = yes
        any_jihad = {
            target = root
            NOT = { participant = root }
        }
        military_strength >= 5000
    }
}

qyv_ai_should_join_jihad = {
    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 < 1000
        }
        piety >= 500
        gold >= 1000
        NOT = { in_debt = yes }
    }
}

# Crusade Target Selection
qyv_ai_should_target_crusade = {
    trigger = {
        is_ai = yes
        is_ruler = yes
        is_faith_head = yes
        any_faith = {
            head_of_faith = root
            can_call_crusade = yes
            any_holy_site = {
                faith = root.faith
                controller = { NOT = { faith = root.faith } }
            }
        }
    }
}

qyv_ai_should_call_crusade = {
    trigger = {
        is_ai = yes
        is_faith_head = yes
        any_holy_site = {
            faith = root.faith
            controller = { NOT = { faith = root.faith } }
        }
        any_independent_ruler = {
            limit = {
                faith != root.faith
                military_strength < root.military_strength * 2.0
            }
        }
        piety >= 2000
    }
}

# Crusade Contribution Optimization
qyv_ai_should_optimize_crusade_contribution = {
    trigger = {
        is_ai = yes
        is_ruler = yes
        any_crusade = {
            participant = root
            war_score < 70
        }
        gold >= 500
        manpower >= 2000
    }
}

# Crusade Reward Optimization
qyv_ai_should_optimize_crusade_rewards = {
    trigger = {
        is_ai = yes
        is_ruler = yes
        any_crusade = {
            participant = root
            war_score >= 80
        }
    }
}

# Jihad Preparation
qyv_ai_should_prepare_jihad = {
    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
    }
}

# Holy War vs Crusade Decision
qyv_ai_should_choose_holy_war_vs_crusade = {
    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
    }
}

# Crusade Benefit Assessment
qyv_ai_crusade_worth_it = {
    trigger = {
        is_ai = yes
        is_ruler = yes
        any_crusade = {
            target_faith = root.faith
            active = yes
            distance_to_target < 1500
            potential_reward >= 500
        }
        gold >= 1000
        manpower >= 2000
    }
}

# Crusade Leader Support
qyv_ai_should_support_crusade_leader = {
    trigger = {
        is_ai = yes
        is_ruler = yes
        any_crusade = {
            leader = {
                is_allied_to = root
                military_strength < root.military_strength
            }
        }
        gold >= 500
    }
}

# Crusade Target Prioritization
qyv_ai_crusade_target_priority = {
    trigger = {
        is_ai = yes
        is_ruler = yes
        any_crusade = {
            target_faith = root.faith
            any_holy_site = {
                faith = root.faith
                controller = { NOT = { faith = root.faith } }
            }
        }
    }
}

# Great Holy War Participation
qyv_ai_should_join_great_holy_war = {
    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
    }
}