<?php
declare(strict_types=1);
$user=is_array($user??null)?$user:[];
$activity=is_array($activity??null)?$activity:['questions'=>[],'answers'=>[],'best_answers'=>[]];
$summary=is_array($summary??null)?$summary:['questions'=>0,'answers'=>0,'accepted_answers'=>0,'received_votes'=>0];
$userId=(int)($user['id']??0);$avatar=trim((string)($user['avatar_url']??''));if($avatar==='')$avatar=quaeriva_default_avatar_url($userId);
?>
<section class="content-section public-profile">
 <div class="container narrow-container">
  <header class="profile-hero">
   <img class="profile-avatar" src="<?= quaeriva_escape($avatar) ?>" alt="">
   <div><p class="eyebrow"><?= quaeriva_t($translator,'profile.public_title') ?></p><h1 dir="auto"><?= quaeriva_escape($user['name']??'') ?></h1><?php if(trim((string)($user['username']??''))!==''): ?><p class="profile-handle"><bdi>@<?= quaeriva_escape($user['username']) ?></bdi></p><?php endif; ?><?php if(trim((string)($user['bio']??''))!==''): ?><p dir="auto"><?= quaeriva_escape($user['bio']) ?></p><?php endif; ?></div>
  </header>
  <dl class="profile-grid">
   <div><dt><?= quaeriva_t($translator,'auth.points') ?></dt><dd><?= (int)($user['points']??0) ?></dd></div>
   <div><dt><?= quaeriva_t($translator,'profile.questions') ?></dt><dd><?= (int)($summary['questions']??0) ?></dd></div>
   <div><dt><?= quaeriva_t($translator,'profile.answers') ?></dt><dd><?= (int)($summary['answers']??0) ?></dd></div>
   <div><dt><?= quaeriva_t($translator,'profile.accepted_answers') ?></dt><dd><?= (int)($summary['accepted_answers']??0) ?></dd></div>
   <div><dt><?= quaeriva_t($translator,'profile.received_votes') ?></dt><dd><?= (int)($summary['received_votes']??0) ?></dd></div>
   <div><dt><?= quaeriva_t($translator,'profile.joined') ?></dt><dd><bdi><?= quaeriva_escape($user['created_at']??'') ?></bdi></dd></div>
  </dl>
  <?php $best=(array)($activity['best_answers']??[]); ?>
  <section class="profile-featured"><h2><?= quaeriva_t($translator,'profile.best_answers') ?></h2><?php if($best===[]): ?><p class="notice notice-muted"><?= quaeriva_t($translator,'profile.no_answers') ?></p><?php else: ?><div class="profile-answer-list"><?php foreach($best as $a):$answerQuery=(int)($a['answer_page']??1)>1?['answers_page'=>(int)$a['answer_page']]:[]; ?><article class="profile-answer<?= !empty($a['is_best'])?' profile-answer-accepted':'' ?>"><?php if(!empty($a['is_best'])): ?><span class="best-badge"><?= quaeriva_t($translator,'questions.best_answer') ?></span><?php endif; ?><a dir="auto" href="<?= quaeriva_escape(quaeriva_question_url((string)($a['question_slug']??''), (int)($a['question_id']??0),$answerQuery).'#answer-'.(int)($a['id']??0)) ?>"><?= quaeriva_escape($a['question_title']??'') ?></a><p dir="auto"><?= quaeriva_plain_text($a['body']??'') ?></p><small><?= (int)($a['votes_count']??0) ?> <?= quaeriva_t($translator,'profile.received_votes') ?></small></article><?php endforeach; ?></div><?php endif; ?></section>
  <div class="profile-activity">
   <section><h2><?= quaeriva_t($translator,'profile.questions') ?></h2><?php if(($activity['questions']??[])===[]): ?><p class="notice notice-muted"><?= quaeriva_t($translator,'profile.no_questions') ?></p><?php else: ?><ul class="activity-list"><?php foreach($activity['questions'] as $q): ?><li><a dir="auto" href="<?= quaeriva_escape(quaeriva_question_url((string)($q['slug']??''), (int)($q['id']??0))) ?>"><?= quaeriva_escape($q['title']??'') ?></a><small><?= (int)($q['answers_count']??0) ?> <?= quaeriva_t($translator,'common.answers') ?> · <?= (int)($q['votes_count']??0) ?> <?= quaeriva_t($translator,'profile.received_votes') ?></small></li><?php endforeach; ?></ul><?php endif; ?></section>
   <section><h2><?= quaeriva_t($translator,'profile.answers') ?></h2><?php if(($activity['answers']??[])===[]): ?><p class="notice notice-muted"><?= quaeriva_t($translator,'profile.no_answers') ?></p><?php else: ?><ul class="activity-list"><?php foreach($activity['answers'] as $a):$answerQuery=(int)($a['answer_page']??1)>1?['answers_page'=>(int)$a['answer_page']]:[]; ?><li><a dir="auto" href="<?= quaeriva_escape(quaeriva_question_url((string)($a['question_slug']??''), (int)($a['question_id']??0),$answerQuery).'#answer-'.(int)($a['id']??0)) ?>"><?= quaeriva_escape($a['question_title']??'') ?></a><p dir="auto"><?= quaeriva_plain_text($a['body']??'') ?></p></li><?php endforeach; ?></ul><?php endif; ?></section>
  </div>
 </div>
</section>
