namespace = qyv_ai_government

# ============================================================
# YEARLY: GOVERNMENT TYPE DETECTION & SPECIALIZATION
# ============================================================

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

    trigger = {
        is_ai = yes
        is_ruler = yes
        NOT = {
            has_character_flag = qyv_gov_feudal
            has_character_flag = qyv_gov_clan
            has_character_flag = qyv_gov_republic
            has_character_flag = qyv_gov_theocracy
            has_character_flag = qyv_gov_tribal
            has_character_flag = qyv_gov_nomadic
        }
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        if = { limit = { has_government = feudal_government } set_character_flag = qyv_gov_feudal }
        else_if = { limit = { has_government = clan_government } set_character_flag = qyv_gov_clan }
        else_if = { limit = { has_government = republic_government } set_character_flag = qyv_gov_republic }
        else_if = { limit = { has_government = theocracy_government } set_character_flag = qyv_gov_theocracy }
        else_if = { limit = { has_government = tribal_government } set_character_flag = qyv_gov_tribal }
        else_if = { limit = { has_government = nomadic_government } set_character_flag = qyv_gov_nomadic }
    }

    option = { name = "OK" }
}

# ============================================================
# QUARTERLY: GOVERNMENT-SPECIFIC MANAGEMENT
# ============================================================

# Feudal Management
character_event = {
    id = qyv_gov.0200
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        has_character_flag = qyv_gov_feudal
    }

    mean_time_to_happen = { months = 3 }

    immediate = {
        if = { limit = { qyv_ai_feudal_should_optimize_vassals = root } qyv_ai_feudal_optimize_vassals = yes }
        if = { limit = { qyv_ai_feudal_should_manage_contracts = root } qyv_ai_feudal_manage_contracts = yes }
        if = { limit = { qyv_ai_feudal_should_create_duchies = root } qyv_ai_feudal_create_duchies = yes }
    }

    option = { name = "OK" }
}

# Clan Management
character_event = {
    id = qyv_gov.0300
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        has_character_flag = qyv_gov_clan
    }

    mean_time_to_happen = { months = 3 }

    immediate = {
        if = { limit = { qyv_ai_clan_should_manage_clan_sentiment = root } qyv_ai_clan_manage_sentiment = yes }
        if = { limit = { qyv_ai_clan_should_grant_lands = root } qyv_ai_clan_grant_lands = yes }
        if = { limit = { qyv_ai_clan_should_convert_tribal = root } qyv_ai_clan_convert_tribal = yes }
    }

    option = { name = "OK" }
}

# Republic Management
character_event = {
    id = qyv_gov.0400
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        has_character_flag = qyv_gov_republic
    }

    mean_time_to_happen = { months = 3 }

    immediate = {
        if = { limit = { qyv_ai_republic_should_manage_trade = root } qyv_ai_republic_manage_trade = yes }
        if = { limit = { qyv_ai_republic_should_manage_patricians = root } qyv_ai_republic_manage_patricians = yes }
        if = { limit = { qyv_ai_republic_should_expand_trade_posts = root } qyv_ai_republic_expand_trade_posts = yes }
    }

    option = { name = "OK" }
}

# Theocracy Management
character_event = {
    id = qyv_gov.0500
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        has_character_flag = qyv_gov_theocracy
    }

    mean_time_to_happen = { months = 3 }

    immediate = {
        if = { limit = { qyv_ai_theocracy_should_manage_faith = root } qyv_ai_theocracy_manage_faith = yes }
        if = { limit = { qyv_ai_theocracy_should_convert = root } qyv_ai_theocracy_convert = yes }
        if = { limit = { qyv_ai_theocracy_should_use_faith_head = root } qyv_ai_theocracy_use_faith_head = yes }
    }

    option = { name = "OK" }
}

# Tribal Management
character_event = {
    id = qyv_gov.0600
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        has_character_flag = qyv_gov_tribal
    }

    mean_time_to_happen = { months = 3 }

    immediate = {
        if = { limit = { qyv_ai_tribal_should_raid = root } qyv_ai_tribal_raid = yes }
        if = { limit = { qyv_ai_tribal_should_convert = root } qyv_ai_tribal_convert = yes }
        if = { limit = { qyv_ai_tribal_should_manage_clans = root } qyv_ai_tribal_manage_clans = yes }
    }

    option = { name = "OK" }
}

# Nomadic Management
character_event = {
    id = qyv_gov.0700
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        has_character_flag = qyv_gov_nomadic
    }

    mean_time_to_happen = { months = 3 }

    immediate = {
        if = { limit = { qyv_ai_nomadic_should_manage_horde = root } qyv_ai_nomadic_manage_horde = yes }
        if = { limit = { qyv_ai_nomadic_should_manage_grazing = root } qyv_ai_nomadic_manage_grazing = yes }
        if = { limit = { qyv_ai_nomadic_should_manage_clans = root } qyv_ai_nomadic_manage_clans = yes }
    }

    option = { name = "OK" }
}

# ============================================================
# MONTHLY (WAR): GOVERNMENT-SPECIFIC WAR LOGIC
# ============================================================

character_event = {
    id = qyv_gov.0800
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        is_at_war = yes
    }

    mean_time_to_happen = { months = 1 }

    immediate = {
        if = { limit = { qyv_ai_feudal_war_logic = root } qyv_ai_feudal_war_logic = yes }
        else_if = { limit = { qyv_ai_clan_war_logic = root } qyv_ai_clan_war_logic = yes }
        else_if = { limit = { qyv_ai_republic_war_logic = root } qyv_ai_republic_war_logic = yes }
        else_if = { limit = { qyv_ai_tribal_war_logic = root } qyv_ai_tribal_war_logic = yes }
    }

    option = { name = "OK" }
}

# NOTE: on_actions wiring for qyv_gov.* lives in common/on_actions/qyv_on_actions.txt.
# This events file does not define its own on_actions block (would conflict).