<?php
$title =
    'Homepage Workflow Duration Analytics';

function hwd_h($value)
{
    return htmlspecialchars(
        (string)$value,
        ENT_QUOTES,
        'UTF-8'
    );
}

function hwd_duration_label($minutes)
{
    $minutes =
        (int)$minutes;

    if ($minutes < 1) {
        return 'Less than 1 minute';
    }

    if ($minutes < 60) {
        return $minutes . ' min';
    }

    $hours =
        floor($minutes / 60);

    $remaining =
        $minutes % 60;

    if ($hours < 24) {
        return $hours . 'h ' . $remaining . 'm';
    }

    $days =
        floor($hours / 24);

    $remainingHours =
        $hours % 24;

    return $days . 'd ' . $remainingHours . 'h';
}
?>

<div class="container-fluid py-4">

<div class="card border-0 shadow-lg rounded-4 mb-4">

<div class="card-body">

<h2 class="fw-bold">

<i class="fa fa-chart-line text-primary"></i>

Homepage Workflow Duration Analytics

</h2>

<div class="text-muted">

Measure workflow speed, transition duration, and homepage publication bottlenecks.

</div>

</div>

</div>

<div class="row g-4 mb-4">

<div class="col-lg-3">

<div class="card border-0 shadow-sm rounded-4 h-100">

<div class="card-body text-center">

<div class="text-muted small">

Total Transitions

</div>

<div class="fs-1 fw-bold">

<?= (int)($summary['total_transitions'] ?? 0) ?>

</div>

</div>

</div>

</div>

<div class="col-lg-3">

<div class="card border-0 shadow-sm rounded-4 h-100">

<div class="card-body text-center">

<div class="text-muted small">

Avg Transition Time

</div>

<div class="fs-3 fw-bold">

<?= hwd_duration_label($summary['avg_minutes'] ?? 0) ?>

</div>

</div>

</div>

</div>

<div class="col-lg-3">

<div class="card border-0 shadow-sm rounded-4 h-100">

<div class="card-body text-center">

<div class="text-muted small">

Fastest Transition

</div>

<div class="fs-3 fw-bold">

<?= hwd_duration_label($summary['fastest_minutes'] ?? 0) ?>

</div>

</div>

</div>

</div>

<div class="col-lg-3">

<div class="card border-0 shadow-sm rounded-4 h-100">

<div class="card-body text-center">

<div class="text-muted small">

Slowest Transition

</div>

<div class="fs-3 fw-bold">

<?= hwd_duration_label($summary['slowest_minutes'] ?? 0) ?>

</div>

</div>

</div>

</div>

</div>

<div class="card border-0 shadow-sm rounded-4 mb-4">

<div class="card-body">

<h4 class="fw-bold mb-3">

<i class="fa fa-clock text-primary"></i>

Transition Duration Details

</h4>

<?php if(empty($durationRows)): ?>

<div class="alert alert-info">

Not enough workflow history yet. At least two transitions for the same conference are needed to calculate durations.

</div>

<?php else: ?>

<div class="table-responsive">

<table class="table table-hover align-middle">

<thead>

<tr>

<th>Conference</th>

<th>From</th>

<th>To</th>

<th>Started</th>

<th>Ended</th>

<th>Duration</th>

<th>Changed By</th>

</tr>

</thead>

<tbody>

<?php foreach($durationRows as $row): ?>

<tr>

<td>

<?= hwd_h($row['conference_name']) ?>

</td>

<td>

<?= hwd_h($row['from_status']) ?>

</td>

<td>

<?= hwd_h($row['to_status']) ?>

</td>

<td>

<?= hwd_h($row['started_at']) ?>

</td>

<td>

<?= hwd_h($row['ended_at']) ?>

</td>

<td>

<span class="badge bg-primary">

<?= hwd_duration_label($row['minutes']) ?>

</span>

</td>

<td>

<?= hwd_h($row['changed_by_name']) ?>

</td>

</tr>

<?php endforeach; ?>

</tbody>

</table>

</div>

<?php endif; ?>

</div>

</div>

<div class="card border-0 shadow-sm rounded-4 mb-4">

<div class="card-body">

<h4 class="fw-bold mb-3">

<i class="fa fa-ranking-star text-warning"></i>

Conference Duration Ranking

</h4>

<?php if(empty($conferenceRanking)): ?>

<div class="alert alert-info">

No ranking available yet.

</div>

<?php else: ?>

<table class="table table-bordered align-middle">

<thead>

<tr>

<th>Conference</th>

<th>Total Workflow Time</th>

<th>Transitions</th>

</tr>

</thead>

<tbody>

<?php foreach($conferenceRanking as $row): ?>

<tr>

<td>

<?= hwd_h($row['conference_name']) ?>

</td>

<td>

<?= hwd_duration_label($row['total_minutes']) ?>

</td>

<td>

<?= (int)$row['transitions'] ?>

</td>

</tr>

<?php endforeach; ?>

</tbody>

</table>

<?php endif; ?>

</div>

</div>

</div>
