<?php
session_start();
require_once 'config.php';
require_once 'functions.php';
requireLogin();
$user = getConnectedUser($pdo);
requireAdmin($user);

$msg = '';

// S'assurer que config_value est TEXT
try { $pdo->exec("ALTER TABLE admin_config MODIFY COLUMN config_value TEXT DEFAULT NULL"); } catch(Exception $e){}

$termGroups = [
    'Gages' => [
        'terme_gage'        => ['label'=>'Gage (singulier)',       'default'=>'Gage'],
        'terme_gages'       => ['label'=>'Gage (pluriel)',          'default'=>'Gages'],
        'terme_lancer_gage' => ['label'=>'Action lancer',          'default'=>'Lancer un gage'],
        'terme_gager'       => ['label'=>'Verbe gager',            'default'=>'Gager'],
        'terme_gageur'      => ['label'=>'Rôle gageur',            'default'=>'Gageur'],
        'terme_lanceur'     => ['label'=>'Rôle lanceur',           'default'=>'Lanceur'],
    ],
    'Monnaie & Points' => [
        'terme_jeton'         => ['label'=>'Jeton (singulier)',    'default'=>'Jeton'],
        'terme_jetons'        => ['label'=>'Jeton (pluriel)',      'default'=>'Jetons'],
        'terme_jeton_symbol'  => ['label'=>'Symbole jeton',       'default'=>'◆'],
        'terme_points'        => ['label'=>'Points honneur',      'default'=>'Points d\'honneur'],
        'terme_honneur'       => ['label'=>'Honneur (mot)',        'default'=>'Honneur'],
        'terme_pts_inter'     => ['label'=>'Points interaction',   'default'=>'Points d\'interaction'],
        'terme_interaction'   => ['label'=>'Interaction (mot)',    'default'=>'Interaction'],
    ],
    'Fiabilité' => [
        'terme_fiable'        => ['label'=>'Fiable',              'default'=>'Fiable'],
        'terme_fiabilite'     => ['label'=>'Fiabilité',           'default'=>'Fiabilité'],
        'terme_score_fidelite'=> ['label'=>'Score de fiabilité',  'default'=>'Score de fiabilité'],
    ],
    'Cercles & Rôles' => [
        'terme_cercle'   => ['label'=>'Cercle (singulier)',        'default'=>'Cercle'],
        'terme_cercles'  => ['label'=>'Cercle (pluriel)',          'default'=>'Cercles'],
        'terme_oracle'   => ['label'=>'Oracle (rôle modérateur)',  'default'=>'Oracle'],
        'terme_membre'   => ['label'=>'Membre (singulier)',        'default'=>'Membre'],
        'terme_membres'  => ['label'=>'Membre (pluriel)',          'default'=>'Membres'],
    ],
    'Site' => [
        'site_nom'      => ['label'=>'Nom du site',               'default'=>'JeTeGageQue'],
        'site_slogan'   => ['label'=>'Slogan',                    'default'=>'La plateforme de gages entre amis'],
        'site_version'  => ['label'=>'Version affichée',          'default'=>'v2.0'],
    ],
];

if ($_SERVER['REQUEST_METHOD']==='POST' && isset($_POST['save_terms'])) {
    foreach ($termGroups as $grp => $terms) {
        foreach ($terms as $key => $info) {
            $val = trim($_POST[$key] ?? $info['default']);
            if ($val === '') $val = $info['default'];
            $pdo->prepare("INSERT INTO admin_config (config_key,config_value) VALUES (?,?) ON DUPLICATE KEY UPDATE config_value=?")
                ->execute([$key, $val, $val]);
        }
    }
    $msg = "Terminologie sauvegardée ! Les changements s'affichent immédiatement.";
}

