namespace = qyv_ai_economy

# ============================================================
# YEARLY: ECONOMIC SPECIALIZATION DETECTION
# ============================================================

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

    trigger = {
        is_ai = yes
        is_ruler = yes
        NOT = {
            has_character_flag = qyv_econ_coastal
            has_character_flag = qyv_econ_agrarian
            has_character_flag = qyv_econ_mountain
        }
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        # Detect coastal economy
        if = {
            limit = { qyv_ai_economy_coastal = root }
            set_character_flag = qyv_econ_coastal
        }
        # Detect agrarian economy
        else_if = {
            limit = { qyv_ai_economy_agrarian = root }
            set_character_flag = qyv_econ_agrarian
        }
        # Detect mountain economy
        else_if = {
            limit = { qyv_ai_economy_mountain = root }
            set_character_flag = qyv_econ_mountain
        }
        # Detect city focus
        else_if = {
            limit = { qyv_ai_economy_city_focus = root }
            set_character_flag = qyv_econ_city
        }
        # Detect temple focus
        else_if = {
            limit = { qyv_ai_economy_temple_focus = root }
            set_character_flag = qyv_econ_temple
        }
    }

    option = { name = "OK" }
}

# ============================================================
# YEARLY: SPECIALIZED BUILDING STRATEGY
# ============================================================

character_event = {
    id = qyv_econ.0200
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        gold >= 500
        NOT = { in_debt = yes }
        OR = {
            has_character_flag = qyv_econ_coastal
            has_character_flag = qyv_econ_agrarian
            has_character_flag = qyv_econ_mountain
            has_character_flag = qyv_econ_city
            has_character_flag = qyv_econ_temple
        }
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        # Coastal specialization
        if = {
            limit = { has_character_flag = qyv_econ_coastal }
            if = { limit = { qyv_ai_should_build_coastal = root } qyv_ai_build_coastal = yes }
            if = { limit = { qyv_ai_should_build_fort = root } qyv_ai_build_fort = yes }
        }

        # Agrarian specialization
        else_if = {
            limit = { has_character_flag = qyv_econ_agrarian }
            if = { limit = { qyv_ai_should_build_farmland = root } qyv_ai_build_farmland = yes }
            if = { limit = { qyv_ai_should_build_fort = root } qyv_ai_build_fort = yes }
        }

        # Mountain specialization
        else_if = {
            limit = { has_character_flag = qyv_econ_mountain }
            if = { limit = { qyv_ai_should_build_mountain = root } qyv_ai_build_mountain = yes }
            if = { limit = { qyv_ai_should_build_fort = root } qyv_ai_build_fort = yes }
        }

        # City focus
        else_if = {
            limit = { has_character_flag = qyv_econ_city }
            if = { limit = { qyv_ai_should_build_city = root } qyv_ai_build_city = yes }
        }

        # Temple focus
        else_if = {
            limit = { has_character_flag = qyv_econ_temple }
            if = { limit = { qyv_ai_should_build_temple = root } qyv_ai_build_temple = yes }
        }
    }

    option = { name = "OK" }
}

# ============================================================
# YEARLY: CAPITAL & BORDER DEVELOPMENT
# ============================================================

character_event = {
    id = qyv_econ.0300
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        gold >= 500
        NOT = { in_debt = yes }
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        # Invest in development
        qyv_ai_invest_in_development = yes

        # Promote culture
        if = { limit = { qyv_eco_ai_should_promote_culture = root } qyv_ai_promote_culture = yes }

        # Convert faith
        if = { limit = { qyv_ai_should_convert_faith = root } qyv_ai_convert_faith = yes }
    }

    option = { name = "OK" }
}

# ============================================================
# QUARTERLY: VASSAL CONTRACT OPTIMIZATION
# ============================================================

character_event = {
    id = qyv_econ.0400
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
    }

    mean_time_to_happen = { months = 3 }

    immediate = {
        # Optimize vassal contracts
        qyv_ai_optimize_vassal_contracts = yes
    }

    option = { name = "OK" }
}

# ============================================================
# QUARTERLY: LOAN MANAGEMENT
# ============================================================

character_event = {
    id = qyv_econ.0500
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
    }

    mean_time_to_happen = { months = 3 }

    immediate = {
        # Manage loans
        qyv_ai_manage_loans = yes

        # War chest: save gold for expected wars, spend when war is imminent
        qyv_ai_manage_war_chest = yes

        # Diplomatic maintenance: prepare claims before truces expire,
        # renew alliances, and target aging rivals' heirs
        qyv_ai_prepare_claim_before_truce_effect = yes
        qyv_ai_renew_alliance_effect = yes
        qyv_ai_target_rival_heir_effect = yes
    }

    option = { name = "OK" }
}

# ============================================================
# YEARLY: CAPITAL & STRATEGIC BUILDINGS
# ============================================================

character_event = {
    id = qyv_econ.0600
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        gold >= 1000
        NOT = { in_debt = yes }
    }

    mean_time_to_happen = { months = 12 }

    immediate = {
        # Build in capital
        if = { limit = { qyv_ai_should_build_capital = root } qyv_ai_build_capital = yes }

        # Build military buildings
        if = { limit = { qyv_ai_should_build_military = root } qyv_ai_build_military = yes }

        # Build forts on borders
        if = { limit = { qyv_ai_should_build_fort = root } qyv_ai_build_fort = yes }
    }

    option = { name = "OK" }
}

# ============================================================
# ON_ACTIONS
# ============================================================

on_actions = {
    on_yearly_pulse = { events = { qyv_econ.0100 qyv_econ.0200 qyv_econ.0300 qyv_econ.0600 } }
    on_quarterly_pulse = { events = { qyv_econ.0400 qyv_econ.0500 } }
}