<?php
$page_title = "All Notifications";
require_once '../config/config.php';
require_once '../includes/user_auth.php';
require_once '../includes/functions.php';

if (!$auth->isLoggedIn()) {
    header("Location: ../login.php");
    exit();
}

$db = Database::getInstance()->connect();
$user_id = $auth->getCurrentUserId();

// Get all notifications for the user
$stmt = $db->prepare("SELECT * FROM notifications WHERE user_id = ? ORDER BY created_at DESC");
$stmt->execute([$user_id]);
$notifications = $stmt->fetchAll();

require_once '../includes/header_user.php';
?>

<div class="container mx-auto px-4 py-8">
    <div class="max-w-4xl mx-auto">
        <div class="mb-8">
            <h1 class="text-3xl font-bold text-gray-800">All Notifications</h1>
            <p class="text-gray-600">Your complete notification history.</p>
        </div>

        <div class="bg-white rounded-xl shadow-lg">
            <div class="space-y-2 p-4">
                <?php if (empty($notifications)): ?>
                    <p class="text-center text-gray-500 py-8">You have no notifications.</p>
                <?php else: ?>
                    <?php foreach ($notifications as $notification):
                        $icon = 'fa-info-circle';
                        $color = 'blue';
                        switch ($notification['type']) {
                            case 'success': case 'profit': case 'deposit_approved':
                                $icon = 'fa-check-circle'; $color = 'green'; break;
                            case 'error':
                                $icon = 'fa-exclamation-circle'; $color = 'red'; break;
                            case 'warning':
                                $icon = 'fa-exclamation-triangle'; $color = 'yellow'; break;
                        }
                        $read_class = $notification['is_read'] ? 'opacity-70' : 'bg-'.$color.'-50';
                    ?>
                    <div class="p-4 rounded-lg border-l-4 border-<?php echo $color; ?>-500 <?php echo $read_class; ?>">
                        <div class="flex items-start space-x-4">
                            <i class="fas <?php echo $icon; ?> text-<?php echo $color; ?>-500 text-xl mt-1"></i>
                            <div>
                                <p class="font-semibold"><?php echo htmlspecialchars($notification['title']); ?></p>
                                <p class="text-sm text-gray-700"><?php echo htmlspecialchars($notification['message']); ?></p>
                                <p class="text-xs text-gray-500 mt-1"><?php echo date('F j, Y, g:i a', strtotime($notification['created_at'])); ?></p>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

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