namespace = lazy_meg_upg_events

country_event = {
	id = lazy_meg_upg_events.1
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_country_flag = auto_upgrade_megastructure_flag
	}

	immediate = {
		while = {
			count = auto_meg_upg_speed

			set_variable = {
				which = auto_upgrade_megastructure_current
				value = -1
			}
			every_owned_megastructure = {
				limit = {
					is_upgrading = yes
				}
				prev = {
					change_variable = {
						which = auto_upgrade_megastructure_current
						value = 1
					}
				}
			}
			if = {
				limit = {
					has_technology = tech_mega_engineering
					check_variable = {
						which = auto_upgrade_megastructure_current
						value < value:mega_cap_modifier
					}
				}
				random_owned_megastructure = {
					limit = {
						is_upgrading = no
						NOT = {
							has_megastructure_flag = auto_upgrade_mega_recently_checked
						}
					}
					set_timed_megastructure_flag = {
						flag = auto_upgrade_mega_recently_checked
						days = 77
					}
					auto_upgrade_megastructures_check = yes
				}
			}
		}
	}
}