<?php require __DIR__.'/../layouts/header.php'; ?>
<div class="section-head">
  <div class="eyebrow">Profile</div>
  <h1><?= e($member['name']) ?> <?= paid_badge_html((int)($member['id'] ?? 0)) ?><?php if (!empty($member['age'])): ?>, <?= (int)$member['age'] ?><?php endif; ?></h1>
  <p><?= e($member['city'] ?? '') ?><?= !empty($member['country']) ? ', '.e($member['country']) : '' ?>
    <?php if (!empty($member['relationship_goal'])): ?> · <?= e($member['relationship_goal']) ?><?php endif; ?>
  </p>
</div>

<?php if (!empty($commonInterests)): ?>
<div class="spark-bar">
  <div class="spark-ico">🔗</div>
  <div>
    <strong><?= count($commonInterests) ?> interest<?= count($commonInterests)===1?'':'s' ?> in common</strong>
    <span><?= e(implode(' · ', $commonInterests)) ?></span>
  </div>
</div>
<?php endif; ?>

<section class="panel profile-view">
  <div class="photo-carousel" id="photo-carousel">
    <?php if ($photos): ?>
      <?php foreach ($photos as $i => $ph): ?>
        <div class="carousel-slide<?= $i===0 ? ' is-active' : '' ?>">
          <img src="<?= url($ph['path']) ?>" alt="Photo <?= $i+1 ?>" loading="<?= $i===0?'eager':'lazy' ?>">
        </div>
      <?php endforeach; ?>
      <?php if (count($photos) > 1): ?>
        <div class="carousel-dots">
          <?php foreach ($photos as $i => $ph): ?>
            <button type="button" class="dot<?= $i===0?' is-active':'' ?>" data-i="<?= $i ?>" aria-label="Photo <?= $i+1 ?>"></button>
          <?php endforeach; ?>
        </div>
        <button type="button" class="carousel-nav prev" aria-label="Previous">‹</button>
        <button type="button" class="carousel-nav next" aria-label="Next">›</button>
      <?php endif; ?>
    <?php else: ?>
      <div class="carousel-slide is-active photo-fallback-lg"><?= e(mb_substr($member['name'], 0, 1)) ?></div>
    <?php endif; ?>
  </div>

  <div class="profile-view-body">
    <?php if (!empty($member['bio'])): ?>
      <h3>About</h3>
      <p><?= nl2br(e($member['bio'])) ?></p>
    <?php endif; ?>
    <?php if (!empty($member['hobbies'])): ?>
      <p><strong>Hobbies:</strong> <?= e($member['hobbies']) ?></p>
    <?php endif; ?>
    <?php if (!empty($member['likes_text'])): ?>
      <p><strong>Likes:</strong> <?= e($member['likes_text']) ?></p>
    <?php endif; ?>
    <?php if (!empty($member['custom_interests'])): ?>
      <p><strong>Also into:</strong> <?= e($member['custom_interests']) ?></p>
    <?php endif; ?>
    <?php if (!empty($member['location_label'])): ?>
      <p><strong>Area:</strong> <?= e($member['location_label']) ?></p>
    <?php endif; ?>
    <?php if (!empty($member['show_phone']) && !empty($member['phone'])): ?>
      <p><strong>Phone:</strong> <?= e($member['phone']) ?></p>
    <?php endif; ?>


    <div class="meta-chips">
      <?php if (!empty($member['gender'])): ?><span class="tag"><?= e($member['gender']) ?></span><?php endif; ?>
      <?php if (!empty($member['orientation'])): ?><span class="tag"><?= e($member['orientation']) ?></span><?php endif; ?>
      <?php if (!empty($member['pronouns'])): ?><span class="tag"><?= e($member['pronouns']) ?></span><?php endif; ?>
      <?php if ($matched): ?><span class="badge">Matched</span><?php elseif ($liked): ?><span class="badge">You liked them</span><?php elseif ($theyLiked): ?><span class="badge">Likes you</span><?php endif; ?>
    </div>

    <?php if ($commonInterests || $otherInterests): ?>
      <h3>Interests & hobbies</h3>
      <div class="interest-grid">
        <?php foreach ($commonInterests as $name): ?>
          <span class="interest is-common">✓ <?= e($name) ?></span>
        <?php endforeach; ?>
        <?php foreach ($otherInterests as $name): ?>
          <span class="interest"><?= e($name) ?></span>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>

    <?php if ($prompts): ?>
      <h3>Ice breakers</h3>
      <div class="prompt-list">
        <?php foreach ($prompts as $pr): ?>
          <div class="prompt-card">
            <div class="prompt-q"><?= e($pr['prompt']) ?></div>
            <div class="prompt-a"><?= e($pr['answer']) ?></div>
          </div>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>
  </div>
</section>

<div class="profile-view-actions profile-view-sticky">
  <?php if ($matched): ?>
    <a class="btn" href="<?= url('chat?user='.(int)$member['id']) ?>">Open chat</a>
  <?php elseif ($liked): ?>
    <p class="muted" style="text-align:center;width:100%">You already liked <?= e($member['name']) ?>. If they like you back, it’s a match.</p>
    <a class="btn ghost" href="<?= url('discovery') ?>">Back to discover</a>
  <?php else: ?>
    <form method="post" action="<?= url('discover/action') ?>"><?= csrf() ?>
      <input type="hidden" name="target_id" value="<?= (int)$member['id'] ?>">
      <input type="hidden" name="action" value="pass">
      <button class="btn icon pass" type="submit" title="Pass">✕</button>
    </form>
    <?php if (!empty($plan) && has_plan($u['id'], 'Gold')): ?>
    <form method="post" action="<?= url('discover/action') ?>"><?= csrf() ?>
      <input type="hidden" name="target_id" value="<?= (int)$member['id'] ?>">
      <input type="hidden" name="action" value="superlike">
      <button class="btn icon super" type="submit" title="Super Like">★</button>
    </form>
    <?php endif; ?>
    <form method="post" action="<?= url('discover/action') ?>"><?= csrf() ?>
      <input type="hidden" name="target_id" value="<?= (int)$member['id'] ?>">
      <input type="hidden" name="action" value="like">
      <button class="btn icon like" type="submit" title="Like">♥</button>
    </form>
  <?php endif; ?>
</div>
<p style="text-align:center;margin-top:12px"><a class="btn ghost small" href="<?= url('discovery') ?>">← Back to discover</a></p>
<?php require __DIR__.'/../layouts/footer.php'; ?>
