<?php

/**
 * @var list<array{status: string, name: string, message: string, label: string, description: string}> $checks
 * @var string $overall
 * @var array{php_version: string, environment: string} $meta
 * @var string $lastChecked
 */

$checks = $checks ?? [];
$overall = $overall ?? 'ok';
$meta = $meta ?? ['php_version' => '', 'environment' => ''];
$lastChecked = $lastChecked ?? '';

$bannerTitles = [
    'ok' => t('All systems operational'),
    'warn' => t('Degraded service'),
    'fail' => t('System outage detected'),
];
$bannerIcons = [
    'ok' => 'bi-check-circle-fill',
    'warn' => 'bi-exclamation-triangle-fill',
    'fail' => 'bi-x-circle-fill',
];
$bannerVariants = [
    'ok' => 'success',
    'warn' => 'warn',
    'fail' => 'danger',
];
$statusBadgeLabel = [
    'ok' => t('OK'),
    'warn' => t('Warning'),
    'fail' => t('Fail'),
];

$bannerTitle = $bannerTitles[$overall] ?? $bannerTitles['ok'];
$bannerIcon = $bannerIcons[$overall] ?? $bannerIcons['ok'];
$bannerVariant = $bannerVariants[$overall] ?? $bannerVariants['ok'];

?>
<div class="app-dashboard-titlebar">
  <h1><?php e(t('System status')); ?></h1>
</div>

<section class="app-status-banner" data-variant="<?php e($bannerVariant); ?>" aria-live="polite">
  <i class="bi <?php e($bannerIcon); ?>" aria-hidden="true"></i>
  <div class="app-status-banner-text">
    <h2><?php e($bannerTitle); ?></h2>
    <small><?php e(t('Last checked')); ?>: <?php e($lastChecked); ?></small>
  </div>
</section>

<div class="app-stats-table">
  <div class="app-stats-table-header">
    <small><?php e(t('Components')); ?></small>
  </div>
  <?php if (empty($checks)): ?>
    <?php $emptyState = ['message' => t('No health checks available.'), 'size' => 'compact']; require templatePath('partials/app-empty-state.phtml'); ?>
  <?php else: ?>
    <table>
      <tbody>
        <?php foreach ($checks as $check):
            $status = (string) ($check['status'] ?? 'fail');
            $label = (string) ($check['label'] ?? $check['name'] ?? '');
            $description = (string) ($check['description'] ?? $check['message'] ?? '');
            $variant = $bannerVariants[$status] ?? 'danger';
            $badgeLabel = $statusBadgeLabel[$status] ?? t('Fail');
        ?>
          <tr>
            <td>
              <strong><?php e(t($label)); ?></strong>
              <small class="app-status-description"><?php e(t($description)); ?></small>
            </td>
            <td class="app-status-badge-cell">
              <span class="badge" data-variant="<?php e($variant); ?>"><?php e($badgeLabel); ?></span>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  <?php endif; ?>
</div>

<div class="app-stats-table">
  <div class="app-stats-table-header">
    <small><?php e(t('Environment')); ?></small>
  </div>
  <table>
    <tbody>
      <tr>
        <th scope="row"><?php e(t('PHP version')); ?></th>
        <td><?php e($meta['php_version']); ?></td>
      </tr>
      <tr>
        <th scope="row"><?php e(t('Environment')); ?></th>
        <td><?php e($meta['environment']); ?></td>
      </tr>
    </tbody>
  </table>
</div>
