# ============================================================
# CHALLENGE SYSTEM EFFECTS (v1.20.8)
# These make the PLAYER's life harder without touching AI stats.
# All gated behind game rules so the player can disable them.
# ============================================================

# --- Resource Scarcity ---
# When the player is low on gold, AI raids them for resources
qyv_ai_raid_for_resources_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_raid_for_resources = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    NOT = { is_allied_to = root }
                    NOT = { has_truce_with = root }
                    military_strength < root.military_strength * 1.5
                }
                save_scope_as = resource_target
            }
            if = {
                limit = { exists = scope:resource_target }
                raid = yes
            }
        }
    }
}

# When the player is weak and struggling, AI demands tribute
qyv_ai_demand_tribute_when_weak_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_demand_tribute_when_weak = root
            }
            qyv_send_tribute = yes
        }
    }
}

# --- Vassal Volatility ---
# Great powers stir faction trouble in the player's realm
qyv_ai_stir_faction_trouble_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_stir_faction_trouble = root
            }
            any_vassal = {
                limit = {
                    primary_title.tier >= tier_duchy
                    opinion_of_liege < 30
                }
                save_scope_as = restless_vassal
            }
            if = {
                limit = { exists = scope:restless_vassal }
                scope:restless_vassal = {
                    add_character_flag = faction_inclined
                }
            }
        }
    }
}

# Great powers sow discontent in vassals who are merely lukewarm
qyv_ai_sow_discontent_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_sow_discontent = root
            }
            any_vassal = {
                limit = {
                    primary_title.tier >= tier_duchy
                    opinion_of_liege >= 30
                    opinion_of_liege < 50
                }
                save_scope_as = lukewarm_vassal
            }
            if = {
                limit = { exists = scope:lukewarm_vassal }
                scope:lukewarm_vassal = {
                    add_opinion = { target = liege modifier = qyv_sow_discontent years = 5 }
                }
            }
        }
    }
}

# --- Succession Fragility ---
# AI targets the heirs of aging rulers with weak succession
qyv_ai_target_heir_of_weak_ruler_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_target_heir_of_weak_ruler = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    age >= 50
                    has_heir = yes
                    heir = { is_child = yes }
                    military_strength < root.military_strength * 1.5
                    NOT = { is_allied_to = root }
                }
                save_scope_as = weak_ruler
            }
            if = {
                limit = { exists = scope:weak_ruler }
                scope:weak_ruler = {
                    fabricate_claim_on = scope:heir
                }
            }
        }
    }
}

# AI targets rulers facing succession crises
qyv_ai_target_succession_crisis_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_target_succession_crisis = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    qyv_ai_has_succession_crisis = root
                    military_strength < root.military_strength * 1.5
                    NOT = { is_allied_to = root }
                }
                save_scope_as = crisis_ruler
            }
            if = {
                limit = { exists = scope:crisis_ruler }
                fabricate_claim_on = scope:crisis_ruler
            }
        }
    }
}

# --- External Pressure ---
# AI creates claimant threats against weak neighbors
qyv_ai_create_claimant_threat_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_create_claimant_threat = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    is_at_war = no
                    military_strength < root.military_strength * 2.0
                    NOT = { is_allied_to = root }
                    NOT = { has_truce_with = root }
                }
                save_scope_as = claimant_target
            }
            if = {
                limit = { exists = scope:claimant_target }
                scope:claimant_target = {
                    scheme_claim = yes
                }
            }
        }
    }
}

# AI encourages rebellion in unstable neighbors
qyv_ai_encourage_rebellion_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_encourage_rebellion = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    qyv_ai_has_faction_trouble = root
                    military_strength < root.military_strength * 1.5
                    NOT = { is_allied_to = root }
                }
                save_scope_as = unstable_neighbor
            }
            if = {
                limit = { exists = scope:unstable_neighbor }
                scope:unstable_neighbor = {
                    add_character_flag = external_encouragement
                }
            }
        }
    }
}

# --- Economic Pressure ---
# Great powers squeeze the player's economy when they are wealthy
qyv_ai_squeeze_economy_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_squeeze_economy = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    NOT = { is_allied_to = root }
                    NOT = { has_truce_with = root }
                    gold >= 1000
                }
                save_scope_as = rich_neighbor
            }
            if = {
                limit = { exists = scope:rich_neighbor }
                scope:rich_neighbor = {
                    demand_tribute = yes
                }
            }
        }
    }
}

# AI blocks trade routes from coastal neighbors
qyv_ai_block_trade_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_block_trade = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    is_coastal = yes
                    NOT = { is_allied_to = root }
                    NOT = { has_truce_with = root }
                }
                save_scope_as = coastal_neighbor
            }
            if = {
                limit = { exists = scope:coastal_neighbor }
                scope:coastal_neighbor = {
                    raid = yes
                }
            }
        }
    }
}