<?php
$migrationStatus = is_array($status ?? null) ? $status : [];
$pending = (int) ($migrationStatus['pending'] ?? 0);
$total = (int) ($migrationStatus['total'] ?? 0);
$applied = (int) ($migrationStatus['applied'] ?? 0);
$pendingFiles = (array) ($migrationStatus['pending_files'] ?? []);
$logs = (array) ($runLogs ?? []);
?>

<section class="card">
  <h2 class="section-title"><?= $e($t('settings.database.title')) ?></h2>

  <?php if (!empty($errorMessage)): ?>
    <div class="alert alert--danger mt-12" role="alert">
      <?= $e((string) $errorMessage) ?>
    </div>
  <?php endif; ?>

  <?php if (!empty($successMessage)): ?>
    <div class="alert alert--success mt-12" role="status">
      <?= $e((string) $successMessage) ?>
    </div>
  <?php endif; ?>

  <div class="settings-grid mt-16">
    <div class="settings-stat">
      <span class="settings-stat__label"><?= $e($t('settings.database.stats.total')) ?></span>
      <strong class="settings-stat__value"><?= $e((string) $total) ?></strong>
    </div>
    <div class="settings-stat">
      <span class="settings-stat__label"><?= $e($t('settings.database.stats.applied')) ?></span>
      <strong class="settings-stat__value"><?= $e((string) $applied) ?></strong>
    </div>
    <div class="settings-stat">
      <span class="settings-stat__label"><?= $e($t('settings.database.stats.pending')) ?></span>
      <strong class="settings-stat__value"><?= $e((string) $pending) ?></strong>
    </div>
  </div>

  <?php if ($pending > 0): ?>
    <div class="alert alert--warning mt-16" role="status">
      <?= $e($t('settings.database.state.needs_update')) ?>
    </div>

    <form class="mt-16" action="/settings/database/migrate" method="post">
      <input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">
      <button type="submit" class="btn btn--primary"><?= $e($t('settings.database.actions.run_update')) ?></button>
    </form>
  <?php else: ?>
    <div class="alert alert--success mt-16" role="status">
      <?= $e($t('settings.database.state.up_to_date')) ?>
    </div>
  <?php endif; ?>
</section>

<section class="card mt-16">
  <h2 class="section-title"><?= $e($t('settings.database.pending_files_title')) ?></h2>
  <div class="table-wrap mt-12">
    <table class="table">
      <thead>
      <tr>
        <th><?= $e($t('settings.database.fields.filename')) ?></th>
      </tr>
      </thead>
      <tbody>
      <?php if (empty($pendingFiles)): ?>
        <tr>
          <td class="muted"><?= $e($t('settings.database.pending_files_empty')) ?></td>
        </tr>
      <?php else: ?>
        <?php foreach ($pendingFiles as $filename): ?>
          <tr>
            <td><?= $e((string) $filename) ?></td>
          </tr>
        <?php endforeach; ?>
      <?php endif; ?>
      </tbody>
    </table>
  </div>
</section>

<?php if (!empty($logs)): ?>
  <section class="card mt-16">
    <h2 class="section-title"><?= $e($t('settings.database.last_run_logs')) ?></h2>
    <pre class="settings-logs mt-12"><?php foreach ($logs as $line): ?><?= $e((string) $line) . "\n" ?><?php endforeach; ?></pre>
  </section>
<?php endif; ?>
