<?php
session_name("teacher_session");
session_start();
include('includes/dbconnection.php');

// Ensure clean output
ob_clean();
header('Content-Type: text/plain');

// Mark individual notification as read
if (isset($_POST['id'])) {
    $notif_id = $_POST['id'];
    $update_stmt = $con->prepare("UPDATE notifications SET status = 'read' WHERE id = ?");
    $update_stmt->bind_param("i", $notif_id);
    $update_stmt->execute();
    $update_stmt->close();
    echo "Notification marked as read.";
    exit();
}

// Mark all notifications as read
if (isset($_POST['mark_all_read'])) {
    $teacher_id = $_POST['teacher_id'] ?? null;

    if ($teacher_id) {
        $update_all_stmt = $con->prepare("UPDATE notifications SET status = 'read' WHERE teacher_id = ? AND status = 'unread'");
        $update_all_stmt->bind_param("i", $teacher_id);
        $update_all_stmt->execute();
        $update_all_stmt->close();
        echo "All notifications marked as read.";
    } else {
        echo "Teacher ID not provided.";
    }

    exit();
}
