<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

if (!isset($_SESSION['user_id']) || $_SESSION['role_code'] !== 'PATIENT') {
    header('Location: login.php');
    exit();
}

$user_id = $_SESSION['user_id'];
$pat_res = $conn->query("SELECT patient_id FROM patients WHERE user_id = $user_id")->fetch_assoc();
$patient_id = $pat_res['patient_id'];

// Create table if not exists
$conn->query("CREATE TABLE IF NOT EXISTS patient_notifications (
    notification_id INT AUTO_INCREMENT PRIMARY KEY,
    patient_id INT NOT NULL,
    title VARCHAR(255) NOT NULL,
    message TEXT NOT NULL,
    is_read BOOLEAN DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");

// Mark all as read if 'mark_all_read' is set
if (isset($_GET['action']) && $_GET['action'] == 'mark_all_read') {
    $conn->query("UPDATE patient_notifications SET is_read = 1 WHERE patient_id = $patient_id");
    header("Location: patient_notifications.php");
    exit();
}

// Fetch Notifications
$sql = "SELECT * FROM patient_notifications WHERE patient_id = ? ORDER BY created_at DESC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $patient_id);
$stmt->execute();
$result = $stmt->get_result();
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Notifications')); ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Notifications</h4>
                                <div class="page-title-right">
                                    <a href="?action=mark_all_read" class="btn btn-sm btn-soft-primary">Mark All as
                                        Read</a>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <div class="list-group list-group-flush">
                                        <?php if ($result->num_rows > 0): ?>
                                            <?php while ($row = $result->fetch_assoc()):
                                                $bgClass = $row['is_read'] ? '' : 'bg-light';
                                                ?>
                                                <a href="#"
                                                    class="list-group-item list-group-item-action <?php echo $bgClass; ?>">
                                                    <div class="d-flex w-100 justify-content-between">
                                                        <h5
                                                            class="mb-1 <?php echo $row['is_read'] ? '' : 'fw-bold text-primary'; ?>">
                                                            <?php echo htmlspecialchars($row['title']); ?>
                                                        </h5>
                                                        <small
                                                            class="text-muted"><?php echo date('M d, H:i', strtotime($row['created_at'])); ?></small>
                                                    </div>
                                                    <p class="mb-1"><?php echo htmlspecialchars($row['message']); ?></p>
                                                </a>
                                            <?php endwhile; ?>
                                        <?php else: ?>
                                            <div class="text-center py-5">
                                                <i class="ri-notification-off-line fs-1 text-muted"></i>
                                                <p class="text-muted mt-2">No notifications found.</p>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>

</html>