auto_upgrade_mega_starbase_check = {
	if = {
		limit = {
			is_ship_size = starbase_deep_space_citadel_1
		}
		auto_upgrade_mega_starbase = {
			TECH_1 = tech_deep_space_citadel
			UPGRADE_TO = starbase_deep_space_citadel_2
			UNITY = 2500
			RARE_CRYSTALS = 100
			ALLOYS = @starfort_cost
			BIO = @starfort_cost
			DAYS = 1080
		}
	}
	else_if = {
		limit = {
			is_ship_size = starbase_deep_space_citadel_2
		}
		auto_upgrade_mega_starbase = {
			TECH_1 = tech_deep_space_citadel
			UPGRADE_TO = starbase_deep_space_citadel_3
			UNITY = 5000
			RARE_CRYSTALS = 500
			ALLOYS = @citadel_cost
			BIO = @citadel_cost
			DAYS = 1440
		}
	}
}

auto_upgrade_mega_starbase = {
	if = {
		limit = {
			NOR = {
				has_fleet_flag = auto_upgrading_mega_starbase_flag
				has_fleet_flag = auto_upgrade_mega_starbases_ready_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_fleet_flag = auto_upgrade_mega_starbases_ready_flag
		set_timed_fleet_flag = {
			flag = auto_upgrading_mega_starbase_flag
			days = root.value:speed_modifier_mult|DAYS|$DAYS$|MODIFIER|starbase_upgrade_speed_mult|
		}
		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.starbase
				}
				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 = {
			NOT = {
				has_fleet_flag = auto_upgrading_mega_starbase_flag
			}
			has_fleet_flag = auto_upgrade_mega_starbases_ready_flag
		}
		
		random_owned_ship = {
			limit = {
				OR = {
					is_ship_size = starbase_deep_space_citadel_1
					is_ship_size = starbase_deep_space_citadel_2
				}
			}
			starbase = {
				set_starbase_size = $UPGRADE_TO$

				prev = {
					set_ship_design = {
						random_existing_design = $UPGRADE_TO$
					}
				}

				if = {
					limit = {
						is_ship_size = starbase_deep_space_citadel_2
					}
					remove_modifier = auto_fix_deep_space_citadel_3
					add_modifier = {
						modifier = auto_fix_deep_space_citadel_2
					}
				}
				else_if = {
					limit = {
						is_ship_size = starbase_deep_space_citadel_3
					}
					remove_modifier = auto_fix_deep_space_citadel_2
					add_modifier = {
						modifier = auto_fix_deep_space_citadel_3
					}
				}
				else = {
					remove_modifier = auto_fix_deep_space_citadel_2
					remove_modifier = auto_fix_deep_space_citadel_3
				}
			}
		}

		remove_fleet_flag = auto_upgrade_mega_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.starbase
				}
				variable = {
					type = name
					localization = SYSTEM
					scope = prev.solar_system
				}
			}
		}
	}
}