<?php
require_once __DIR__ . '/includes/functions.php';
startSecureSession();
requireLogin();

$user = getUserById($_SESSION['user_id']);
if (!$user) {
    header('Location: logout.php');
    exit;
}

$planExpiresAt = strtotime($user['plan_expires_at']);
$secondsRemaining = max(0, $planExpiresAt - time());
$isExpired = $secondsRemaining <= 0;

$pageTitle = 'Overview';
$activeNav = 'dashboard';
include __DIR__ . '/includes/user_header.php';
?>

<div class="page-head">
  <h1 class="page-title">Hello, <?= htmlspecialchars($user['name']) ?></h1>
  <p class="page-sub">Here's your account at a glance. Use the menu to manage keys, links, plans and settings.</p>
</div>

<?php if ($isExpired): ?>
  <div class="alert alert-warn">
    <strong>Your plan has expired.</strong> Your API keys will not work until you renew. <a href="plans.php" style="font-weight:600; text-decoration:underline;">Renew now →</a>
  </div>
<?php endif; ?>

<div class="stat-grid">
  <div class="stat-card">
    <div class="stat-label">Total requests</div>
    <div class="stat-value"><?= number_format($user['total_requests']) ?></div>
  </div>
  <div class="stat-card">
    <div class="stat-label">Successful</div>
    <div class="stat-value up"><?= number_format($user['total_success']) ?></div>
  </div>
  <div class="stat-card">
    <div class="stat-label">Failed / pending</div>
    <div class="stat-value down"><?= number_format($user['total_failed']) ?></div>
  </div>
  <div class="stat-card">
    <div class="stat-label">Amount received</div>
    <div class="stat-value">₹<?= number_format($user['total_amount_received'], 2) ?></div>
  </div>
</div>

<div class="card">
  <div class="justify-between flex flex-wrap items-center" style="gap:16px;">
    <div>
      <h2 class="card-title">Plan status</h2>
      <div class="card-sub" style="margin-bottom:0;">Valid until <?= date('d M Y, h:i A', $planExpiresAt) ?></div>
    </div>
    <div class="flex items-center gap-16">
      <span class="timer <?= $isExpired ? 'expired' : '' ?>" id="expiryTimer" data-seconds="<?= $secondsRemaining ?>">
        <?= $isExpired ? 'Expired' : 'Calculating…' ?>
      </span>
      <a href="plans.php" class="btn btn-primary"><?= $isExpired ? 'Renew plan' : 'Upgrade plan' ?></a>
    </div>
  </div>
</div>

<script>
    (function() {
        const timerEl = document.getElementById('expiryTimer');
        let seconds = parseInt(timerEl.dataset.seconds, 10);
        if (seconds <= 0) return;

        function formatTime(s) {
            const d = Math.floor(s / 86400);
            const h = Math.floor((s % 86400) / 3600);
            const m = Math.floor((s % 3600) / 60);
            const sec = s % 60;
            if (d > 0) return `${d}d ${h}h ${m}m remaining`;
            if (h > 0) return `${h}h ${m}m ${sec}s remaining`;
            return `${m}m ${sec}s remaining`;
        }

        const interval = setInterval(() => {
            seconds--;
            if (seconds <= 0) {
                timerEl.innerText = 'Expired';
                timerEl.classList.add('expired');
                clearInterval(interval);
                return;
            }
            timerEl.innerText = formatTime(seconds);
        }, 1000);
        timerEl.innerText = formatTime(seconds);
    })();
</script>

<?php include __DIR__ . '/includes/user_footer.php'; ?>
