<?php

declare(strict_types=1);

/**
 * Public member profile — "/u/{username}". Shows the member's reputation, level,
 * earned badges, and their questions + answers. Indexable when the member has
 * activity (real content), no-indexed when empty to avoid thin pages.
 */

require __DIR__ . '/_shell.php';

$b        = forum_base();
$username = (string) ($GLOBALS['__forum_user'] ?? '');

$u = $username !== '' ? dbRow(
    'SELECT id, username, display_name, bio, website, avatar_url, reputation, role, created_at, email
       FROM qa_users WHERE username = ?', [$username]
) : null;

if (!$u) {
    http_response_code(404);
    forum_head('Member not found — PPPHP Community', '<meta name="robots" content="noindex,follow">');
    echo '<div class="fx-empty"><i class="bi bi-person-x" style="font-size:34px;opacity:.4"></i>'
       . '<p>That member doesn\'t exist.</p>'
       . '<p><a class="fx-tag" href="' . $b . '/">← Back to all questions</a></p></div>';
    forum_foot();
    exit;
}

$uid  = (int) $u['id'];
$name = trim((string) $u['display_name']) !== '' ? (string) $u['display_name'] : (string) $u['username'];
$rep  = (int) $u['reputation'];
[$levelLabel, $levelClass] = forum_level($rep);

$questions = dbAll(
    'SELECT id, slug, title, vote_score, answer_count, accepted_answer_id, created_at
       FROM qa_questions WHERE author_id = ? AND deleted_at IS NULL
      ORDER BY created_at DESC LIMIT 25', [$uid]
);
$answers = dbAll(
    'SELECT a.vote_score, a.is_accepted, a.created_at, q.id AS qid, q.slug, q.title
       FROM qa_answers a JOIN qa_questions q ON q.id = a.question_id
      WHERE a.author_id = ? AND a.deleted_at IS NULL AND q.deleted_at IS NULL
      ORDER BY a.is_accepted DESC, a.vote_score DESC, a.created_at DESC LIMIT 25', [$uid]
);
$qCount = (int) (dbValue('SELECT COUNT(*) FROM qa_questions WHERE author_id = ? AND deleted_at IS NULL', [$uid]) ?? 0);
$aCount = (int) (dbValue('SELECT COUNT(*) FROM qa_answers   WHERE author_id = ? AND deleted_at IS NULL', [$uid]) ?? 0);
$accepted = (int) (dbValue('SELECT COUNT(*) FROM qa_answers WHERE author_id = ? AND is_accepted = 1 AND deleted_at IS NULL', [$uid]) ?? 0);
$hasActivity = ($qCount + $aCount) > 0;

// Earned badges (computed from activity + reputation milestones).
$badges = [];
$badges[] = ['star-fill', $levelLabel . ' · ' . number_format($rep) . ' rep', $rep >= 500 ? 'gold' : ''];
if ($qCount >= 1)  { $badges[] = ['patch-question-fill', 'Asked a question', '']; }
if ($qCount >= 10) { $badges[] = ['collection-fill', 'Prolific asker', '']; }
if ($aCount >= 1)  { $badges[] = ['chat-left-text-fill', 'Answered', '']; }
if ($aCount >= 10) { $badges[] = ['chat-square-quote-fill', 'Active answerer', '']; }
if ($accepted >= 1) { $badges[] = ['check-circle-fill', 'Accepted answer', 'gold']; }

$joined = $u['created_at'] ? date('F Y', strtotime((string) $u['created_at'])) : '';

$sch  = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$canon = isset($_SERVER['HTTP_HOST']) ? $sch . '://' . $_SERVER['HTTP_HOST'] . $b . '/u/' . rawurlencode((string) $u['username']) : '';
$meta = '<link rel="canonical" href="' . forum_e($canon) . '">'
      . '<meta name="description" content="' . forum_e($name . ' on the PPPHP community — ' . number_format($rep) . ' reputation, ' . $qCount . ' questions, ' . $aCount . ' answers.') . '">'
      . (!$hasActivity ? '<meta name="robots" content="noindex,follow">' : '');

