package dev.nincodedo.ninbot.common;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.sharding.ShardManager;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class JDAHealthIndicator implements HealthIndicator {

    private final ShardManager shardManager;

    public JDAHealthIndicator(ShardManager shardManager) {
        this.shardManager = shardManager;
    }

    @Override
    public Health health() {
        var shardStatuses = shardManager.getStatuses();
        var totalShardCount = shardStatuses.values().size();
        var connectedShardsCount = getConnectedShardsCount(shardStatuses);
        if (totalShardCount == connectedShardsCount && connectedShardsCount > 0) {
            return Health.up().status(new Status("UP", getShardTotals(connectedShardsCount, totalShardCount))).build();
        } else if (connectedShardsCount > 0) {
            return Health.up()
                    .status(new Status("Partial service", getShardTotals(connectedShardsCount, totalShardCount)))
                    .build();
        } else {
            return Health.down().build();
        }
    }

    private String getShardTotals(int connectedShardsCount, int totalShardCount) {
        return String.format("%s of %s shards connected", connectedShardsCount, totalShardCount);
    }

    private int getConnectedShardsCount(Map<JDA, JDA.Status> shardStatuses) {
        return (int) shardStatuses.values().stream().filter(status -> status == JDA.Status.CONNECTED).count();
    }
}

@@ -236,63 +236,8 @@
 or;%0A
-import org.springframework.boot.actuate.health.Status;%0A
 impo
@@ -779,276 +779,171 @@
     
-if (totalShardCount == connectedShardsCount && connectedShardsCount %3E 0) %7B%0A            return Health.up().status(new Status(%22UP%22, getShardTotals(connectedShardsCount, totalShardCount))).build();%0A        %7D else if (connectedShardsCount %3E 0) %7B%0A            return
+Health.Builder healthBuilder;%0A        if (connectedShardsCount %3E 0) %7B%0A            healthBuilder = Health.up();%0A        %7D else %7B%0A            healthBuilder =
  Health.
 up()
@@ -938,20 +938,23 @@
  Health.
-up
+down
 ()
+;
 %0A       
@@ -950,32 +950,34 @@
 ();%0A        
+%7D%0A
         
     .status(
@@ -968,65 +968,41 @@
     
-    .status(new Status(%22Partial service%22, getShardTotals(
+return healthBuilder.withDetail(%22
 conn
@@ -1015,34 +1015,37 @@
 hard
-s
 Count
+%22
 , 
-total
+connected
 Shard
+s
 Count)
-))
 %0A   
@@ -1061,161 +1061,38 @@
     
-    .build();%0A        %7D else %7B%0A            return Health.down().build();%0A        %7D%0A    %7D%0A%0A    private String getShardTotals(int connected
+.withDetail(%22total
 Shard
-s
 Count
+%22
 ,
- int
  tot
@@ -1104,18 +1104,16 @@
 rdCount)
- %7B
 %0A       
@@ -1117,95 +1117,23 @@
     
-return String.format(%22%25s of %25s shards connected%22, connectedShardsCount, totalShardCount
+        .build(
 );%0A 

Update JDAHealthIndicator