<?php
/**
 * User Dashboard (Protected)
 * PHP User Registration - https://rskworld.in
 * Developer: Molla Samser | RSK World
 * Contact: help@rskworld.in | +91 93305 39277
 * Year: 2026
 */

require_once __DIR__ . '/includes/init.php';
requireLogin();

$user = getCurrentUser();
if ($user === null) {
    session_destroy();
    header('Location: login.php');
    exit;
}

$pageTitle = 'Dashboard';
require_once __DIR__ . '/includes/header.php';
?>

<div class="dashboard-card">
    <h1><i class="fas fa-tachometer-alt"></i> Dashboard</h1>
    <p class="subtitle">PHP User Registration — <a href="https://rskworld.in" target="_blank">rskworld.in</a></p>

    <?php if (empty($user['email_verified'])): ?>
        <div class="alert alert-warning">
            <strong>Email not verified.</strong> Please check your inbox and click the verification link.
            <br><br>
            <a href="resend-verify.php" class="btn btn-primary btn-sm">Resend verification email</a>
            <?php
            $isLocal = in_array($_SERVER['HTTP_HOST'] ?? '', ['localhost', '127.0.0.1'], true);
            if ($isLocal): ?>
                <a href="verify-email.php" class="btn btn-secondary btn-sm">Mark as verified (local testing)</a>
            <?php endif; ?>
        </div>
    <?php else: ?>
        <div class="alert alert-success">Your email is verified.</div>
    <?php endif; ?>

    <div class="user-info">
        <?php
        $avatarPath = $user['avatar_path'] ?? null;
        if ($avatarPath && is_string($avatarPath) && $avatarPath !== ''):
            $avatarSrc = (strpos($avatarPath, '/') === 0 || strpos($avatarPath, 'http') === 0) ? $avatarPath : $avatarPath;
        ?><p><img src="<?= htmlspecialchars($avatarSrc) ?>" alt="" class="avatar avatar-dashboard" width="48" height="48"></p>
        <?php endif; ?>
        <p><strong>Name:</strong> <?= htmlspecialchars($user['name'] ?? '') ?></p>
        <p><strong>Email:</strong> <?= htmlspecialchars($user['email'] ?? '') ?></p>
        <p><strong>Member since:</strong> <?= !empty($user['created_at']) ? date('F j, Y', strtotime($user['created_at'])) : '—' ?></p>
        <?php if (!empty($user['last_login_at'])): ?>
            <p><strong>Last login:</strong> <?= date('M j, Y g:i A', strtotime($user['last_login_at'])) ?><?= !empty($user['last_login_ip']) ? ' from ' . htmlspecialchars($user['last_login_ip']) : '' ?></p>
        <?php endif; ?>
    </div>
    <p>
        <a href="profile.php" class="btn btn-primary">Profile</a>
        <?php if (function_exists('isAdmin') && isAdmin()): ?>
            <a href="admin/index.php" class="btn btn-primary">Admin Panel</a>
        <?php endif; ?>
        <a href="logout.php" class="btn btn-secondary">Log Out</a>
    </p>
</div>

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