// Lire valeurs actuelles
$current = [];
foreach ($termGroups as $grp => $terms) {
    foreach (array_keys($terms) as $key) {
        try {
            $r = $pdo->prepare("SELECT config_value FROM admin_config WHERE config_key=?");
            $r->execute([$key]);
            $v = $r->fetchColumn();
            $current[$key] = $v !== false ? $v : $terms[$key]['default'];
        } catch(Exception $e) { $current[$key] = $terms[$key]['default']; }
    }
}

include 'header.php';
?>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<div class="container-fluid mt-3 mb-5" style="max-width:900px;">
    <div class="d-flex align-items-center gap-3 mb-4">
        <a href="admin.php" class="btn btn-outline-secondary btn-sm"><i class="fa fa-arrow-left fa-xs me-1"></i>Admin</a>
        <h4 class="oracle-text text-info mb-0" style="letter-spacing:2px;"><i class="fa fa-language me-2"></i>TERMINOLOGIE DU SITE</h4>
    </div>

    <?php if ($msg): ?>
    <div class="alert alert-success py-2 small"><?= htmlspecialchars($msg) ?></div>
    <?php endif; ?>

    <div class="alert alert-info py-2 mb-3" style="font-size:0.72rem;background:rgba(13,202,240,0.08);border-color:#0dcaf0;">
        <i class="fa fa-circle-info me-1"></i>
        Modifiez ces termes pour personnaliser le vocabulaire du site. Exemple : remplacer "Gage" par "Défi", "Jeton" par "Pièce", "Oracle" par "Arbitre", etc. Les changements s'appliquent partout où le code utilise <code>t('terme_gage')</code>.
    </div>

    <!-- v1.7 — Avertissement termes privés -->
    <div class="alert py-2 mb-4" style="font-size:0.7rem;background:rgba(167,139,250,0.08);border:1px solid rgba(167,139,250,0.35);color:#a78bfa;">
        <i class="fa fa-lock me-1"></i>
        <strong>Comptes Famille / École / Entreprise</strong> — Ces termes configurables ne s'appliquent <strong>pas</strong> aux comptes privés.
        Ces comptes utilisent des termes fixes et séparés : <em>Engagement, Participant, Organisateur, Groupe</em>, etc.
        Aucune modification ici ne les affectera.
    </div>

    <form method="POST">
        <?php foreach ($termGroups as $grp => $terms): ?>
        <div class="card bg-dark border-secondary shadow mb-3">
            <div class="card-header py-2" style="background:#050510;border-bottom:1px solid #1a1a2a;">
                <span class="fw-bold text-info" style="font-size:0.72rem;letter-spacing:1px;"><?= htmlspecialchars($grp) ?></span>
            </div>
            <div class="card-body">
                <div class="row g-3">
                <?php foreach ($terms as $key => $info): ?>
                <div class="col-md-6">
                    <label class="small text-muted d-block mb-1" style="font-size:0.62rem;">
                        <?= htmlspecialchars($info['label']) ?>
                        <span style="color:#555;"> — défaut: "<?= htmlspecialchars($info['default']) ?>"</span>
                    </label>
                    <input type="text" name="<?= $key ?>"
                           value="<?= htmlspecialchars($current[$key]) ?>"
                           class="form-control form-control-sm bg-dark text-white border-secondary"
                           placeholder="<?= htmlspecialchars($info['default']) ?>">
                </div>
                <?php endforeach; ?>
                </div>
            </div>
        </div>
        <?php endforeach; ?>

        <div class="d-flex justify-content-between align-items-center">
            <div class="text-muted" style="font-size:0.65rem;">
                <i class="fa fa-code me-1"></i>Dans le code PHP : <code>t('terme_gage')</code> · Dans le HTML : <code>&lt;?= t('terme_gage') ?&gt;</code>
            </div>
            <button type="submit" name="save_terms" class="btn btn-info fw-bold">
                <i class="fa fa-floppy-disk me-1"></i>Sauvegarder la terminologie
            </button>
        </div>
    </form>
</div>
<?php include 'footer.php'; ?>
