use Updown:ver<0.0.5>:auth<zef:lizmat>;

unit sub MAIN(:$api-key);

sub divider() {
    say "================================================";
}

sub duration-human(Int() $duration) {
    "$duration seconds";
}

say "Updown.io Checker v{ Updown.^ver }";
divider;
my $ud = Updown.new(:$api-key);

my @problems;
for $ud.checks.values.sort({ .alias || .url }) {
    print .down ?? "❌ " !! "✅ ";
    print .title;
    if .uptime < 100 {
        @problems.push: $_;
        printf " (%4.2f%%)", .uptime;
    }
    print "\n";
}

if @problems {
    divider;
    for @problems -> $check {
        say "\n❌ $check.title()";
        for $ud.downtimes($check.token).head(3) {
            say "$_.started_at() {
                "for &duration-human($_) " with .duration
            }(&duration-human(DateTime.now - .started_at) ago)";
            say "   URL: $check.url()";
            say " Error: $_.error() { "(partial)" if .partial }";
        }
    }
}
divider;

# vim: expandtab shiftwidth=4
