# ============================================================
# CRUSADE/JIHAD COORDINATION EFFECTS
# ============================================================

# Crusade Participation
qyv_ai_join_crusade = {
    any_crusade = {
        limit = {
            target_faith = root.faith
            NOT = { participant = root }
            distance_to_target < 1000
        }
        join_crusade = yes
    }
    root = {
        add_piety = -500
        add_gold = -1000
        raise_all_levies = yes
        raise_all_men_at_arms = yes
    }
}

qyv_ai_lead_crusade = {
    any_crusade = {
        limit = {
            target_faith = root.faith
            NOT = { leader = root }
            distance_to_target < 1000
        }
        become_crusade_leader = yes
    }
    root = {
        add_prestige = -1000
        add_piety = -1000
        raise_all_levies = yes
        raise_all_men_at_arms = yes
    }
}

qyv_ai_contribute_crusade = {
    any_crusade = {
        limit = {
            participant = root
            war_score < 50
        }
        contribute_to_crusade = { gold = 500 manpower = 1000 }
    }
    root = {
        add_gold = -500
        raise_levies = { amount = 1000 }
        raise_men_at_arms = { amount = 5 }
    }
}

# Jihad Defense
qyv_ai_defend_jihad = {
    any_jihad = {
        limit = {
            target = root
            NOT = { participant = root }
        }
        join_jihad_defense = yes
    }
    root = {
        raise_all_levies = yes
        raise_all_men_at_arms = yes
        add_dread = 10
    }
}

qyv_ai_join_jihad = {
    any_great_holy_war = {
        limit = {
            target_faith = root.faith
            NOT = { participant = root }
            distance_to_target < 1000
        }
        join_great_holy_war = yes
    }
    root = {
        add_piety = -500
        add_gold = -1000
        raise_all_levies = yes
        raise_all_men_at_arms = yes
    }
}

# Crusade Calling
qyv_ai_call_crusade = {
    any_holy_site = {
        limit = {
            faith = root.faith
            controller = { NOT = { faith = root.faith } }
        }
        call_crusade = { target_faith = root.faith target_province = this }
    }
    root = {
        add_piety = -2000
        add_prestige = -1000
    }
}

qyv_ai_optimize_crusade_contribution = {
    any_crusade = {
        limit = {
            participant = root
            war_score < 70
        }
        contribute_to_crusade = { gold = 500 manpower = 2000 }
    }
    root = {
        add_gold = -500
        raise_levies = { amount = 2000 }
        raise_men_at_arms = { amount = 5 }
    }
}

qyv_ai_optimize_crusade_rewards = {
    any_crusade = {
        limit = {
            participant = root
            war_score >= 80
        }
        claim_crusade_rewards = yes
    }
    root = {
        add_prestige = 500
        add_piety = 500
    }
}

# Jihad Preparation
qyv_ai_prepare_jihad = {
    if = {
        limit = { qyv_ai_should_prepare_jihad = root }
        any_great_holy_war = {
            limit = {
                target_faith = root.faith
                NOT = { participant = root }
                preparation_phase = yes
            }
            join_great_holy_war = yes
        }
        root = {
            add_piety = -500
            add_gold = -1000
            raise_all_levies = yes
            raise_all_men_at_arms = yes
        }
    }
}

# Holy War vs Crusade Decision
qyv_ai_choose_holy_war_vs_crusade = {
    if = {
        limit = {
            any_crusade = {
                target_faith = root.faith
                active = no
            }
            any_holy_site = {
                faith = root.faith
                controller = { NOT = { faith = root.faith } }
            }
        }
        any_holy_site = {
            limit = {
                faith = root.faith
                controller = { NOT = { faith = root.faith } }
            }
            declare_war = { target = scope:controller cb = holy_war }
        }
        else = {
            any_crusade = {
                limit = {
                    target_faith = root.faith
                    active = no
                }
            }
            if = {
                limit = { piety >= 2000 }
                call_crusade = { target_faith = root.faith }
            }
        }
    }
}

# Crusade Worth Assessment
qyv_ai_assess_crusade_worth = {
    if = {
        limit = { qyv_ai_crusade_worth_it = root }
        any_crusade = {
            limit = {
                target_faith = root.faith
                active = yes
                distance_to_target < 1500
            }
            join_crusade = yes
        }
        root = {
            add_piety = -500
            add_gold = -1000
            raise_all_levies = yes
        }
    }
}

qyv_ai_support_crusade_leader = {
    any_crusade = {
        limit = {
            leader = {
                is_allied_to = root
                military_strength < root.military_strength
            }
        }
        send_gold = { target = scope:leader amount = 500 }
        call_ally = { target = scope:leader }
    }
    root = {
        add_gold = -500
    }
}

qyv_ai_prioritize_crusade_target = {
    if = {
        limit = { qyv_ai_crusade_target_priority = root }
        any_holy_site = {
            limit = {
                faith = root.faith
                controller = { NOT = { faith = root.faith } }
            }
            save_scope_as = priority_target
        }
    }
    if = {
        limit = { exists = scope:priority_target }
        declare_war = { target = scope:priority_target cb = holy_war }
    }
}

qyv_ai_join_great_holy_war = {
    any_great_holy_war = {
        limit = {
            target_faith = root.faith
            NOT = { participant = root }
            distance_to_target < 1500
        }
        join_great_holy_war = yes
    }
    root = {
        add_piety = -1000
        add_gold = -2000
        raise_all_levies = yes
        raise_all_men_at_arms = yes
    }
}

# Crusade Reward Claiming
qyv_ai_claim_crusade_rewards = {
    any_crusade = {
        limit = {
            participant = root
            war_score >= 80
        }
        claim_rewards = yes
    }
    root = {
        add_prestige = 1000
        add_piety = 1000
        add_gold = 500
    }
}