// Load server status
function loadStatus() {
    $.getJSON('/api/v2/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"]);
        $("#cleanup-last-ran").text((jsonData["cleanup"]["last_ran"] > 0) ? moment.unix(jsonData["cleanup"]["last_ran"]).utc().fromNow() : "N/A");

        jsonData["spot_providers"].forEach(p => {
            $("#spot_providers-status-container").append(`
                <div class="row row-cols-1 row-cols-md-4 g-4 mb-4 mb-md-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-4 mb-md-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-4 mb-md-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["static_data_providers"].forEach(p => {
            $("#static_data_providers-status-container").append(`
                <div class="row row-cols-1 row-cols-md-4 g-4 mb-4 mb-md-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["sig_ref_data_providers"].forEach(p => {
            $("#sig_ref_data_providers-status-container").append(`
                <div class="row row-cols-1 row-cols-md-4 g-4 mb-4 mb-md-2">
                    <div class="col"><strong>${p["sig_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">References: ${p["enabled"] ? p["reference_count"] : "N/A"}</div>
                </div>`);
        });

        jsonData["callsign_data_providers"].forEach(p => {
            $("#callsign_data_providers-status-container").append(`
                <div class="row row-cols-1 row-cols-md-4 g-4 mb-4 mb-md-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">Lookups: ${p["enabled"] ? p["lookup_count"] : "N/A"}</div>
                </div>`);
        });
    });
}

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