// Load server status
function loadStatus() {
    $.getJSON('/api/v1/status', function(jsonData) {
        $("#software-version").text(jsonData["software-version"]);
        $("#server-owner-callsign").text(jsonData["server-owner-callsign"]);
        $("#up-since").text(moment().subtract(jsonData["uptime"], 'seconds').fromNow());
        $("#memory-use").text(jsonData["mem_use_mb"] + " MB");
        $("#total-spots").text(jsonData["num_spots"]);
        $("#total-alerts").text(jsonData["num_alerts"]);

        $("#web-server-status").text(jsonData["webserver"]["status"]);
        $("#web-server-last-api").text(moment.unix(jsonData["webserver"]["last_api_access"]).utc().fromNow());
        $("#web-server-last-page").text(moment.unix(jsonData["webserver"]["last_page_access"]).utc().fromNow());

        $("#cleanup-status").text(jsonData["cleanup"]["status"]);
        $("#cleanu-last-ran").text(moment.unix(jsonData["cleanup"]["last_ran"]).utc().fromNow());

        jsonData["spot_providers"].forEach(p => {
            $("#spot-providers-status-container").append(`
                <div class="row row-cols-1 row-cols-md-4 g-4 mb-2">
                    <div class="col"><strong>${p["name"]}</strong></div>
                    <div class="col">Status: ${p["status"]}</div>
                    <div class="col">Last updated: ${(p["enabled"] && p["last_updated"] > 0) ? moment.unix(p["last_updated"]).utc().fromNow() : "N/A"}</div>
                    <div class="col">Latest spot: ${(p["enabled"] && p["last_spot"] > 0) ? moment.unix(p["last_spot"]).utc().fromNow() : "N/A"}</div>
                </div>`);
        });

        jsonData["alert_providers"].forEach(p => {
            $("#alert-providers-status-container").append(`
                <div class="row row-cols-1 row-cols-md-4 g-4 mb-2">
                    <div class="col"><strong>${p["name"]}</strong></div>
                    <div class="col">Status: ${p["status"]}</div>
                    <div class="col">Last updated: ${(p["enabled"] && p["last_updated"] > 0) ? moment.unix(p["last_updated"]).utc().fromNow() : "N/A"}</div>
                </div>`);
        });

        jsonData["solar_condition_providers"].forEach(p => {
            $("#condition-providers-status-container").append(`
                <div class="row row-cols-1 row-cols-md-4 g-4 mb-2">
                    <div class="col"><strong>${p["name"]}</strong></div>
                    <div class="col">Status: ${p["status"]}</div>
                    <div class="col">Last updated: ${(p["enabled"] && p["last_updated"] > 0) ? moment.unix(p["last_updated"]).utc().fromNow() : "N/A"}</div>
                </div>`);
        });
    });
}

// Startup
$(document).ready(function() {
    loadStatus();
});