#!/usr/bin/env perl

use v5.24.0;
use strict;
#use warnings;

use Data::Dumper;
use Storable;
use lib '../lib';
use Dist;
use Template;

my $data = retrieve '../everything.stor';

my $tested_count =()= grep {exists $data->{jobstatus}{$_}{tested}} keys $data->{jobstatus}->%*;
my $inc_count =()= grep {$data->{jobstatus}{$_}{status} =~ /inc/} keys $data->{jobstatus}->%*;
my $gen_count =()= grep {$data->{jobstatus}{$_}{status} =~ /gen/} keys $data->{jobstatus}->%*;
my $dists = 37410;

my $percent = sprintf "%02.02f%%", $tested_count/$dists * 100;
my $inc_percent = sprintf "~%02.02f%%", $inc_count/$tested_count * 100;
my $gen_percent = sprintf "~%02.02f%%", $gen_count/$tested_count * 100;

my @mods;

for my $mod ($data->{modules}->@*) {
  my ($tested, $status) = $data->{jobstatus}{$mod}->@{qw/tested status/};
  $tested = !!$tested ? "yes" : "no";
  $status //= "";

  my $log = "";
  if ($status && $status ne 'success') {
    my $failure = $status =~ /inc/ ? 'incfailure' : 'genfailure';
    $log = "<a href='/logs/${mod}_$failure.log'>here</a>"
  }

  my $author = Dist::get_author($mod);

  next unless $status =~ /inc/;

  push @mods, {
    name => $mod,
    author => $author,
    status => $status,
    tested => $tested,
    dist => $Dist::mod_to_dist{$mod} // $mod,
    log => $log
  };
}

my $vars = {
  modules => \@mods,
  percent => $percent,
  inc_percent => $inc_percent,
  gen_percent => $gen_percent,
  dists => $dists,
  tested_count => $tested_count,
  inc_count => $inc_count,
  gen_count => $gen_count,
};

my $tt = Template->new();
$tt->process('template.tt.html', $vars);
