<?php
/** @var array $currentUser */
/** @var Auth $auth */
/** @var I18n $i18n */
/** @var string $activeTheme */
/** @var string|null $error */
/** @var string|null $success */

$pageTitle = t('nav.profile');
include WEBROOT_PATH . '/includes/header.php';
?>

<h1><?= t('nav.profile') ?></h1>
<p><?= t('auth.username') ?>: <strong><?= htmlspecialchars($currentUser['username']) ?></strong>
   · <?= t('user.roles') ?>:
   <?php if (empty($currentUser['roles'])): ?>
       <em>—</em>
   <?php else: foreach ($currentUser['roles'] as $r): ?>
       <span class="badge <?= $r === ROLE_APP_ADMIN ? 'badge-warning' : 'badge-info' ?>"
             style="font-size:.72rem;margin-right:.2rem"><?= htmlspecialchars(t('role.' . $r)) ?></span>
   <?php endforeach; endif; ?>
</p>

<?php if ($error):   echo '<div class="alert alert-error">'   . htmlspecialchars($error)   . '</div>'; endif; ?>
<?php if ($success): echo '<div class="alert alert-success">' . htmlspecialchars($success) . '</div>'; endif; ?>

<div class="card-grid">

    <div class="card">
        <h2><?= t('user.language') ?> &amp; <?= t('user.theme') ?></h2>
        <form method="post" action="/profile">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars(Session::get('csrf_token')) ?>">
            <input type="hidden" name="action"     value="preferences">

            <div class="form-group">
                <label for="language"><?= t('user.language') ?></label>
                <select id="language" name="language">
                    <option value=""><?= t('settings.default_lang') ?> (<?= t('lang.' . $i18n->getLanguage()) ?>)</option>
                    <?php foreach (LANGUAGES as $code): ?>
                    <option value="<?= $code ?>"
                        <?= ($currentUser['language'] === $code) ? 'selected' : '' ?>>
                        <?= t('lang.' . $code) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="form-group">
                <label for="theme"><?= t('user.theme') ?></label>
                <select id="theme" name="theme">
                    <option value=""><?= t('settings.default_theme') ?></option>
                    <?php foreach (THEMES as $themeKey): ?>
                    <option value="<?= $themeKey ?>"
                        <?= ($currentUser['theme'] === $themeKey) ? 'selected' : '' ?>>
                        <?= t('theme.' . $themeKey) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <?php
            // Modules supprimables (admin > Modules) : ne pas fataler si absent.
            $_oraUrls   = WEBROOT_PATH . '/modules/oracle/lib/OracleUrls.php';
            $_userRoles = $currentUser['roles'] ?? [];
            $_hasOracle = is_file($_oraUrls) && (
                          in_array(ROLE_APP_ADMIN, $_userRoles, true)
                       || in_array('oracle_admin', $_userRoles, true)
                       || in_array('oracle_user',  $_userRoles, true));
            if ($_hasOracle):
                require_once $_oraUrls;
                $_curLanding = (string)($currentUser['oracle_landing']        ?? '');
                $_curFormat  = (string)($currentUser['oracle_report_format']  ?? 'html');
                if ($_curFormat === '') $_curFormat = 'html';
            ?>
            <hr style="margin:1rem 0;border:0;border-top:1px solid var(--color-border)">
            <h3 style="margin:0 0 .5rem;font-size:.92rem;color:var(--color-text-muted)">
                <?= t('user.oracle_prefs') ?>
            </h3>

            <div class="form-group">
                <label for="oracle_landing"><?= t('user.oracle_landing') ?></label>
                <select id="oracle_landing" name="oracle_landing">
                    <?php foreach (oracle_landing_pages() as $_path => $_labelKey): ?>
                    <option value="<?= htmlspecialchars($_path) ?>"
                        <?= ($_curLanding === $_path) ? 'selected' : '' ?>>
                        <?= htmlspecialchars(t($_labelKey)) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
                <small style="display:block;color:var(--color-text-muted);margin-top:.25rem;font-size:.78rem">
                    <?= t('user.oracle_landing_hint') ?>
                </small>
            </div>

            <div class="form-group">
                <label for="oracle_report_format"><?= t('user.oracle_report_format') ?></label>
                <select id="oracle_report_format" name="oracle_report_format">
                    <option value="html"   <?= $_curFormat === 'html'   ? 'selected' : '' ?>>
                        <?= t('user.oracle_format_html') ?>
                    </option>
                    <option value="active" <?= $_curFormat === 'active' ? 'selected' : '' ?>>
                        <?= t('user.oracle_format_active') ?>
                    </option>
                </select>
                <small style="display:block;color:var(--color-text-muted);margin-top:.25rem;font-size:.78rem">
                    <?= t('user.oracle_format_hint') ?>
                </small>
            </div>
            <?php endif; ?>

            <?php
            /* ── Préférences PostgreSQL ────────────────────────────────────
             *   Même principe que côté Oracle : page d'accueil après le choix
             *   d'une base. Réglable aussi en un clic depuis le bouton
             *   flottant de n'importe quelle page de rapport.            */
            $_pgUrls  = WEBROOT_PATH . '/modules/postgresql/lib/PostgresUrls.php';
            $_hasPg   = is_file($_pgUrls) && (
                        in_array(ROLE_APP_ADMIN, $_userRoles, true)
                     || in_array('postgresql_admin', $_userRoles, true)
                     || in_array('postgresql_user',  $_userRoles, true));
            if ($_hasPg):
                require_once $_pgUrls;
                $_curPgLanding = (string)($currentUser['postgresql_landing'] ?? '');
            ?>
            <hr style="margin:1rem 0;border:0;border-top:1px solid var(--color-border)">
            <h3 style="margin:0 0 .5rem;font-size:.92rem;color:var(--color-text-muted)">
                <?= t('user.postgresql_prefs') ?>
            </h3>

            <div class="form-group">
                <label for="postgresql_landing"><?= t('user.postgresql_landing') ?></label>
                <select id="postgresql_landing" name="postgresql_landing">
                    <?php foreach (pg_landing_pages() as $_path => $_labelKey): ?>
                    <option value="<?= htmlspecialchars($_path) ?>"
                        <?= ($_curPgLanding === $_path) ? 'selected' : '' ?>>
                        <?= htmlspecialchars(t($_labelKey)) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
                <small style="display:block;color:var(--color-text-muted);margin-top:.25rem;font-size:.78rem">
                    <?= t('user.postgresql_landing_hint') ?>
                </small>
            </div>
            <?php endif; ?>

            <div class="form-actions">
                <button type="submit" class="btn btn-primary"><?= t('user.save') ?></button>
            </div>
        </form>
    </div>

    <?php if ($currentUser['auth_method'] === AUTH_LOCAL): ?>
    <div class="card">
        <h2><?= t('auth.password') ?></h2>
        <form method="post" action="/profile">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars(Session::get('csrf_token')) ?>">
            <input type="hidden" name="action"     value="password">

            <div class="form-group">
                <label for="current_password"><?= t('auth.password') ?> *</label>
                <input type="password" id="current_password" name="current_password"
                       required autocomplete="current-password">
            </div>

            <div class="form-group">
                <label for="new_password"><?= t('auth.password_new') ?> *</label>
                <input type="password" id="new_password" name="new_password"
                       required minlength="8" autocomplete="new-password">
            </div>

            <div class="form-group">
                <label for="confirm_password"><?= t('auth.password_conf') ?> *</label>
                <input type="password" id="confirm_password" name="confirm_password"
                       required minlength="8" autocomplete="new-password">
            </div>

            <div class="form-actions">
                <button type="submit" class="btn btn-secondary"><?= t('msg.password_changed') ?></button>
            </div>
        </form>
    </div>
    <?php endif; ?>

</div>

<?php include WEBROOT_PATH . '/includes/footer.php'; ?>
