auto_upgrade_starbases_check = {
	if = {
		limit = {
			has_starbase_size = starbase_outpost
		}
		auto_upgrade_starbase = {
			TECH_1 = tech_starbase_2
			UPGRADE_TO = starbase_starport
			ALLOYS = @starport_cost
			BIO = @starport_cost
			DAYS = 360
		}
	}
	else_if = {
		limit = {
			has_starbase_size = starbase_starport
		}
		auto_upgrade_starbase = {
			TECH_1 = tech_starbase_3
			UPGRADE_TO = starbase_starhold
			ALLOYS = @starhold_cost
			BIO = @starhold_cost
			DAYS = 720
		}
	}
	else_if = {
		limit = {
			has_starbase_size = starbase_starhold
		}
		auto_upgrade_starbase = {
			TECH_1 = tech_starbase_4
			UPGRADE_TO = starbase_starfortress
			ALLOYS = @starfort_cost
			BIO = @starfort_cost
			DAYS = 1080
		}
	}
	else_if = {
		limit = {
			has_starbase_size = starbase_starfortress
		}
		auto_upgrade_starbase = {
			TECH_1 = tech_starbase_5
			UPGRADE_TO = starbase_citadel
			ALLOYS = @citadel_cost
			BIO = @citadel_cost
			DAYS = 1440
		}
	}
	else_if = {
		limit = {
			has_global_flag = has_nsc_active
			has_starbase_size = starbase_citadel
		}
		auto_upgrade_starbase = {
			TECH_1 = tech_starbase_6
			UPGRADE_TO = starbase_stronghold
			ALLOYS = 6500
			BIO = 6500
			DAYS = 2000
		}
	}
	else_if = {
		limit = {
			has_global_flag = has_nsc_active
			has_starbase_size = starbase_stronghold
		}
		auto_upgrade_starbase = {
			TECH_1 = tech_starbase_7
			UPGRADE_TO = starbase_headquarters
			ALLOYS = 8000
			BIO = 8000
			DAYS = 2500
		}
	}
}

auto_upgrade_starbase = {
	if = {
		limit = {
			NOR = {
				has_starbase_flag = auto_upgrading_starbase_flag
				has_starbase_flag = auto_upgrade_starbases_ready_flag
				has_starbase_flag = auto_outfitting_starbase_flag
			}
			root = {
				[[TECH_1]
					has_technology = $TECH_1$
				]
				[[BIO]
					if = {
						limit = {
							country_uses_bio_ships = yes
						}
						resource_stockpile_compare = {
							resource = food
							value >= $BIO$
							mult = value:posivite_starbase_upgrading_bio_cost
						}
					}
				]
				[[ALLOYS]
					resource_stockpile_compare = {
						resource = alloys
						value >= $ALLOYS$
						mult = value:posivite_starbase_upgrading_alloy_cost
					}
				]
				[[UNITY]
					resource_stockpile_compare = {
						resource = unity
						value >= $UNITY$
						mult = value:positive_modifier_mult|MODIFIER|starbase_upgrade_cost_mult|
					}
				]
                [[RARE_CRYSTALS]
                    resource_stockpile_compare = {
                        resource = rare_crystals
                        value >= $RARE_CRYSTALS$
                        mult = value:positive_modifier_mult|MODIFIER|starbase_upgrade_cost_mult|
                    }
                ]
			}
		}
		set_starbase_flag = auto_upgrade_starbases_ready_flag
		if = {
			limit = {
				has_global_flag = has_nsc_active
			}
			set_timed_starbase_flag = {
				flag = auto_upgrading_starbase_flag
				days = root.value:speed_modifier_mult|DAYS|360|MODIFIER|starbase_upgrade_speed_mult|
			}
		}
		else = {
			set_timed_starbase_flag = {
				flag = auto_upgrading_starbase_flag
				days = root.value:speed_modifier_mult|DAYS|$DAYS$|MODIFIER|starbase_upgrade_speed_mult|
			}
		}
		fleet = {
			if = {
				limit = {
					has_global_flag = has_nsc_active
				}
				set_variable = {
					which = lazy_automation_month_timer
					value = root.value:days_to_months_speed_converter|DAYS|360|MODIFIER|starbase_upgrade_speed_mult|
				}
			}
			else = {
				set_variable = {
					which = lazy_automation_month_timer
					value = root.value:days_to_months_speed_converter|DAYS|$DAYS$|MODIFIER|starbase_upgrade_speed_mult|
				}
			}
		}
		root = {
			create_message = {
				type = AUTO_STARBASE_MESSAGE_TYPE
				localization = AUTO_STARBASE_UPGRADING_MESSAGE_TYPE_DESC
				days = 30
				target = prev.solar_system
				variable = {
					type = name
					localization = FLEET
					scope = prev
				}
				variable = {
					type = name
					localization = SYSTEM
					scope = prev.solar_system
				}
			}
			[[BIO]
				if = {
					limit = {
						country_uses_bio_ships = yes
					}
					add_resource = {
						food = $BIO$
						mult = value:negative_starbase_upgrading_bio_cost
					}
				}
			]
			[[ALLOYS]
				add_resource = {
					alloys = $ALLOYS$
					mult = value:negative_starbase_upgrading_alloy_cost
				}
			]
			[[UNITY]
				add_resource = {
					unity = $UNITY$
					mult = value:negative_modifier_mult|MODIFIER|starbase_upgrade_cost_mult|
				}
			]
            [[RARE_CRYSTALS]
                add_resource = {
                    rare_crystals = $RARE_CRYSTALS$
                    mult = value:negative_modifier_mult|MODIFIER|starbase_upgrade_cost_mult|
                }
            ]
		}
	}
	if = {
		limit = {
			NOR = {
				has_starbase_flag = auto_upgrading_starbase_flag
				has_starbase_flag = auto_outfitting_starbase_flag
			}
			has_starbase_flag = auto_upgrade_starbases_ready_flag
		}
		
		set_starbase_size = $UPGRADE_TO$

		remove_starbase_flag = auto_upgrade_starbases_ready_flag

		root = {
			create_message = {
				type = AUTO_STARBASE_MESSAGE_TYPE
				localization = AUTO_STARBASE_UPGRADED_MESSAGE_TYPE_DESC
				days = 30
				target = prev.solar_system
				variable = {
					type = name
					localization = FLEET
					scope = prev
				}
				variable = {
					type = name
					localization = SYSTEM
					scope = prev.solar_system
				}
			}
		}
	}
}