<?php
require_once __DIR__ . '/../includes/bootstrap.php';
require_once __DIR__ . '/_helpers.php';
require_admin();

// İstatistikler
$total_users  = (int) DB::fetchValue("SELECT COUNT(*) FROM users");
$total_clans  = (int) DB::fetchValue("SELECT COUNT(*) FROM clans");
$total_items  = (int) DB::fetchValue("SELECT COUNT(*) FROM items");
$banned_users = (int) DB::fetchValue("SELECT COUNT(*) FROM users WHERE is_banned = 1");
$online_now   = (int) DB::fetchValue(
    "SELECT COUNT(*) FROM users WHERE last_active > :t",
    [':t' => time() - 300]
);
$total_fights = (int) DB::fetchValue("SELECT COUNT(*) FROM user_fights");

$recent_users = DB::fetchAll(
    "SELECT id, name, race, created_at FROM users ORDER BY created_at DESC LIMIT 10"
);
$recent_logs = DB::fetchAll(
    "SELECT al.*, u.name AS admin_name
     FROM admin_log al
     LEFT JOIN users u ON al.admin_id = u.id
     ORDER BY al.created_at DESC LIMIT 10"
);

$page_title = __('admin_dashboard');
require __DIR__ . '/_header.php';
?>

<h1><?= e(__('admin_dashboard')) ?></h1>

<div class="stats-grid">
    <div class="stat-box">
        <div class="label"><?= __('admin_total_users') ?></div>
        <div class="value"><?= format_number($total_users) ?></div>
    </div>
    <div class="stat-box gold">
        <div class="label"><?= __('admin_online_now') ?></div>
        <div class="value"><?= format_number($online_now) ?></div>
    </div>
    <div class="stat-box">
        <div class="label"><?= __('admin_total_clans') ?></div>
        <div class="value"><?= format_number($total_clans) ?></div>
    </div>
    <div class="stat-box">
        <div class="label"><?= __('admin_total_items') ?></div>
        <div class="value"><?= format_number($total_items) ?></div>
    </div>
    <div class="stat-box">
        <div class="label"><?= __('menu_fight') ?></div>
        <div class="value"><?= format_number($total_fights) ?></div>
    </div>
    <div class="stat-box hp">
        <div class="label"><?= __('admin_user_ban') ?></div>
        <div class="value" style="color: var(--danger)"><?= format_number($banned_users) ?></div>
    </div>
</div>

<div style="display:grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-top: 2rem;">
    <div class="card">
        <h3><?= __('admin_users') ?></h3>
        <table class="tbl">
            <thead>
                <tr><th><?= __('username') ?></th><th>Race</th><th>Date</th></tr>
            </thead>
            <tbody>
                <?php foreach ($recent_users as $u): ?>
                <tr>
                    <td><a href="<?= APP_URL ?>/admin/users.php?id=<?= (int)$u['id'] ?>"><?= e($u['name']) ?></a></td>
                    <td>
                        <span class="badge <?= $u['race'] == 1 ? 'badge-vampire' : 'badge-werewolf' ?>" style="font-size:0.65rem;">
                            <?= __($u['race'] == 1 ? 'race_vampire' : 'race_werewolf') ?>
                        </span>
                    </td>
                    <td class="text-muted"><?= format_datetime((int)$u['created_at']) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <div class="card">
        <h3><?= __('admin_logs') ?></h3>
        <?php if (empty($recent_logs)): ?>
            <p class="text-muted">—</p>
        <?php else: ?>
        <table class="tbl">
            <thead>
                <tr><th>Admin</th><th>Action</th><th>Date</th></tr>
            </thead>
            <tbody>
                <?php foreach ($recent_logs as $log): ?>
                <tr>
                    <td><?= e($log['admin_name'] ?? '-') ?></td>
                    <td><?= e($log['action']) ?></td>
                    <td class="text-muted"><?= format_datetime((int)$log['created_at']) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
</div>

<?php require __DIR__ . '/_footer.php'; ?>
