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

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

<h2 class="mb-2">
Track Chair Assignment Governance
</h2>

<p class="text-muted">
Monitor track-chair coverage, orphan tracks, duplicate ownership, recovery priority, and suggested chair assignments.
</p>

</div>
</div>

<?php
$totalTracks =
    count($tracks ?? []);

$orphanCount =
    count($orphanTracks ?? []);

$duplicateCount =
    count($duplicateTracks ?? []);

$governedCount =
    max(
        0,
        $totalTracks - $orphanCount
    );

$coverageRate =
    $totalTracks > 0
    ? round(($governedCount / $totalTracks) * 100, 2)
    : 0;

$highRisk =
    count(
        array_filter(
            $orphanTracks ?? [],
            function($r) {
                return ($r['recovery_priority'] ?? '') === 'HIGH';
            }
        )
    );

$mediumRisk =
    count(
        array_filter(
            $orphanTracks ?? [],
            function($r) {
                return ($r['recovery_priority'] ?? '') === 'MEDIUM';
            }
        )
    );

$lowRisk =
    count(
        array_filter(
            $orphanTracks ?? [],
            function($r) {
                return ($r['recovery_priority'] ?? '') === 'LOW';
            }
        )
    );
?>

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

<div class="col-md-3">
<div class="card border-0 shadow-sm">
<div class="card-body text-center">
<div class="text-muted">Total Tracks</div>
<h1 class="text-primary"><?= (int)$totalTracks ?></h1>
</div>
</div>
</div>

<div class="col-md-3">
<div class="card border-0 shadow-sm">
<div class="card-body text-center">
<div class="text-muted">Governed Tracks</div>
<h1 class="text-success"><?= (int)$governedCount ?></h1>
</div>
</div>
</div>

<div class="col-md-3">
<div class="card border-0 shadow-sm">
<div class="card-body text-center">
<div class="text-muted">Orphan Tracks</div>
<h1 class="text-danger"><?= (int)$orphanCount ?></h1>
</div>
</div>
</div>

<div class="col-md-3">
<div class="card border-0 shadow-sm">
<div class="card-body text-center">
<div class="text-muted">Coverage Rate</div>
<h1 class="text-info"><?= htmlspecialchars((string)$coverageRate) ?>%</h1>
</div>
</div>
</div>

</div>

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

<div class="col-md-3">
<div class="card border-0 shadow-sm">
<div class="card-body text-center">
<div class="text-muted">High Priority</div>
<h1 class="text-danger"><?= (int)$highRisk ?></h1>
</div>
</div>
</div>

<div class="col-md-3">
<div class="card border-0 shadow-sm">
<div class="card-body text-center">
<div class="text-muted">Medium Priority</div>
<h1 class="text-warning"><?= (int)$mediumRisk ?></h1>
</div>
</div>
</div>

<div class="col-md-3">
<div class="card border-0 shadow-sm">
<div class="card-body text-center">
<div class="text-muted">Low Priority</div>
<h1 class="text-secondary"><?= (int)$lowRisk ?></h1>
</div>
</div>
</div>

<div class="col-md-3">
<div class="card border-0 shadow-sm">
<div class="card-body text-center">
<div class="text-muted">Secondary Ownership</div>
<h1 class="text-warning"><?= (int)$duplicateCount ?></h1>
</div>
</div>
</div>

</div>

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

<h4>Track Governance Overview</h4>

<div class="table-responsive">

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

<thead class="table-dark">

<tr>
<th>ID</th>
<th>Conference</th>
<th>Track</th>
<th>Active Chairs</th>
<th>Submissions</th>
<th>Reviews</th>
<th>Governance Status</th>
</tr>

</thead>

<tbody>

<?php foreach(($tracks ?? []) as $r): ?>

<tr>

<td><?= (int)($r['id'] ?? 0) ?></td>

<td>
<?= htmlspecialchars($r['conference_name'] ?? ('Conference #' . (int)($r['conference_id'] ?? 0))) ?>
</td>

<td><?= htmlspecialchars($r['track_name'] ?? '') ?></td>

