<?php $pageTitle = 'Dashboard'; require __DIR__ . '/layout/header.php'; ?>

<!-- Stats Row -->
<div class="row g-3 mb-4">
  <?php
  $cards = [
    ['label' => 'Projects',         'value' => $stats['projects'],         'icon' => 'briefcase',          'color' => 'primary'],
    ['label' => 'Topics',           'value' => $stats['topics'],           'icon' => 'tags',               'color' => 'info'],
    ['label' => 'Pending Topics',   'value' => $stats['topics_pending'],   'icon' => 'hourglass-split',    'color' => 'warning'],
    ['label' => 'Generated',        'value' => $stats['topics_generated'], 'icon' => 'check2-circle',      'color' => 'success'],
    ['label' => 'Content Pieces',   'value' => $stats['content_total'],    'icon' => 'collection',         'color' => 'secondary'],
    ['label' => 'Approved',         'value' => $stats['content_approved'], 'icon' => 'patch-check-fill',   'color' => 'success'],
    ['label' => 'Drafts',           'value' => $stats['content_draft'],    'icon' => 'pencil-square',      'color' => 'info'],
    ['label' => 'QA Failed',        'value' => $stats['content_failed'],   'icon' => 'x-octagon',          'color' => 'danger'],
  ];
  foreach ($cards as $card):
  ?>
  <div class="col-6 col-md-3">
    <div class="card border-0 shadow-sm h-100">
      <div class="card-body d-flex align-items-center gap-3">
        <div class="stat-icon bg-<?= $card['color'] ?>-subtle rounded-3 p-3">
          <i class="bi bi-<?= $card['icon'] ?> text-<?= $card['color'] ?> fs-4"></i>
        </div>
        <div>
          <div class="h3 fw-bold mb-0 lh-1"><?= number_format($card['value']) ?></div>
          <div class="text-muted small mt-1"><?= $card['label'] ?></div>
        </div>
      </div>
    </div>
  </div>
  <?php endforeach; ?>
</div>

<!-- Quick Actions -->
<div class="row g-3 mb-4">
  <div class="col-12">
    <div class="card border-0 shadow-sm">
      <div class="card-body">
        <h5 class="card-title fw-semibold mb-3"><i class="bi bi-lightning-fill text-warning me-2"></i>Quick Actions</h5>
        <div class="d-flex flex-wrap gap-2">
          <a href="<?= BASE_URL ?>/projects/create" class="btn btn-primary">
            <i class="bi bi-plus-lg me-1"></i>New Project
          </a>
          <a href="<?= BASE_URL ?>/topics/create" class="btn btn-outline-primary">
            <i class="bi bi-plus-lg me-1"></i>Add Topic
          </a>
          <a href="<?= BASE_URL ?>/topics/bulk-upload" class="btn btn-outline-secondary">
            <i class="bi bi-upload me-1"></i>Bulk Upload Topics
          </a>
          <a href="<?= BASE_URL ?>/content/generate" class="btn btn-success">
            <i class="bi bi-lightning me-1"></i>Generate Content
          </a>
          <a href="<?= BASE_URL ?>/content" class="btn btn-outline-secondary">
            <i class="bi bi-collection me-1"></i>View Library
          </a>
        </div>
      </div>
    </div>
  </div>
</div>

<!-- How It Works -->
<div class="row g-3 mb-4">
  <div class="col-12">
    <div class="card border-0 shadow-sm">
      <div class="card-body">
        <h5 class="card-title fw-semibold mb-3"><i class="bi bi-diagram-3 text-primary me-2"></i>Content Flow</h5>
        <div class="d-flex align-items-center flex-wrap gap-2">
          <?php
          $steps = [
            ['num' => 1, 'label' => 'Create Project', 'sub' => 'Brand + SEO rules', 'icon' => 'briefcase'],
            ['num' => 2, 'label' => 'Add Topics',     'sub' => 'Manual or CSV',      'icon' => 'tags'],
            ['num' => 3, 'label' => 'Select & Generate', 'sub' => 'One click',       'icon' => 'lightning'],
            ['num' => 4, 'label' => 'QA Check',       'sub' => 'Auto validation',    'icon' => 'shield-check'],
            ['num' => 5, 'label' => 'Approve & Export', 'sub' => 'HTML/Word/JSON',   'icon' => 'box-arrow-up'],
          ];
          foreach ($steps as $i => $step):
          ?>
          <div class="flow-step d-flex align-items-center gap-2">
            <div class="flow-step-badge bg-primary text-white rounded-circle d-flex align-items-center justify-content-center fw-bold"
                 style="width:32px;height:32px;font-size:13px;flex-shrink:0;">
              <?= $step['num'] ?>
            </div>
            <div>
              <div class="fw-semibold lh-1" style="font-size:13px;"><?= $step['label'] ?></div>
              <div class="text-muted" style="font-size:11px;"><?= $step['sub'] ?></div>
            </div>
            <?php if ($i < count($steps) - 1): ?>
            <i class="bi bi-chevron-right text-muted ms-2"></i>
            <?php endif; ?>
          </div>
          <?php endforeach; ?>
        </div>
      </div>
    </div>
  </div>
</div>

<!-- Recent Content -->
<div class="card border-0 shadow-sm">
  <div class="card-header bg-white d-flex align-items-center justify-content-between py-3">
    <h5 class="mb-0 fw-semibold"><i class="bi bi-clock-history text-secondary me-2"></i>Recent Content</h5>
    <a href="<?= BASE_URL ?>/content" class="btn btn-sm btn-outline-primary">View All</a>
  </div>
  <div class="card-body p-0">
    <?php if (empty($recentContent)): ?>
    <div class="text-center text-muted py-5">
      <i class="bi bi-inbox fs-1 d-block mb-2"></i>
      No content generated yet. <a href="<?= BASE_URL ?>/content/generate">Generate your first piece.</a>
    </div>
    <?php else: ?>
    <div class="table-responsive">
      <table class="table table-hover mb-0">
        <thead class="table-light">
          <tr>
            <th>Title</th>
            <th>Business</th>
            <th>Page Type</th>
            <th>Words</th>
            <th>Status</th>
            <th>Date</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($recentContent as $row): ?>
          <tr>
            <td class="fw-medium"><?= htmlspecialchars(mb_strimwidth($row['title'], 0, 55, '…')) ?></td>
            <td><span class="text-muted small"><?= htmlspecialchars($row['business_name']) ?></span></td>
            <td><span class="badge bg-secondary-subtle text-secondary"><?= htmlspecialchars($row['page_type_name']) ?></span></td>
            <td><?= number_format($row['word_count'] ?? 0) ?></td>
            <td><?php
              $sc2 = ['draft'=>'warning','approved'=>'success','qa_failed'=>'danger','published'=>'primary'][$row['status']] ?? 'secondary';
              echo '<span class="badge bg-' . $sc2 . '-subtle text-' . $sc2 . '">' . htmlspecialchars(str_replace('_',' ',ucfirst($row['status']))) . '</span>';
            ?></td>
            <td class="text-muted small"><?= date('M j, Y', strtotime($row['created_at'])) ?></td>
            <td><a href="<?= BASE_URL ?>/content/<?= $row['id'] ?>" class="btn btn-sm btn-outline-primary">View</a></td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
    <?php endif; ?>
  </div>
</div>

<?php require __DIR__ . '/layout/footer.php'; ?>
