{% extends 'layouts/base.html.twig' %}

{% block title %}{{ trans('profile.title') }} — {{ site.name }}{% endblock %}

{% block content %}
    <section class="page-header">
        <h1>{{ trans('profile.title') }}</h1>
        <p class="muted">{{ trans('profile.signed_in_as') }} <strong>{{ user.username }}</strong> · <a href="/user/{{ user.username }}">{{ trans('profile.public_profile') }}</a></p>
    </section>

    <section class="form-section">
        <h2>{{ trans('profile.section_profile') }}</h2>
        <form method="post" action="/profile/save" class="form form-wide">
            {{ csrf_field() }}
            <div class="profile-avatar-row">
                {% include 'partials/avatar.twig' with { username: user.username, avatar_src: avatar_src, avatar_pending_src: avatar_pending_src|default(''), avatar_hue: user.avatar_hue|default(210), size: 64 } %}
                <div>
                    <p class="muted">
                        {% if site.use_gravatar %}
                            {{ trans('profile.avatar_gravatar')|replace({'Gravatar': '<a href="https://gravatar.com" rel="noopener noreferrer">Gravatar</a>'})|raw }}
                        {% else %}
                            {{ trans('profile.avatar_identicon') }}
                        {% endif %}
                    </p>
                </div>
            </div>
            <label>
                {{ trans('profile.bio') }}
                <textarea name="bio" rows="3" maxlength="{{ input_limits.bio_max }}">{{ bio }}</textarea>
            </label>
            {% for html in plugin_profile_form_html|default([]) %}
                {{ html|raw }}
            {% endfor %}
            <button type="submit" class="btn btn-primary">{{ trans('profile.save_profile') }}</button>
        </form>
    </section>

    {% if direct_messages_enabled|default(false) %}
    <section class="form-section">
        <h2>{{ trans('profile.section_messages') }}</h2>
        <p class="muted">{{ trans('profile.messages_lead') }}</p>
        <form method="post" action="/profile/accept-messages" class="form form-wide">
            {{ csrf_field() }}
            <label class="checkbox">
                <input type="checkbox" name="accept_messages" value="1"{% if accept_messages %} checked{% endif %}>
                {{ trans('profile.accept_messages') }}
            </label>
            <button type="submit" class="btn btn-primary">{{ trans('profile.save_message_pref') }}</button>
        </form>
    </section>
    {% endif %}

    <section class="form-section">
        <h2>{{ trans('profile.appearance') }}</h2>
        {% include 'partials/theme_selector.html.twig' %}
        <p class="muted theme-selector-hint">{{ trans('profile.theme_apply_hint') }}</p>
        <form method="post" action="/profile/locale" class="form form-wide" style="margin-top: 1rem;">
            {{ csrf_field() }}
            <label>
                {{ trans('profile.language') }}
                <select name="locale">
                    {% for code, meta in locale_catalog %}
                        <option value="{{ code }}"{% if user_locale == code %} selected{% endif %}>{{ meta.native }} ({{ meta.name }})</option>
                    {% endfor %}
                </select>
            </label>
            <button type="submit" class="btn btn-primary">{{ trans('profile.save_language') }}</button>
        </form>
    </section>

    {% if mail_enabled %}
    <section class="form-section">
        <h2>{{ trans('profile.section_email') }}</h2>
        <p class="muted">{{ trans('profile.current_email') }} <strong>{{ user.email }}</strong></p>
        <form method="post" action="/profile/email-change" class="form form-wide">
            {{ csrf_field() }}
            <label>
                {{ trans('profile.new_email') }}
                <input type="email" name="new_email" required autocomplete="email" maxlength="{{ input_limits.email_max }}">
            </label>
            <label>
                {{ trans('profile.current_password_required') }}
                <input type="password" name="current_password" required autocomplete="current-password">
            </label>
            <p class="muted">{{ trans('profile.email_change_lead')|replace({'new': '<strong>new</strong>'})|raw }}</p>
            <button type="submit" class="btn btn-primary">{{ trans('profile.request_email_change') }}</button>
        </form>
    </section>

    <section class="form-section">
        <h2>{{ trans('profile.section_email_notify') }}</h2>
        <p class="muted">{{ trans('profile.email_notify_lead') }}</p>
        <form method="post" action="/profile/notify-email" class="form form-wide">
            {{ csrf_field() }}
            <label class="checkbox">
                <input type="checkbox" name="notify_email" value="1"{% if notify_email %} checked{% endif %}>
                {{ trans('profile.notify_email') }}
            </label>
            <button type="submit" class="btn btn-primary">{{ trans('profile.save_notify_pref') }}</button>
        </form>
    </section>
    {% endif %}

    <section class="form-section">
        <h2>{{ trans('profile.section_security') }}</h2>
        <p><a href="/profile/2fa" class="btn btn-small" data-account-panel>{{ trans('profile.two_factor_title') }}</a></p>
    </section>

    <section class="form-section">
        <h2>{{ trans('profile.section_password') }}</h2>
        <form method="post" action="/profile/password" class="form form-wide">
            {{ csrf_field() }}
            <label>
                {{ trans('profile.current_password') }}
                <input type="password" name="current_password" required autocomplete="current-password">
            </label>
            <label>
                {{ trans('auth.new_password') }}
                <input type="password" name="new_password" required autocomplete="new-password">
            </label>
            <label>
                {{ trans('profile.confirm_new_password') }}
                <input type="password" name="new_password_confirm" required autocomplete="new-password">
            </label>
            <button type="submit" class="btn btn-primary">{{ trans('auth.update_password') }}</button>
        </form>
    </section>

    <section class="form-section">
        <h2>{{ trans('profile.section_oauth') }}</h2>
        <p class="muted">{{ trans('profile.oauth_lead') }}</p>
        {% if authorized_apps is empty %}
            <p class="muted">{{ trans('profile.oauth_none') }}</p>
        {% else %}
            <ul class="oauth-app-list">
                {% for app in authorized_apps %}
                    <li class="oauth-app-item">
                        <div class="oauth-app-details">
                            <strong>{{ app.client_name }}</strong>
                            <span class="muted">{{ trans('profile.oauth_authorized') }} {{ app.authorized_at|format_datetime }}</span>
                            <ul class="oauth-app-scopes">
                                {% for scope in app.scopes %}
                                    <li>{{ app.scope_labels[scope] ?? scope }}</li>
                                {% endfor %}
                            </ul>
                        </div>
                        <form class="inline-form" method="post" action="/profile/oauth-apps/{{ app.client_id }}/revoke">
                            {{ csrf_field() }}
                            <button type="submit" class="btn btn-small btn-icon oauth-app-revoke" title="{{ trans('profile.oauth_revoke') }}" aria-label="{{ trans('profile.oauth_revoke') }} {{ app.client_name }}">
                                {% include 'partials/icon.twig' with { name: 'unlink' } %}
                            </button>
                        </form>
                    </li>
                {% endfor %}
            </ul>
        {% endif %}
    </section>

    <section class="form-section">
        <h2>{{ trans('profile.section_sessions') }}</h2>
        {% if sessions is empty %}
            <p class="muted">{{ trans('profile.sessions_none') }}</p>
        {% else %}
            <ul class="session-list">
                {% for session in sessions %}
                    <li>
                        <span>{{ session.user_agent|slice(0, 60) }}</span>
                        <span class="muted">{{ session.ip_address }} · {{ session.last_seen_at|format_datetime }}</span>
                        {% if session.id == current_session_id %}
                            <span class="badge">{{ trans('profile.session_current') }}</span>
                        {% else %}
                            <form class="inline-form" method="post" action="/profile/sessions/{{ session.id }}/revoke">
                                {{ csrf_field() }}
                                <button type="submit" class="link-button">{{ trans('profile.session_revoke') }}</button>
                            </form>
                        {% endif %}
                    </li>
                {% endfor %}
            </ul>
            <form method="post" action="/profile/sessions/revoke-all" class="inline-form">
                {{ csrf_field() }}
                <button type="submit" class="btn btn-small">{{ trans('profile.sessions_revoke_all') }}</button>
            </form>
        {% endif %}
    </section>

    <section class="form-section">
        <h2>{{ trans('profile.section_export') }}</h2>
        <p><a href="/profile/export" class="btn btn-small" data-account-bypass>{{ trans('profile.export_download') }}</a></p>
    </section>

    <section class="form-section danger-zone">
        <h2>{{ trans('profile.section_delete') }}</h2>
        {% if anonymise_posts_on_delete %}
            <p class="muted">{{ trans('profile.delete_anonymise')|replace({'[deleted]': '<code>[deleted]</code>'})|raw }}</p>
        {% else %}
            <p class="muted">{{ trans('profile.delete_retain')|replace({'[deleted]': '<code>[deleted]</code>'})|raw }}</p>
        {% endif %}
        <form method="post" action="/profile/delete" class="form">
            {{ csrf_field() }}
            <label>
                {{ trans('profile.delete_confirm_password') }}
                <input type="password" name="password" required autocomplete="current-password">
            </label>
            <button type="submit" class="btn btn-danger">{{ trans('profile.delete_account') }}</button>
        </form>
    </section>
{% endblock %}