<?php declare(strict_types=1); ?>
<h1>Android Cast Builder</h1>
<p class="muted">Docker CI for APK baking, OTA staging, and pipeline history.</p>

<section class="card card--lift" style="margin-bottom:16px">
  <h2>Ecosystem health</h2>
  <ul id="build-health-list">
    <li>Docker: <strong id="build-health-docker"><?= h($health['docker'] ?? 'unknown') ?></strong></li>
    <li>CI image version: <code><?= h($health['ci_version'] ?? '') ?></code></li>
    <li>Running builds: <span id="build-health-running"><?= (int) ($health['running'] ?? 0) ?></span></li>
    <li>Passed (7d): <?= (int) ($health['passed_7d'] ?? 0) ?> · Failed (7d): <?= (int) ($health['failed_7d'] ?? 0) ?></li>
  </ul>
</section>

<section class="card card--lift" id="build-trigger-panel">
  <h2>Run pipeline</h2>
  <form id="build-trigger-form" class="build-form">
    <div class="build-form-grid">
      <label>Git ref (branch / tag / commit)<input name="git_ref" placeholder="next" value="next"></label>
      <label>Gradle task<input name="gradle_task" value="assembleDebug"></label>
      <label>OTA channel<select name="ota_channel">
        <option value="staging" selected>staging</option>
        <option value="dev">dev</option>
        <option value="nightly">nightly</option>
        <option value="stable">stable / prod</option>
      </select></label>
    </div>
    <div class="build-form-checks">
      <label><input type="checkbox" name="run_tests" checked> Unit tests</label>
      <label><input type="checkbox" name="run_native" checked> Native codecs</label>
      <label><input type="checkbox" name="run_apk" checked> APK output</label>
      <label><input type="checkbox" name="auto_ota"> Create OTA artifacts</label>
      <label><input type="checkbox" name="auto_deploy"> Publish OTA to mount</label>
    </div>
    <button type="submit" class="btn btn-primary">Start build</button>
    <span id="build-trigger-status" class="muted"></span>
  </form>
</section>

<section class="card card--lift">
  <h2>Recent builds (last 10)</h2>
  <div class="reports-table-wrap">
    <table class="data-table" id="builds-recent-table">
      <thead>
        <tr>
          <th class="build-col-controls" aria-label="Job controls"></th>
          <th>Code</th>
          <th>Status</th>
          <th>Phase</th>
          <th>Branch/ref</th>
          <th>Channel</th>
          <th>Created</th>
        </tr>
      </thead>
      <tbody id="builds-recent-tbody">
        <?php foreach ($builds as $b):
          $bStatus = (string) ($b['status'] ?? '');
          $isActive = in_array($bStatus, ['running', 'queued'], true);
        ?>
        <tr data-build-id="<?= (int) $b['id'] ?>" data-build-status="<?= h($bStatus) ?>">
          <td class="build-row-controls">
            <div class="build-transport-actions">
              <button type="button" class="build-transport-btn build-transport-play" data-build-id="<?= (int) $b['id'] ?>" data-build-status="<?= h($bStatus) ?>" title="Re-run job" aria-label="Re-run build <?= h($b['build_code']) ?>"></button>
              <button type="button" class="build-transport-btn build-transport-stop" data-build-id="<?= (int) $b['id'] ?>" data-build-status="<?= h($bStatus) ?>" title="Stop job" aria-label="Stop build <?= h($b['build_code']) ?>"<?= $isActive ? '' : ' disabled' ?>></button>
            </div>
          </td>
          <td><a href="<?= h(Auth::basePath()) ?>/?view=build&id=<?= (int) $b['id'] ?>"><?= h($b['build_code']) ?></a></td>
          <td>
            <span class="build-status-cell-inner" data-build-status-cell="<?= (int) $b['id'] ?>">
              <?= build_status_icon($bStatus) ?>
              <span class="build-status-label"><?= h($bStatus) ?></span>
            </span>
          </td>
          <td class="build-phase-cell" data-build-phase-cell="<?= (int) $b['id'] ?>"><?= h($b['phase']) ?></td>
          <td><?= h($b['git_ref'] ?? $b['branch'] ?? '—') ?></td>
          <td><?= h($b['ota_channel'] ?? '—') ?></td>
          <td><?= h($b['created_at'] ?? '') ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</section>
