<?php
$debug = is_array($debug ?? null) ? $debug : [];
$lastRequest = is_array($debug['last_request'] ?? null) ? $debug['last_request'] : [];
$state = is_array($debug['state'] ?? null) ? $debug['state'] : [];
$successCache = is_array($debug['success_cache'] ?? null) ? $debug['success_cache'] : [];
$failureCache = is_array($debug['failure_cache'] ?? null) ? $debug['failure_cache'] : [];
$selectionDebug = is_array($state['selection_debug'] ?? null) ? $state['selection_debug'] : [];
$statusSummary = (string)($state['error'] ?? '');
if ($statusSummary === '') {
    $statusSummary = !empty($state['has_update']) ? 'has_update' : 'ok';
}
?>

<style>
  .update-debug-card {
    border: 1px solid #e5edf8;
    border-radius: 16px;
    background: #fff;
    box-shadow: 0 10px 30px rgba(16, 24, 40, 0.04);
    margin-bottom: 16px;
  }
  .update-debug-card .card-header h4 {
    margin: 0;
    font-weight: 700;
  }
  .update-debug-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
  }
  .update-debug-box {
    border: 1px solid #e7eef9;
    border-radius: 12px;
    padding: 12px;
    background: #f8fbff;
  }
  .update-debug-label {
    display: block;
    margin-bottom: 4px;
    color: #54719a;
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .04em;
  }
  .update-debug-value {
    color: #17325d;
    font-size: 15px;
    font-weight: 600;
    word-break: break-all;
  }
  .update-debug-pre {
    white-space: pre-wrap;
    word-break: break-word;
    background: #0f172a;
    color: #dbeafe;
    border-radius: 12px;
    padding: 14px;
    font-size: 12px;
    line-height: 1.6;
    max-height: 480px;
    overflow: auto;
  }
  .update-debug-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin-bottom: 14px;
  }
  @media (max-width: 900px) {
    .update-debug-grid {
      grid-template-columns: 1fr;
    }
  }
</style>

<div class="card update-debug-card">
  <div class="card-header">
    <h4><?= e(app_zh('更新链路调试')) ?></h4>
  </div>
  <div class="card-body">
    <div class="update-debug-actions">
      <a class="btn btn-default" href="<?= e_url(base_url('admin/update-debug')) ?>"><?= e(app_zh('刷新页面')) ?></a>
      <a class="btn btn-primary" href="<?= e_url(base_url('admin/update-debug?refresh=1')) ?>"><?= e(app_zh('强制重新拉取')) ?></a>
      <a class="btn btn-default" href="<?= e_url(base_url('admin/system')) ?>"><?= e(app_zh('返回系统设置')) ?></a>
    </div>

    <div class="update-debug-grid">
      <div class="update-debug-box">
        <span class="update-debug-label">Update API URL</span>
        <div class="update-debug-value"><?= e((string)($debug['update_api_url'] ?? '')) ?></div>
      </div>
      <div class="update-debug-box">
        <span class="update-debug-label"><?= e(app_zh('当前版本')) ?></span>
        <div class="update-debug-value"><?= e((string)($app_version ?? '0.0.0')) ?></div>
      </div>
      <div class="update-debug-box">
        <span class="update-debug-label"><?= e(app_zh('当前域名')) ?></span>
        <div class="update-debug-value"><?= e((string)($debug['domain'] ?? '')) ?></div>
      </div>
      <div class="update-debug-box">
        <span class="update-debug-label"><?= e(app_zh('最近一次更新状态')) ?></span>
        <div class="update-debug-value"><?= e($statusSummary) ?></div>
      </div>
      <div class="update-debug-box">
        <span class="update-debug-label"><?= e(app_zh('上次 download_url')) ?></span>
        <div class="update-debug-value"><?= e((string)($state['download_url'] ?? '')) ?></div>
      </div>
      <div class="update-debug-box">
        <span class="update-debug-label"><?= e(app_zh('选版来源')) ?></span>
        <div class="update-debug-value"><?= e((string)($state['selection_source'] ?? '')) ?></div>
      </div>
      <div class="update-debug-box">
        <span class="update-debug-label"><?= e(app_zh('是否命中缓存')) ?></span>
        <div class="update-debug-value"><?= !empty($state['cache_hit']) ? e(app_zh('是')) : e(app_zh('否')) ?></div>
      </div>
    </div>
  </div>
</div>

<div class="card update-debug-card">
  <div class="card-header">
    <h4><?= e(app_zh('缓存与状态快照')) ?></h4>
  </div>
  <div class="card-body">
    <div class="update-debug-pre"><?= e(json_encode([
        'success_cache' => $successCache,
        'failure_cache' => $failureCache,
        'state' => $state,
    ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) ?></div>
  </div>
</div>

<div class="card update-debug-card">
  <div class="card-header">
    <h4><?= e(app_zh('选版详情')) ?></h4>
  </div>
  <div class="card-body">
    <div class="update-debug-pre"><?= e(json_encode($selectionDebug, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) ?></div>
  </div>
</div>

<div class="card update-debug-card">
  <div class="card-header">
    <h4><?= e(app_zh('最近一次请求结果')) ?></h4>
  </div>
  <div class="card-body">
    <div class="update-debug-pre"><?= e(json_encode($lastRequest, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) ?></div>
  </div>
</div>
