<?php
session_start();
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../classes/Database.php';
require_once __DIR__ . '/../../classes/Notifications.php';


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

$database = new Database();
$conn = $database->getConnection();
$auth = new Auth($conn);
$notifications = new Notifications($conn);
$isAdmin = $auth->isSuperAdmin($_SESSION['user_id']) || $auth->isController($_SESSION['user_id']);
$userBranchId = $notifications->getUserBranch($_SESSION['user_id']);
$returnTo = $_POST['return_to'] ?? ($isAdmin ? 'admin.php' : 'branch.php');
$allowedReturns = ['admin.php', '2admin.php', 'branch.php', 'branch2.php', 'controller.php'];
if (!in_array($returnTo, $allowedReturns, true)) {
	$returnTo = $isAdmin ? 'admin.php' : 'branch.php';
}

/*
if (!$auth->isBranchManager($_SESSION['user_id'])) {
    header('Location: weekly2.php?error=' . urlencode('Access denied. Only branch managers can delete deposits.'));
    exit();
} */
	
		if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_message'], $_POST['notification_id'])) {
			$notification_id = (int)$_POST['notification_id'];
			if (!$notifications->markMessageForTarget($notification_id, $userBranchId, $isAdmin)) {
				header('Location: ' . $returnTo . '?error=' . urlencode('Cannot mark notification as read.'));
				exit();
					
			}
			else{
				header('Location: ' . $returnTo . '?success=' . urlencode('Notification marked as read.'));
				exit();
			}

		}

/*

    if ($deposit->deleteDeposit($depositId)) {
        header('Location: weekly2.php?success=' . urlencode('Deposit deleted successfully.'));
    } else {
        header('Location: weekly2.php?error=' . urlencode('Error deleting deposit. Please try again.'));
    }
} else {
    header('Location: weekly2.php?error=' . urlencode('Invalid request.'));
}


*/
//exit();
?>
