<?php
require_once __DIR__.'/../includes/functions.php';
$admin = require_admin();
$adminTitle = 'System Health';
$adminSubtitle = 'Check server readiness, required folders, database tables, migrations, links and admin alerts.';
if (function_exists('mj_v289_ensure_schema')) { mj_v289_ensure_schema(); }
$checks = function_exists('mj_v288_health_checks') ? mj_v288_health_checks() : [];
$metrics = function_exists('mj_v289_admin_dashboard_metrics') ? mj_v289_admin_dashboard_metrics() : [];
$score = function_exists('mj_v289_admin_health_score') ? mj_v289_admin_health_score($metrics) : 100;
$okCount = 0; foreach($checks as $c){ if(!empty($c['ok'])) $okCount++; }
include __DIR__.'/_header.php';
?>

<div class="row mb-3">
  <div class="col-lg-4 mb-3"><div class="card mj-health-summary"><div class="card-body"><small>Overall Health</small><div class="d-flex align-items-center justify-content-between"><h2><?= e($score) ?>%</h2><div class="mj-health-ring" style="--score:<?= e($score) ?>"><span><?= e($score) ?></span></div></div><div class="progress" style="height:8px"><div class="progress-bar <?= $score>=80?'bg-success':($score>=50?'bg-warning':'bg-danger') ?>" style="width:<?= e($score) ?>%"></div></div></div></div></div>
  <div class="col-lg-4 mb-3"><div class="card"><div class="card-body"><small>Checks Passing</small><h2><?= e($okCount) ?> / <?= e(count($checks)) ?></h2><p class="text-muted mb-0">Upload folders, config, PHP and required tables.</p></div></div></div>
  <div class="col-lg-4 mb-3"><div class="card"><div class="card-body"><small>Action Required</small><h2><?= e(number_format((int)(($metrics['errors_open']??0)+($metrics['support_open']??0)+($metrics['payments_pending']??0)))) ?></h2><p class="text-muted mb-0">Errors + support + payment queue.</p></div></div></div>
</div>

<div class="card card-primary card-outline">
  <div class="card-header d-flex align-items-center"><h3 class="card-title"><i class="fas fa-heartbeat mr-2"></i>System Health Checks</h3><div class="ml-auto"><a href="link-health.php" class="btn btn-sm btn-outline-primary"><i class="fas fa-link mr-1"></i>Link Health</a></div></div>
  <div class="card-body p-0 table-responsive">
    <table class="table table-hover mb-0"><thead><tr><th>Check</th><th>Value</th><th>Status</th><th>Suggestion</th></tr></thead><tbody>
      <?php foreach($checks as $c): ?>
      <tr><td><b><?= e($c['name']) ?></b></td><td><?= e($c['value']) ?></td><td><?= !empty($c['ok'])?'<span class="badge badge-success">OK</span>':'<span class="badge badge-danger">Need Fix</span>' ?></td><td><?= !empty($c['ok'])?'<span class="text-muted">Ready</span>':'<span class="text-danger">Check migration, folder permission 755/775, or server setting.</span>' ?></td></tr>
      <?php endforeach; ?>
    </tbody></table>
  </div>
</div>

<div class="row">
  <div class="col-md-4"><a href="error-center.php" class="mj-system-shortcut"><i class="fas fa-bug"></i><b><?= e(number_format((int)($metrics['errors_open']??0))) ?></b><span>Open Errors</span></a></div>
  <div class="col-md-4"><a href="automation-center.php" class="mj-system-shortcut"><i class="fas fa-robot"></i><b>Run</b><span>Automation Center</span></a></div>
  <div class="col-md-4"><a href="version.php" class="mj-system-shortcut"><i class="fas fa-code-branch"></i><b>v2.8.9</b><span>Version / Update</span></a></div>
</div>

<?php include __DIR__.'/_footer.php'; ?>
