scripted_gui = {
	spy_recruitment_office = {
	
		context_type = selected_state_context
		parent_window_token = selected_state_view
		window_name = "spy_recruitment_office_container"
		
		visible = {
			CONTROLLER = {
				tag = ROOT 
			}
		}
		
		effects = {
			build_spy_recruitment_office_click = {
				THIS = {
					add_building_construction = {
                    type = Spy_University
                    level = 1
                    instant_build = no
				} 
				}
			}
		}
		
		triggers = {
	     
		 
		enemy_resistance_click_enabled = {check_variable = {FROM.FROM.resistance > 0}}
		 
		}
		
	
		# ===== FIXED: AI NOW BUILDS UNIVERSITIES =====	
		ai_enabled = {
            always = yes  # Changed from "no" to "yes"
        }
		
		# AI should build universities when it has spare IC and needs spies
		ai_check = {
			# Has some civilian factories available
			num_of_civilian_factories > 10
			
			# Doesn't have too many spies already
			OR = {
				# Either under 50% of cap
				AND = {
					is_major = yes
					check_variable = { spies_available < 1000 }
				}
				AND = {
					is_major = no
					check_variable = { spies_available < 400 }
				}
			}
		}
		
		# Test periodically
		ai_test_interval = 180  # Check every 6 months
		ai_test_variance = 0.3
		
		# Only build in owned states
		ai_test_scopes = test_random_owned_states
		
		# Build in states with good infrastructure and no university yet
		ai_check_scope = {
			# State doesn't already have a university (max 1 per state)
			NOT = { Spy_University amount > 0 }
			
			# Prefer developed states
			infrastructure > 3
			
			# Prefer states with factories (major cities)
			OR = {
				industrial_complex > 1
				arms_factory > 1
			}
		}
		
		# How much AI wants to build universities
		ai_max_weight_taken_per_test = 1
		
		ai_weights = {
			build_spy_recruitment_office_click = {
				weight = 10
				
				ai_will_do = {
					factor = 1
					
					# Higher priority for major powers
					modifier = {
						is_major = yes
						factor = 2
					}
					
					# Higher priority if at war or about to be
					modifier = {
						OR = {
							has_war = yes
							threat > 0.3
						}
						factor = 1.5
					}
					
					# Lower priority if low on IC
					modifier = {
						num_of_civilian_factories < 15
						factor = 0.3
					}
					
					# Don't build too many - diminishing priority
					modifier = {
						check_variable = { Spy_University > 3 }
						factor = 0.5
					}
					
					modifier = {
						check_variable = { Spy_University > 5 }
						factor = 0.3
					}
					
					modifier = {
						check_variable = { Spy_University > 7 }
						factor = 0.2
					}
				}
			}
		}
	}
}