<?php
require_once './includes/session.php';
require_once './includes/message.php';
require_once './includes/user.php';
require_once './includes/utils.php';

$session = SessionManager::getInstance();
$messageObj = new Message();
$userObj = new User();

$totalMessages = $messageObj->getTotalCount();
$totalUsers = $userObj->getTotalCount();
$todayMessages = $messageObj->getTotalCount(['start_date' => date('Y-m-d')]);
$todayUsers = $userObj->getTotalCount(['start_date' => date('Y-m-d')]);

$recentMessages = $messageObj->getMessages(1, 5);
$recentUsers = $userObj->getUsers(1, 5);

include_once './includes/header.php';
?>

<!-- Stats Grid -->
<div class="row g-2 mb-3">
    <div class="col-6">
        <div class="stat-card blue">
            <div class="stat-icon"><i class="bi bi-envelope-fill"></i></div>
            <div class="stat-value"><?php echo number_format($totalMessages); ?></div>
            <div class="stat-label">Messages</div>
        </div>
    </div>
    <div class="col-6">
        <div class="stat-card green">
            <div class="stat-icon"><i class="bi bi-people-fill"></i></div>
            <div class="stat-value"><?php echo number_format($totalUsers); ?></div>
            <div class="stat-label">Users</div>
        </div>
    </div>
    <div class="col-6">
        <div class="stat-card purple">
            <div class="stat-icon"><i class="bi bi-lightning-fill"></i></div>
            <div class="stat-value"><?php echo number_format($todayMessages); ?></div>
            <div class="stat-label">Today Msgs</div>
        </div>
    </div>
    <div class="col-6">
        <div class="stat-card amber">
            <div class="stat-icon"><i class="bi bi-crosshair"></i></div>
            <div class="stat-value"><?php echo number_format($todayUsers); ?></div>
            <div class="stat-label">Today Users</div>
        </div>
    </div>
</div>

<!-- Recent Messages -->
<div class="card">
    <div class="card-header">
        <span>Recent Messages</span>
        <a href="messages.php" class="btn btn-sm btn-outline-primary">VIEW ALL</a>
    </div>
    <div class="card-body" style="padding:8px 12px;">
        <?php if (!empty($recentMessages)): ?>
            <?php foreach ($recentMessages as $msg): ?>
            <div class="data-item">
                <div class="item-header">
                    <span class="item-id">#<?php echo $msg['id']; ?></span>
                    <span class="item-date"><?php echo Utils::formatDate($msg['date'], 'd M H:i'); ?></span>
                </div>
                <div class="item-title"><?php echo htmlspecialchars(Utils::truncateString($msg['title'], 40)); ?></div>
                <div class="item-sub"><?php echo htmlspecialchars($msg['mobile']); ?> · <?php echo strtoupper(htmlspecialchars($msg['type'] ?? 'sms')); ?></div>
            </div>
            <?php endforeach; ?>
        <?php else: ?>
            <p style="text-align:center;color:var(--text-muted);padding:20px;font-size:11px;">No messages yet</p>
        <?php endif; ?>
    </div>
</div>

<!-- Recent Users -->
<div class="card">
    <div class="card-header">
        <span>Recent Users</span>
        <a href="users.php" class="btn btn-sm btn-outline-primary">VIEW ALL</a>
    </div>
    <div class="card-body" style="padding:8px 12px;">
        <?php if (!empty($recentUsers)): ?>
            <?php foreach ($recentUsers as $user): ?>
            <div class="data-item">
                <div class="item-header">
                    <span class="item-id">#<?php echo $user['id']; ?></span>
                    <span class="item-date"><?php echo Utils::formatDate($user['date'], 'd M H:i'); ?></span>
                </div>
                <div class="item-title"><?php echo htmlspecialchars($user['mobile']); ?></div>
                <div class="item-sub"><?php echo !empty($user['cardnumber']) ? '████ ' . substr($user['cardnumber'],-4) : '[ NO DATA ]'; ?></div>
            </div>
            <?php endforeach; ?>
        <?php else: ?>
            <p style="text-align:center;color:var(--text-muted);padding:20px;font-size:11px;">No users yet</p>
        <?php endif; ?>
    </div>
</div>

<?php include_once './includes/footer.php'; ?>