<td><?= (int)($r['active_chairs'] ?? 0) ?></td>

<td><?= (int)($r['submissions'] ?? 0) ?></td>

<td><?= (int)($r['reviews'] ?? 0) ?></td>

<td>
<?php if((int)($r['active_chairs'] ?? 0) === 0): ?>

<span class="badge bg-danger">
No Chair
</span>

<?php elseif((int)($r['active_chairs'] ?? 0) > 1): ?>

<span class="badge bg-warning text-dark">
Primary + Secondary
</span>

<?php else: ?>

<span class="badge bg-success">
OK
</span>

<?php endif; ?>
</td>

</tr>

<?php endforeach; ?>

</tbody>

</table>

</div>

</div>
</div>

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

<h4>Tracks Without Active Chairs — Recovery Queue</h4>

<p class="text-muted">
Suggested chair is advisory only. This page does not automatically assign tracks.
</p>

<div class="table-responsive">

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

<thead class="table-dark">
<tr>
<th>ID</th>
<th>Conference</th>
<th>Track</th>
<th>Submissions</th>
<th>Reviews</th>
<th>Priority</th>
<th>Suggested Chair</th>
<th>Suggested Chair Load</th>
</tr>
</thead>

<tbody>

<?php foreach(($orphanTracks ?? []) as $r): ?>

<tr>

<td><?= (int)($r['id'] ?? 0) ?></td>

<td>
<?= htmlspecialchars($r['conference_name'] ?? ('Conference #' . (int)($r['conference_id'] ?? 0))) ?>
</td>

<td><?= htmlspecialchars($r['track_name'] ?? '') ?></td>

<td><?= (int)($r['submissions'] ?? 0) ?></td>

<td><?= (int)($r['reviews'] ?? 0) ?></td>

<td>

<?php if(($r['recovery_priority'] ?? '') === 'HIGH'): ?>

<span class="badge bg-danger">
HIGH
</span>

<?php elseif(($r['recovery_priority'] ?? '') === 'MEDIUM'): ?>

<span class="badge bg-warning text-dark">
MEDIUM
</span>

<?php else: ?>

<span class="badge bg-secondary">
LOW
</span>

<?php endif; ?>

</td>

<td>
<?= htmlspecialchars($r['suggested_chair'] ?? '') ?>
</td>

<td>
Tracks:
<?= (int)($r['suggested_tracks'] ?? 0) ?>
/
Reviews:
<?= (int)($r['suggested_reviews'] ?? 0) ?>
</td>

</tr>

<?php endforeach; ?>

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

<tr>
<td colspan="8" class="text-center text-success">
All tracks currently have active chair ownership.
</td>
</tr>

<?php endif; ?>

</tbody>

</table>

</div>

</div>
</div>

<div class="card border-0 shadow-sm">
<div class="card-body">

<h4>Tracks With Secondary Ownership</h4>

<p class="text-muted">
Multiple active chairs are treated as primary/secondary ownership, not as an error.
The first active assignment is considered primary.
</p>

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

<thead class="table-dark">
<tr>
<th>ID</th>
<th>Conference</th>
<th>Track</th>
<th>Active Chairs</th>
<th>Governance Interpretation</th>
</tr>
</thead>

<tbody>

<?php foreach(($duplicateTracks ?? []) as $r): ?>

<tr>

<td><?= (int)($r['id'] ?? 0) ?></td>

<td>
<?= htmlspecialchars($r['conference_name'] ?? ('Conference #' . (int)($r['conference_id'] ?? 0))) ?>
</td>

<td><?= htmlspecialchars($r['track_name'] ?? '') ?></td>

<td><?= (int)($r['active_chairs'] ?? 0) ?></td>

<td>
<span class="badge bg-warning text-dark">
Primary + Secondary
</span>
</td>

</tr>

<?php endforeach; ?>

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

<tr>
<td colspan="5" class="text-center text-success">
No secondary ownership detected.
</td>
</tr>

<?php endif; ?>

</tbody>

</table>

</div>
</div>

</div>
