<?php
declare(strict_types=1);
$user=is_array($user??null)?$user:[];
$siteName = trim((string) ($site['name'] ?? ''));
if ($siteName === '') {
    $siteName = trim($translator->get('app.name'));
}
?>
<section class="content-section">
 <div class="container narrow-container profile-card">
  <p class="eyebrow"><?= quaeriva_t($translator,'auth.profile') ?></p>
  <h1><?= quaeriva_t($translator,'profile.settings_title') ?></h1>
  <p class="section-lede"><?= quaeriva_t($translator,'profile.settings_intro') ?></p>
  <?php if(($notice??'')!==''): ?><div class="notice" role="status"><?= quaeriva_escape($notice) ?></div><?php endif; ?>
  <?php if(($error??'')!==''): ?><div class="notice notice-error" role="alert"><?= quaeriva_escape($error) ?></div><?php endif; ?>
  <form method="post" action="<?= quaeriva_escape(quaeriva_url('settings/profile')) ?>" class="question-form">
   <input type="hidden" name="_csrf" value="<?= quaeriva_escape($csrfToken??'') ?>">
   <div class="field-group"><label for="profile-name"><?= quaeriva_t($translator,'profile.display_name') ?></label><input id="profile-name" name="name" dir="auto" required maxlength="100" value="<?= quaeriva_escape($user['name']??'') ?>"></div>
   <div class="field-group"><label for="profile-username"><?= quaeriva_t($translator,'profile.username') ?></label><input id="profile-username" name="username" dir="auto" maxlength="40" value="<?= quaeriva_escape($user['username']??'') ?>"><p class="form-hint"><?= quaeriva_t($translator,'profile.username_hint') ?></p></div>
   <div class="field-group"><label for="profile-bio"><?= quaeriva_t($translator,'profile.bio') ?></label><textarea id="profile-bio" name="bio" dir="auto" maxlength="500" rows="5"><?= quaeriva_escape($user['bio']??'') ?></textarea></div>
   <div class="field-group"><label for="profile-avatar"><?= quaeriva_t($translator,'profile.avatar_url') ?></label><input id="profile-avatar" name="avatar_url" type="url" dir="ltr" maxlength="500" value="<?= quaeriva_escape($user['avatar_url']??'') ?>"><p class="form-hint"><?= quaeriva_t($translator,'profile.avatar_hint', ['site' => $siteName]) ?></p></div>
   <button class="button" type="submit"><?= quaeriva_t($translator,'common.save') ?></button>
  </form>
  <?php if(trim((string)($user['username']??''))!==''): ?><p><a class="text-link" href="<?= quaeriva_escape(quaeriva_url('u/'.rawurlencode((string)$user['username']))) ?>"><?= quaeriva_t($translator,'profile.view_public') ?></a></p><?php endif; ?>
  <p><a class="text-link" href="<?= quaeriva_escape(quaeriva_url('settings/api-tokens')) ?>"><?= quaeriva_t($translator,'api_tokens.title') ?></a></p>
  <dl class="profile-grid"><div><dt><?= quaeriva_t($translator,'auth.role') ?></dt><dd><bdi><?= quaeriva_escape($user['role']??'member') ?></bdi></dd></div><div><dt><?= quaeriva_t($translator,'auth.points') ?></dt><dd><?= (int)($user['points']??0) ?></dd></div><div><dt><?= quaeriva_t($translator,'auth.locale') ?></dt><dd><bdi><?= quaeriva_escape($user['preferred_locale']??$translator->locale()) ?></bdi></dd></div></dl>
  <form method="post" action="<?= quaeriva_escape(quaeriva_url('logout')) ?>"><input type="hidden" name="_csrf" value="<?= quaeriva_escape($csrfToken??'') ?>"><button class="button button-secondary" type="submit"><?= quaeriva_t($translator,'auth.logout') ?></button></form>
 </div>
</section>
