<?php
include('php/db_connect_localhost.php');
include('php/useful.php');

// Stats for the metrics grid
$stats = [];
$r = $conn->query("SELECT COUNT(*) AS n FROM ads");
$stats['ads'] = $r ? number_format($r->fetch_assoc()['n']) : '—';

$r = $conn->query("SELECT COUNT(DISTINCT id) AS n FROM vendors");
$stats['vendors'] = $r ? number_format($r->fetch_assoc()['n']) : '—';

$stats['industries'] = '12';
$stats['years'] = '42';

// Featured ad: rotate daily by seeding rand with the date
$r = $conn->query("SELECT COUNT(*) AS n FROM ads WHERE exemplar > 0");
$ad_total = $r ? $r->fetch_assoc()['n'] : 0;
$daily_offset = date('z') % max($ad_total, 1); // day-of-year offset
$r = $conn->query("SELECT a.id, a.vendor_id, a.transcription, e.path AS scan_path, c.vendor
  FROM ads a
  INNER JOIN images e ON a.exemplar = e.id
  INNER JOIN vendors c ON a.vendor_id = c.id
  WHERE a.exemplar > 0
  LIMIT 1 OFFSET $daily_offset");
$featured = $r ? $r->fetch_assoc() : null;

// Recently transcribed: 4 most recently modified ads with an exemplar
$r = $conn->query("SELECT a.id, a.transcription, a.last_modified, c.vendor, d.industry, e.path AS scan_path,
    SUBSTRING(MIN(img.date), 1, 4) AS year
  FROM ads a
  INNER JOIN images e ON a.exemplar = e.id
  INNER JOIN images img ON a.id = img.ad_id
  INNER JOIN vendors c ON a.vendor_id = c.id
  INNER JOIN industries d ON c.industry_id = d.id
  WHERE a.exemplar > 0 AND a.transcription != ''
  GROUP BY a.id
  ORDER BY a.last_modified DESC
  LIMIT 4");
$recents = [];
if ($r) { while ($row = $r->fetch_assoc()) { $recents[] = $row; } }

include('top.php');
?>
<main>

  <!-- HERO -->
  <section class="home-hero">

    <!-- Left: search column -->
    <div class="home-hero-left">
      <p class="eyebrow">A Digital Archive · Est. 2019</p>
      <h1 class="home-hero-headline">
        The commerce of the<br>
        <em>Woman's Exponent</em>,<br>
        1872&ndash;1914.
      </h1>
      <p class="home-hero-lede">
        Forty-two years of advertisements from Salt Lake City's foremost women's newspaper,
        fully transcribed and searchable. A window into the commercial life of the nineteeth-century American West.
      </p>
      <form class="search-rail" action="browse.php" method="GET">
        <label for="hero-search" class="sr-only">Search advertisements</label>
        <input type="search" id="hero-search" name="q" placeholder="Search <?= $stats['ads'] ?> advertisements&hellip;"
          value="<?= isset($_GET['q']) ? htmlspecialchars($_GET['q']) : '' ?>" autocomplete="off">
        <button type="submit">Search &rarr;</button>
      </form>
      <p class="search-suggestions">
        Try:
        <a href="browse.php?q=Z.C.M.I.">&ldquo;Z.C.M.I.&rdquo;</a>
        <a href="browse.php?q=tonic">tonic</a>
        <a href="browse.php?q=millinery">millinery</a>
        <a href="browse.php?q=piano">piano</a>
        <a href="browse.php?q=railway">railway</a>
      </p>
    </div>

    <!-- Right: at-a-glance + featured -->
    <div class="home-hero-right">
      <p class="eyebrow">At a Glance</p>
      <div class="metrics-grid">
        <div class="metric">
          <div class="metric-number"><?= $stats['ads'] ?></div>
          <div class="metric-label">Advertisements</div>
        </div>
        <div class="metric">
          <div class="metric-number"><?= $stats['vendors'] ?></div>
          <div class="metric-label">Distinct vendors</div>
        </div>
        <div class="metric">
          <div class="metric-number"><?= $stats['industries'] ?></div>
          <div class="metric-label">Industry categories</div>
        </div>
        <div class="metric">
          <div class="metric-number"><?= $stats['years'] ?></div>
          <div class="metric-label">Years of coverage</div>
        </div>
      </div>

      <?php if ($featured): ?>
      <hr class="featured-divider">
      <p class="eyebrow">Featured this week</p>
      <div class="featured-ad">
        <a href="ad.php?ad-id=<?= $featured['id'] ?>" class="featured-scan" aria-label="View ad by <?= htmlspecialchars($featured['vendor']) ?>">
          <img src="<?= htmlspecialchars($featured['scan_path']) ?>"
               alt="Advertisement by <?= htmlspecialchars($featured['vendor']) ?>">
        </a>
        <div class="featured-info">
          <div class="featured-label"><?= htmlspecialchars($featured['vendor']) ?></div>
          <div class="featured-headline">
            <a href="ad.php?ad-id=<?= $featured['id'] ?>">
              <?= htmlspecialchars(mb_strimwidth($featured['transcription'], 0, 80, '…')) ?>
            </a>
          </div>
        </div>
      </div>
      <?php endif; ?>
    </div>

  </section>

  <!-- RECENTLY TRANSCRIBED STRIP -->
  <?php if ($recents): ?>
  <section class="home-recents">
    <div class="strip-header">
      <h2>Recently transcribed</h2>
      <a href="browse.php" class="strip-link">Browse all &rarr;</a>
    </div>
    <div class="card-grid">
      <?php foreach ($recents as $ad): ?>
      <a href="ad.php?ad-id=<?= $ad['id'] ?>" class="aa-card">
        <div class="aa-card-scan">
          <img src="<?= htmlspecialchars($ad['scan_path']) ?>"
               alt="Advertisement by <?= htmlspecialchars($ad['vendor']) ?>, <?= htmlspecialchars($ad['year']) ?>"
               loading="lazy">
        </div>
        <div>
          <div class="aa-card-id">A-<?= str_pad($ad['id'], 4, '0', STR_PAD_LEFT) ?> · <?= htmlspecialchars(substr($ad['industry'], 0, 4)) ?>.</div>
          <div class="aa-card-headline"><?= htmlspecialchars(mb_strimwidth($ad['transcription'], 0, 60, '…')) ?></div>
          <div class="aa-card-meta"><?= htmlspecialchars($ad['vendor']) ?> · <?= htmlspecialchars($ad['year']) ?></div>
        </div>
      </a>
      <?php endforeach; ?>
    </div>
  </section>
  <?php endif; ?>

</main>
<?php include('bottom.html'); ?>
