scripted_gui = {

	Spy_Menu = {
		context_type = selected_country_context
		
		window_name = "Spy_Menu_container"
		parent_window_token = selected_country_view_diplomacy
		
		visible={
		check_variable = { ROOT.show_Spy_Menu@PREV = 0 }
		NOT = {TAG = ROOT}
		}
		

		effects = {
            Spy_Menu_click = {
				set_variable = { ROOT.show_Spy_Menu@PREV = 1 }
			}
		}
	
	
	
		ai_enabled = {
            always = yes
        }

        ai_check = {
            check_variable = { spies_available > 99 }
}
        
        ai_test_interval = 1000
        ai_test_variance = 0.3
		
		ai_test_scopes = test_neighbouring_countries
        ai_test_scopes = test_enemy_countries
		
		# FIXED: Prevent AI from opening spy menu on allies and friendly countries
		ai_check_scope = {
        # Must have negative opinion
        check_variable = {ROOT.opinion@PREV < 0}
        # Must have significant industry
		check_variable = {num_of_factories > 30}
        # NOT in a faction together
        NOT = { is_in_faction_with = ROOT }
        # NOT guaranteed by ROOT
        NOT = { has_guaranteed = ROOT }
        # ROOT has NOT guaranteed them
        ROOT = { NOT = { has_guaranteed = PREV } }
        # NOT a subject of ROOT
        NOT = { is_subject_of = ROOT }
        # ROOT is NOT a subject of them
        ROOT = { NOT = { is_subject_of = PREV } }
        # NOT at war together (on same side)
        OR = {
            # Either no war
            has_war = no
            # Or if at war, must be at war WITH each other (not as allies)
            AND = {
                has_war = yes
                has_war_with = ROOT
            }
        }
		}
		
		ai_max_weight_taken_per_test = 1
		
		
		ai_weights = {
        Spy_Menu_click = {
        weight = 1
		
		ai_will_do = {
		
            factor = 1

        }}}
	
	
	
	}
    
}