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

$user = User::current();
if (!$user) {
    User::logout();
    redirect(APP_URL . '/login.php');
}

$stats = User::effectiveStats($user);
$equipped_count = (int) DB::fetchValue(
    "SELECT COUNT(*) FROM user_items WHERE user_id = :u AND equipped = 1",
    [':u' => $user['id']]
);

$page_title = __('character');
require __DIR__ . '/includes/header.php';
?>

<div class="character-header">
    <img src="<?= APP_URL ?>/assets/img/race<?= (int)$user['race'] ?>.gif"
         alt="<?= __($user['race'] == 1 ? 'race_vampire' : 'race_werewolf') ?>"
         class="character-portrait">
    <div class="character-info">
        <h1><?= e($user['name']) ?>
            <span class="badge <?= $user['race'] == 1 ? 'badge-vampire' : 'badge-werewolf' ?>">
                <?= __($user['race'] == 1 ? 'race_vampire' : 'race_werewolf') ?>
            </span>
            <?php if ($user['is_admin']): ?>
                <span class="badge badge-admin">ADMIN</span>
            <?php endif; ?>
        </h1>
    </div>
</div>

<div class="stats-grid mt-1">
    <div class="stat-box">
        <div class="label"><?= __('level') ?></div>
        <div class="value"><?= (int)$user['level'] ?></div>
    </div>
    <div class="stat-box">
        <div class="label"><?= __('experience') ?></div>
        <div class="value"><?= format_number((int)$user['exp']) ?></div>
    </div>
    <div class="stat-box hp">
        <div class="label"><?= __('health') ?></div>
        <div class="value"><?= (int)$user['hp_now'] ?>/<?= (int)$user['hp_max'] ?></div>
    </div>
    <div class="stat-box gold">
        <div class="label"><?= __('gold') ?></div>
        <div class="value"><?= format_number((int)$user['gold']) ?></div>
    </div>
    <div class="stat-box">
        <div class="label"><?= __('hellstones') ?></div>
        <div class="value" style="color: var(--hellstone)"><?= format_number((int)$user['hellstones']) ?></div>
    </div>
</div>

<h2 class="mt-2"><?= __('stats') ?></h2>
<div class="stats-grid">
    <div class="stat-box">
        <div class="label"><?= __('power') ?></div>
        <div class="value"><?= $stats['pow'] ?></div>
    </div>
    <div class="stat-box">
        <div class="label"><?= __('defense') ?></div>
        <div class="value"><?= $stats['def'] ?></div>
    </div>
    <div class="stat-box">
        <div class="label"><?= __('agility') ?></div>
        <div class="value"><?= $stats['agi'] ?></div>
    </div>
    <div class="stat-box">
        <div class="label"><?= __('stamina') ?></div>
        <div class="value"><?= $stats['stam'] ?></div>
    </div>
    <div class="stat-box">
        <div class="label"><?= __('charisma') ?></div>
        <div class="value"><?= $stats['chr'] ?></div>
    </div>
</div>

<h2 class="mt-2"><?= __('equipment') ?></h2>
<p class="text-muted">
    <?= __('inventory') ?>: <?= $equipped_count ?> <?= __('item_equip') ?>.
    <a href="<?= APP_URL ?>/hideout.php" class="btn btn-sm btn-secondary"><?= __('menu_hideout') ?> →</a>
</p>

<hr class="divider">

<div class="card">
    <h3>📊 <?= __('stats') ?></h3>
    <table class="tbl">
        <tr><td>⚔ <?= __('attack') ?> / <?= __('menu_fight') ?></td>
            <td class="text-right"><?= (int)$user['stat_battle'] ?></td></tr>
        <tr><td>🏆 <?= __('fight_won') ?></td>
            <td class="text-right text-success"><?= (int)$user['stat_win'] ?></td></tr>
        <tr><td>💀 <?= __('fight_lost') ?></td>
            <td class="text-right text-danger"><?= (int)$user['stat_loss'] ?></td></tr>
        <tr><td>🤝 <?= __('fight_draw') ?></td>
            <td class="text-right"><?= (int)$user['stat_draw'] ?></td></tr>
    </table>
</div>

<?php require __DIR__ . '/includes/footer.php'; ?>