forum_head($name . ' (@' . forum_e((string) $u['username']) . ') — PPPHP Community', $meta);
?>
<a class="fx-back" href="<?= $b ?>/"><i class="bi bi-arrow-left"></i> All questions</a>
<div class="fx-profhead">
  <?= forum_avatar(forum_initials($name), (string) ($u['email'] ?? ''), 160, 'fx-ava-prof') ?>
  <div class="fx-profinfo">
    <h1 class="fx-profname"><?= forum_e($name) ?> <span class="fx-lvl <?= $levelClass ?>" style="vertical-align:middle;font-size:12px"><?= forum_e($levelLabel) ?></span></h1>
    <p class="fx-profuser">@<?= forum_e((string) $u['username']) ?><?= $joined ? ' · <i class="bi bi-calendar3"></i> Joined ' . forum_e($joined) : '' ?></p>
    <?php if (trim((string) $u['bio']) !== ''): ?><p class="fx-profbio"><?= forum_e((string) $u['bio']) ?></p><?php endif; ?>
    <div class="fx-profstats">
      <div class="fx-profstat"><b><?= number_format($rep) ?></b><span>Reputation</span></div>
      <div class="fx-profstat"><b><?= number_format($qCount) ?></b><span>Questions</span></div>
      <div class="fx-profstat"><b><?= number_format($aCount) ?></b><span>Answers</span></div>
      <div class="fx-profstat"><b><?= number_format($accepted) ?></b><span>Accepted</span></div>
    </div>
  </div>
</div>

<div class="fx-badges">
  <?php foreach ($badges as [$ico, $lbl, $cls]): ?>
    <span class="fx-badge <?= $cls ?>"><i class="bi bi-<?= $ico ?>"></i> <?= forum_e($lbl) ?></span>
  <?php endforeach; ?>
</div>

<h2 class="fx-profsec-h">Questions (<?= number_format($qCount) ?>)</h2>
<?php if ($questions): ?>
<div class="fx-plist">
  <?php foreach ($questions as $r): $ok = ((int) $r['answer_count'] > 0 && !empty($r['accepted_answer_id'])); ?>
    <a class="fx-prow" href="<?= $b ?>/q/<?= (int) $r['id'] ?>/<?= forum_e($r['slug']) ?>">
      <span class="sc"><b><?= (int) $r['vote_score'] ?></b><span>votes</span></span>
      <span class="sc <?= $ok ? 'ok' : '' ?>"><b><?= (int) $r['answer_count'] ?><?= $ok ? ' ✓' : '' ?></b><span>answers</span></span>
      <span class="tt"><?= forum_e($r['title']) ?></span>
      <span class="dt"><?= forum_e(forum_ago($r['created_at'] ?? null)) ?></span>
    </a>
  <?php endforeach; ?>
</div>
<?php else: ?><p class="fx-sub">No questions yet.</p><?php endif; ?>

<h2 class="fx-profsec-h">Answers (<?= number_format($aCount) ?>)</h2>
<?php if ($answers): ?>
<div class="fx-plist">
  <?php foreach ($answers as $r): $acc = (int) $r['is_accepted'] === 1; ?>
    <a class="fx-prow" href="<?= $b ?>/q/<?= (int) $r['qid'] ?>/<?= forum_e($r['slug']) ?>">
      <span class="sc <?= $acc ? 'ok' : '' ?>"><b><?= (int) $r['vote_score'] ?><?= $acc ? ' ✓' : '' ?></b><span>score</span></span>
      <span class="tt"><?= forum_e($r['title']) ?></span>
      <span class="dt"><?= forum_e(forum_ago($r['created_at'] ?? null)) ?></span>
    </a>
  <?php endforeach; ?>
</div>
<?php else: ?><p class="fx-sub">No answers yet.</p><?php endif; ?>
<?php forum_foot(); ?>
