# ============================================================
# GOVERNMENT SPECIALIZATION EFFECTS
# ============================================================

# Feudal Effects
qyv_ai_feudal_optimize_vassals = {
    any_vassal = {
        limit = { opinion_of_liege < -25 }
        if = {
            limit = { is_powerful = yes }
            enforce_hook = {
                target = root
                demand_council_position = yes
            }
        }
        else = {
            add_opinion = { target = root modifier = feudal_opinion_boost years = 5 }
        }
    }
}

qyv_ai_feudal_manage_contracts = {
    any_vassal = {
        limit = {
            contract_has = taxes_normal
            contract_has = levies_normal
            opinion_of_self >= 30
        }
        set_vassal_contract = { taxes = high levies = normal }
    }
}

qyv_ai_feudal_create_duchies = {
    if = {
        limit = { realm_size >= 30 }
        create_title = { title = duchy_[capital_province.GetName] }
        add_prestige = -500
    }
}

# Clan Effects
qyv_ai_clan_manage_sentiment = {
    any_clan_member = {
        limit = { opinion_of_self < -25 }
        if = {
            limit = { is_powerful = yes }
            enforce_hook = {
                target = root
                demand_gold = 200
            }
        }
        else = {
            add_opinion = { target = root modifier = clan_sentiment_boost years = 5 }
        }
    }
}

qyv_ai_clan_grant_lands = {
    any_clan_member = {
        limit = { is_landed = no opinion_of_self >= 0 }
        grant_title = { title = county_[capital_province.GetName] }
        add_opinion = { target = root modifier = granted_land years = 10 }
    }
}

qyv_ai_clan_convert_tribal = {
    any_owned_province = {
        limit = { is_tribal = yes }
        convert_to_castle = yes
    }
    add_prestige = -500
}

# Republic Effects
qyv_ai_republic_manage_trade = {
    any_owned_province = {
        limit = { is_coastal = yes NOT = { has_trade_post = yes } }
        build_trade_post = yes
        add_gold = -300
    }
}

qyv_ai_republic_manage_patricians = {
    any_patrician_family = {
        limit = { respect < 500 }
        if = {
            limit = { gold >= 1000 }
            add_respect = 200
            add_gold = -1000
        }
    }
}

qyv_ai_republic_expand_trade_posts = {
    any_owned_province = {
        limit = { is_coastal = yes NOT = { has_trade_post = yes } }
        build_trade_post = yes
        add_gold = -300
    }
}

# Theocracy Effects
qyv_ai_theocracy_manage_faith = {
    if = {
        limit = { faith = { can_create_head_of_faith = yes } }
        create_head_of_faith = {
            faith = root.faith
            title = "head_of_faith_[faith.GetName]"
        }
        add_piety = -1000
    }
}

qyv_ai_theocracy_convert = {
    any_owned_province = {
        limit = { faith != root.faith }
        convert_faith = { faith = root.faith }
    }
}

qyv_ai_theocracy_use_faith_head = {
    if = {
        limit = { is_faith_head = yes }
        any_character = {
            limit = {
                is_ruler = yes
                faith = root.faith
                opinion_of_self >= 50
            }
            add_character_flag = endorsed_by_faith
            add_opinion = { target = root modifier = faith_endorsement years = 10 }
        }
    }
}

# Tribal Effects
qyv_ai_tribal_raid = {
    if = {
        limit = { any_owned_province = { is_coastal = yes } }
        any_neighbor_independent_ruler = {
            limit = { is_coastal = yes military_strength < root.military_strength * 1.5 }
            start_raiding = yes
        }
    }
}

qyv_ai_tribal_convert = {
    if = {
        limit = { prestige >= 1000 realm_size >= 15 }
        change_government = clan_government
        add_prestige = -1000
    }
}

qyv_ai_tribal_manage_clans = {
    any_clan_member = {
        limit = { opinion_of_self < -25 }
        if = {
            limit = { is_powerful = yes }
            enforce_hook = {
                target = root
                demand_gold = 200
            }
        }
    }
}

# Nomadic Effects
qyv_ai_nomadic_manage_horde = {
    if = {
        limit = { horde_unity < 50 }
        add_horde_unity = 20
        add_prestige = -200
    }
}

qyv_ai_nomadic_manage_grazing = {
    any_owned_province = {
        limit = { is_nomadic_capital = yes }
        add_development = 1
        add_horde_unity = 5
    }
}

qyv_ai_nomadic_manage_clans = {
    any_clan = {
        limit = { sentiment < 0 }
        if = {
            limit = { is_powerful = yes }
            enforce_hook = {
                target = root
                demand_gold = 300
            }
        }
        else = {
            add_clan_sentiment = 20
        }
    }
}

# Government-Specific War Effects
qyv_ai_feudal_war_logic = {
    if = {
        limit = { qyv_strat_expansion = root }
        qyv_ai_choose_cb = yes
    }
    if = {
        limit = { qyv_strat_consolidation = root }
        if = { limit = { qyv_ai_prefers_consolidation = root } qyv_ai_prefers_consolidation = yes }
    }
}

qyv_ai_clan_war_logic = {
    if = {
        limit = { qyv_strat_expansion = root }
        if = { limit = { gold >= 500 } qyv_ai_hire_mercs = yes }
        qyv_ai_choose_cb = yes
    }
}

qyv_ai_republic_war_logic = {
    if = {
        limit = { qyv_strat_expansion = root }
        if = { limit = { gold >= 1000 } qyv_ai_hire_mercs = yes }
        qyv_ai_choose_cb = yes
    }
}

qyv_ai_tribal_war_logic = {
    if = {
        limit = { qyv_strat_expansion = root }
        any_neighbor_independent_ruler = {
            limit = { military_strength < root.military_strength * 1.5 }
            start_raiding = yes
        }
    }
}