<?php
session_start();
include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/nav.php';

// Load Friends data
$friendsFile = __DIR__ . '/data/friends.json';
$friends = file_exists($friendsFile) ? json_decode(file_get_contents($friendsFile), true) : [];
if (!is_array($friends)) $friends = [];
?>

<main class="content-area">

  <div class="smurf-divider"></div>

  <div class="content-wrapper">

    <section class="content-card center-text">
      <h1>🤝 Our Friends & Partners 🤝</h1>
      <h2>Supporting Smurfette’s Country Delights</h2>
    </section>

    <div class="friends-grid">
      <?php if (!empty($friends)): ?>
        <?php foreach ($friends as $friend): ?>
          <?php
            $name = $friend['name'] ?? '';
            $desc = $friend['description'] ?? '';
            $link = $friend['link'] ?? ($friend['url'] ?? '');
            $img  = $friend['image'] ?? '';

            $src = '';
            if (!empty($img)) {
              if (preg_match('#^https?://#i', $img)) $src = $img;
              else $src = '/img/friends/' . ltrim($img, '/');
            }
          ?>
          <div class="content-card friend-card">

            <div class="friend-top">
              <div class="friend-image">
                <?php if ($src): ?>
                  <img src="<?= htmlspecialchars($src) ?>" alt="<?= htmlspecialchars($name) ?>">
                <?php else: ?>
                  <div class="friend-image-fallback">🤝</div>
                <?php endif; ?>
              </div>

              <div class="friend-meta">
                <h3 class="friend-name"><?= htmlspecialchars($name) ?></h3>
                <?php if (!empty($desc)): ?>
                  <p class="friend-desc"><?= nl2br(htmlspecialchars($desc)) ?></p>
                <?php endif; ?>
              </div>
            </div>

            <?php if (!empty($link)): ?>
              <div class="friend-actions">
                <a class="friend-btn" href="<?= htmlspecialchars($link) ?>" target="_blank" rel="noopener">
                  Visit Website
                </a>
              </div>
            <?php endif; ?>

          </div>
        <?php endforeach; ?>
      <?php else: ?>
        <div class="content-card center-text">
          <p>No friends found.</p>
        </div>
      <?php endif; ?>
    </div>

  </div>

  <div class="smurf-divider"></div>

</main>

<?php include __DIR__ . '/includes/footer.php'; ?>
