<?php
session_start();
$db_path = file_exists('includes/db.php') ? 'includes/db.php' : '../includes/db.php';
if (file_exists($db_path)) {
    require $db_path;
} else {
    $conn = new mysqli('localhost', 'root', '', 'bajipari');
}

// Auth Check
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

$uid = $_SESSION['user_id'];

// --- Create Notifications Table (If not exists) ---
$conn->query("CREATE TABLE IF NOT EXISTS `notifications` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `title` varchar(255) NOT NULL,
  `message` text NOT NULL,
  `type` varchar(50) DEFAULT 'system',
  `is_read` tinyint(1) DEFAULT 0,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");

// --- Insert Welcome Msg if empty (For Demo) ---
$check = $conn->query("SELECT id FROM notifications WHERE user_id=$uid");
if ($check->num_rows == 0) {
    $conn->query("INSERT INTO notifications (user_id, title, message, type, created_at) VALUES ($uid, 'Sign up success.', 'Congratulations sign up success.', 'system', NOW())");
}

// --- Fetch Notifications ---
$sql = "SELECT * FROM notifications WHERE user_id=$uid ORDER BY created_at DESC";
$result = $conn->query($sql);
$current_date = date('Y/m/d');
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Inbox</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">

    <style>
        body { 
            background-color: #121619; 
            color: #d1d5db; 
            font-family: 'Roboto', sans-serif; 
            padding-bottom: 80px;
        }

        /* Header */
        .header-bg { 
            background-color: #232a31; 
            padding: 15px; 
            display: flex; 
            justify-content: center; 
            align-items: center; 
            position: sticky; 
            top: 0; 
            z-index: 50; 
            border-bottom: 1px solid #2f3840; 
        }
        .close-btn { position: absolute; right: 15px; font-size: 18px; color: #9ca3af; }

        /* Toolbar (Date & GMT) */
        .toolbar {
            background-color: #1c2227;
            padding: 10px 15px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #2f3840;
            font-size: 12px;
            color: #6b7280;
        }
        .gmt-badge {
            border: 1px solid #4b5563;
            border-radius: 4px;
            padding: 1px 4px;
            font-size: 10px;
            margin-right: 10px;
        }

        /* Message Item */
        .msg-item {
            background-color: #232a31;
            padding: 15px;
            border-bottom: 1px solid #2f3840;
            display: flex;
            gap: 12px;
            align-items: flex-start;
        }
        .msg-icon {
            width: 35px; height: 35px;
            background-color: #eab308; /* Yellow/Gold */
            border-radius: 50%;
            display: flex; align-items: center; justify-content: center;
            color: white; font-size: 16px;
            flex-shrink: 0;
        }
        .msg-content { flex: 1; }
        .msg-title { color: #fff; font-size: 13px; font-weight: bold; margin-bottom: 2px; }
        .msg-desc { color: #6b7280; font-size: 12px; }
        .msg-time { color: #6b7280; font-size: 11px; margin-top: 2px; }

        /* End Text */
        .end-text { text-align: center; color: #4b5563; font-size: 12px; margin-top: 20px; }

        /* Bottom Nav */
        .bottom-nav { background-color: #000; border-top: 1px solid #222; }
        .nav-item { color: #6b7280; } .nav-item.active { color: #facc15; }
    </style>
</head>
<body>

    <div class="header-bg">
        <h1 class="text-white font-medium text-base">Inbox</h1>
        <a href="account.php" class="close-btn"><i class="fas fa-times"></i></a>
    </div>

    <div class="toolbar">
        <div class="flex items-center gap-2">
            <i class="far fa-calendar-alt"></i>
            <span><?php echo $current_date; ?></span>
        </div>
        <div class="flex items-center">
            <span class="gmt-badge">GMT+6</span>
            <i class="fas fa-ellipsis-h text-lg text-gray-400"></i>
        </div>
    </div>

    <div>
        <?php if($result && $result->num_rows > 0): ?>
            <?php while($row = $result->fetch_assoc()): ?>
                <div class="msg-item">
                    <div class="msg-icon">
                        <i class="fas fa-bullhorn transform -rotate-12"></i>
                    </div>
                    <div class="msg-content">
                        <div class="flex justify-between items-start">
                            <h3 class="msg-title"><?php echo $row['title']; ?></h3>
                            <span class="msg-time"><?php echo date('H:i:s', strtotime($row['created_at'])); ?></span>
                        </div>
                        <p class="msg-desc"><?php echo $row['message']; ?></p>
                    </div>
                </div>
            <?php endwhile; ?>
        <?php else: ?>
            <div class="p-10 text-center text-gray-500 text-xs">No messages</div>
        <?php endif; ?>
    </div>

    <div class="end-text">- end of page -</div>

    <div class="fixed bottom-0 left-0 w-full h-[60px] bottom-nav z-50 flex justify-between items-center px-6">
        <a href="index.php" class="flex flex-col items-center justify-center nav-item">
            <i class="fas fa-home text-lg mb-1"></i>
            <span class="text-[10px]">Home</span>
        </a>
        <a href="promotions.php" class="flex flex-col items-center justify-center nav-item">
            <i class="fas fa-gift text-lg mb-1"></i>
            <span class="text-[10px]">Promotions</span>
        </a>
        <a href="deposit.php" class="flex flex-col items-center justify-center nav-item">
            <i class="fas fa-wallet text-lg mb-1"></i>
            <span class="text-[10px]">Deposit</span>
        </a>
        <a href="account.php" class="flex flex-col items-center justify-center nav-item active">
            <i class="fas fa-user text-lg mb-1"></i>
            <span class="text-[10px]">My Account</span>
        </a>
    </div>

</body>
</html>