# ============================================================
# OPPONENT MODELING & MEMORY EFFECTS
# ============================================================

# Memory Formation
qyv_ai_remember_aggression = {
    scope:aggressive_neighbor = {
        set_character_flag = qyv_mem_aggressive
    }
}

qyv_ai_remember_diplomacy = {
    scope:diplomatic_neighbor = {
        set_character_flag = qyv_mem_diplomatic
    }
}

qyv_ai_remember_opportunism = {
    scope:opportunistic_neighbor = {
        set_character_flag = qyv_mem_opportunistic
    }
}

qyv_ai_remember_isolationism = {
    scope:isolationist_neighbor = {
        set_character_flag = qyv_mem_isolationist
    }
}

qyv_ai_remember_reliable_ally = {
    scope:reliable_ally = {
        set_character_flag = qyv_mem_reliable_ally
    }
}

qyv_ai_remember_betrayal = {
    scope:betrayer = {
        set_character_flag = qyv_mem_betrayed
    }
}

qyv_ai_remember_broken_truce = {
    scope:truce_breaker = {
        set_character_flag = qyv_mem_betrayed
    }
}

# Strategy Adjustments
qyv_ai_adjust_vs_aggressive = {
    if = {
        limit = { qyv_ai_memory_aggressive = root }
        any_character = {
            limit = { has_character_flag = qyv_mem_aggressive }
            if = {
                limit = { military_strength > root.military_strength * 0.8 }
                root = {
                    add_opinion = { target = scope:neighbor modifier = defensive_posture years = 12 }
                    set_character_flag = qyv_defensive_vs_aggressive
                }
            }
            else = {
                root = {
                    add_opinion = { target = scope:neighbor modifier = cautious_approach years = 12 }
                    if = { limit = { gold >= 300 } qyv_send_diplomatic_gift = { gift_target = scope:neighbor } }
                }
            }
        }
    }
}

qyv_ai_adjust_vs_diplomatic = {
    if = {
        limit = { qyv_ai_memory_diplomatic = root }
        any_character = {
            limit = { has_character_flag = qyv_mem_diplomatic }
            root = {
                add_opinion = { target = scope:neighbor modifier = trusted_partner years = 24 }
                if = { limit = { NOT = { is_allied_to = scope:neighbor } } add_alliance = scope:neighbor }
            }
        }
    }
}

qyv_ai_adjust_vs_opportunistic = {
    if = {
        limit = { qyv_ai_memory_opportunistic = root }
        any_character = {
            limit = { has_character_flag = qyv_mem_opportunistic }
            if = {
                limit = { military_strength > root.military_strength * 0.7 }
                root = {
                    add_opinion = { target = scope:neighbor modifier = suspicious_of_opportunist years = 12 }
                    set_character_flag = qyv_wary_of_opportunist
                }
            }
            else = {
                root = {
                    add_opinion = { target = scope:neighbor modifier = watchful_eye years = 12 }
                }
            }
        }
    }
}

qyv_ai_adjust_vs_isolationist = {
    if = {
        limit = { qyv_ai_memory_isolationist = root }
        any_character = {
            limit = { has_character_flag = qyv_mem_isolationist }
            root = {
                add_opinion = { target = scope:neighbor modifier = isolationist_neighbor years = 12 }
                # Don't waste diplomacy on them
            }
        }
    }
}

qyv_ai_adjust_vs_reliable_ally = {
    if = {
        limit = { qyv_ai_memory_reliable_ally = root }
        any_character = {
            limit = { has_character_flag = qyv_mem_reliable_ally }
            root = {
                add_opinion = { target = scope:neighbor modifier = proven_friend years = 36 }
                if = { limit = { NOT = { is_allied_to = scope:neighbor } } add_alliance = scope:neighbor }
            }
        }
    }
}

qyv_ai_adjust_vs_betrayed = {
    if = {
        limit = { qyv_ai_memory_betrayed = root }
        any_character = {
            limit = { has_character_flag = qyv_mem_betrayed }
            root = {
                add_opinion = { target = scope:neighbor modifier = eternal_enemy years = 60 }
                add_dread = 10
                set_character_flag = qyv_never_trust_again
                if = { limit = { is_allied_to = scope:neighbor } break_alliance = scope:neighbor }
                if = { limit = { has_truce_with = scope:neighbor } break_truce = scope:neighbor }
            }
        }
    }
}

# Trust Modifiers
qyv_ai_low_trust_behavior = {
    if = {
        limit = { qyv_ai_low_trust = root }
        any_character = {
            limit = { has_character_flag = qyv_mem_betrayed }
            root = {
                add_opinion = { target = scope:neighbor modifier = eternal_enemy years = 60 }
                if = { limit = { is_allied_to = scope:neighbor } break_alliance = scope:neighbor }
                if = { limit = { has_truce_with = scope:neighbor } break_truce = scope:neighbor }
                # Never ally or trade with them again
            }
        }
    }
}

qyv_ai_high_trust_behavior = {
    if = {
        limit = { qyv_ai_high_trust = root }
        any_character = {
            limit = { has_character_flag = qyv_mem_reliable_ally }
            root = {
                add_opinion = { target = scope:neighbor modifier = proven_friend years = 36 }
                if = { limit = { NOT = { is_allied_to = scope:neighbor } } add_alliance = scope:neighbor }
            }
        }
    }
}

# Succession Tracking
qyv_ai_track_succession = {
    if = {
        limit = { qyv_ai_should_track_succession = root }
        scope:succession_risk = {
            set_character_flag = qyv_succession_watch
        }
        root = {
            set_character_flag = qyv_monitoring_succession
        }
    }
}

# War Strategy Based on Personality
qyv_ai_war_vs_aggressive = {
    if = {
        limit = {
            qyv_ai_memory_aggressive = root
            is_at_war = yes
            war = { defender = { is_ruler = root } attacker = { has_character_flag = qyv_mem_aggressive } }
        }
        root = {
            set_character_flag = qyv_defensive_war
            if = { limit = { gold >= 500 } qyv_ai_hire_mercs = yes }
            if = { limit = { qyv_ai_should_call_allies = root } qyv_ai_call_allies = yes }
        }
    }
}

qyv_ai_war_vs_opportunistic = {
    if = {
        limit = {
            qyv_ai_memory_opportunistic = root
            is_at_war = yes
            war = { defender = { is_ruler = root } attacker = { has_character_flag = qyv_mem_opportunistic } }
        }
        root = {
            set_character_flag = qyv_defensive_war
            if = { limit = { gold >= 500 } qyv_ai_hire_mercs = yes }
            if = { limit = { qyv_ai_should_call_allies = root } qyv_ai_call_allies = yes }
            qyv_ai_use_terrain = yes
        }
    }
}

qyv_ai_peace_vs_diplomatic = {
    if = {
        limit = {
            qyv_ai_memory_diplomatic = root
            is_at_war = yes
            war = { defender = { is_ruler = root } attacker = { has_character_flag = qyv_mem_diplomatic } }
        }
        war = { send_white_peace = yes }
    }
}