<!DOCTYPE html>
<html lang="it">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Profilo — Console MEB</title>
    <link rel="stylesheet" href="/static/style/style.css">
    <style>
        main { padding: 24px 30px; max-width: 600px; }
        main h2 { font-size: 1.1rem; margin-bottom: 20px; color: var(--text-secondary); font-weight: 500; }
        .field { margin-bottom: 20px; }
        .field label { display: block; font-size: 0.75rem; font-weight: 600; color: var(--text-secondary); margin-bottom: 4px; }
        .field p { font-size: 0.95rem; padding: 10px 14px; border: 1px solid var(--header-border); border-radius: var(--radius-md); }
        .field-empty { color: var(--text-tertiary); font-style: italic; }
    </style>
</head>

<body>
    <div class="header">
        <h1>Console MEB</h1>
        <div class="profile">
            <p id="username-label">{{ user.username }}</p>
            <form action="/api/auth/logout" method="post">
                <button type="submit">Logout</button>
            </form>
        </div>
    </div>

    <main>
        <h2>Profilo utente</h2>
        <div id="user-info">
            <p style="color:var(--text-tertiary)">Caricamento...</p>
        </div>
    </main>

    <script>
        function escapeHtml(str) {
            const d = document.createElement('div');
            d.appendChild(document.createTextNode(str || ''));
            return d.innerHTML;
        }

        function formatDate(iso) {
            if (!iso) return 'N/D';
            return new Date(iso).toLocaleDateString('it-IT', {
                day: 'numeric', month: 'long', year: 'numeric'
            });
        }

        async function loadUser() {
            try {
                const res = await fetch('/api/users/me');
                if (res.status === 401) { window.location.href = '/login'; return; }
                if (!res.ok) throw new Error();

                const user = await res.json();
                document.getElementById('username-label').textContent = user.username;
                document.getElementById('user-info').innerHTML = `
                    <div class="field">
                        <label>Username</label>
                        <p>${escapeHtml(user.username)}</p>
                    </div>
                    <div class="field">
                        <label>Account creato il</label>
                        <p>${formatDate(user.created_at)}</p>
                    </div>
                    <div class="field">
                        <label>Telegram ID</label>
                        <p>${user.telegram_id ? escapeHtml(user.telegram_id) : '<span class="field-empty">Non configurato</span>'}</p>
                    </div>
                    <div class="field">
                        <label>Sessioni</label>
                        <p><a href="/sessions">Gestisci sessioni →</a></p>
                    </div>
                `;
            } catch {
                document.getElementById('user-info').innerHTML =
                    '<p style="color:#dc2626">Errore nel caricamento del profilo.</p>';
            }
        }

        loadUser();
    </script>
</body>

</html>
