<?php
require_login
();

// Mark notification as read if requested
if (isset($_GET['mark_read']) && is_numeric($_GET['mark_read'])) {
    
$notifId intval($_GET['mark_read']);
    
$userId $_SESSION['user_id'];
    
    
$markReadSql "UPDATE TICKET_Notifications SET readAt = NOW() WHERE pk_Notification = ? AND fk_User = ?";
    
$markReadStmt mysqli_prepare($conn$markReadSql);
    
mysqli_stmt_bind_param($markReadStmt'ii'$notifId$userId);
    
mysqli_stmt_execute($markReadStmt);
    
mysqli_stmt_close($markReadStmt);
    
    
header('Location: index.php?page=notifications');
    exit;
}

// Get user's notifications with additional context
$userId $_SESSION['user_id'];
$sql "
    SELECT 
        n.pk_Notification,
        n.sendAt,
        n.readAt,
        r.pk_Request,
        r.fk_User as request_creator_id,
        r.is_closed,
        w.title AS workflow_title,
        s.title AS state_title,
        u.firstName,
        u.lastName
    FROM TICKET_Notifications n
    JOIN TICKET_Request r ON n.fk_Request = r.pk_Request
    JOIN TICKET_Workflow w ON r.fk_Workflow = w.pk_Workflow
    JOIN TICKET_State s ON n.fk_State = s.pk_State
    JOIN TICKET_User u ON r.fk_User = u.pk_User
    WHERE n.fk_User = ?
    ORDER BY n.sendAt DESC
"
;

$stmt mysqli_prepare($conn$sql);
mysqli_stmt_bind_param($stmt'i'$userId);
mysqli_stmt_execute($stmt);
$result mysqli_stmt_get_result($stmt);
$hasNotifications mysqli_num_rows($result) > 0;
?>

<div class="page-notifications">
    <div class="page-header">
        <h1 class="page-title">Notifications</h1>
        <p class="page-subtitle">Stay updated on request assignments and status changes</p>
    </div>
    
    <?php if ($hasNotifications): ?>
        <div class="notifications-container">
            <?php while ($notification mysqli_fetch_assoc($result)): ?>
                <?php 
                $isCompletion 
= ($notification['is_closed'] == && $notification['request_creator_id'] == $userId);
                
$isAssignment = !$isCompletion;
                
?>
                <div class="notification-card <?= $notification['readAt'] ? 'read' 'unread' ?> <?= $isCompletion 'completed' '' ?>">
                    <div class="notification-header">
                        <div class="notification-icon">
                            <?php if ($isCompletion): ?>
                                <?= $notification['readAt'] ? '✅' '🎉' ?>
                            <?php else: ?>
                                <?= $notification['readAt'] ? '📧' '📬' ?>
                            <?php endif; ?>
                        </div>
                        <div class="notification-meta">
                            <span class="notification-time">
                                <?= date('M j, Y g:i A'strtotime($notification['sendAt'])) ?>
                            </span>
                            <?php if (!$notification['readAt']): ?>
                                <span class="unread-badge">New</span>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <div class="notification-content">
                        <h3 class="notification-title">
                            <?= $isCompletion 'Request Completed' 'New Task Assignment' ?>
                        </h3>
                        <p class="notification-message">
                            <?php if ($isCompletion): ?>
                                Your request <strong>#<?= $notification['pk_Request'?></strong> 
                                for "<?= htmlspecialchars($notification['workflow_title']) ?>
                                has been completed and closed.
                            <?php else: ?>
                                Request <strong>#<?= $notification['pk_Request'?></strong> 
                                for "<?= htmlspecialchars($notification['workflow_title']) ?>
                                has moved to state "<?= htmlspecialchars($notification['state_title']) ?>
                                and requires your attention.
                            <?php endif; ?>
                        </p>
                        <p class="notification-requester">
                            <?php if ($isCompletion): ?>
                                Status: <span style="color: var(--success-color); font-weight: 600;">Completed</span>
                            <?php else: ?>
                                Requested by: <?= htmlspecialchars($notification['firstName'] . ' ' $notification['lastName']) ?>
                            <?php endif; ?>
                        </p>
                    </div>
                    
                    <div class="notification-actions">
                        <a href="index.php?page=request_handle&req=<?= $notification['pk_Request'?>
                           class="btn btn-primary">
                            <?= $isCompletion 'View Completed Request' 'Handle Request' ?>
                        </a>
                        <?php if (!$notification['readAt']): ?>
                            <a href="index.php?page=notifications&mark_read=<?= $notification['pk_Notification'?>
                               class="btn btn-secondary">
                                Mark as Read
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endwhile; ?>
        </div>
    <?php else: ?>
        <div class="empty-state">
            <div class="empty-state-icon">🔔</div>
            <h3 class="empty-state-title">No Notifications</h3>
            <p class="empty-state-description">
                You don't have any notifications yet. When requests are assigned to you or your group, 
                or when your requests are completed, they'll appear here.
            </p>
        </div>
    <?php endif; ?>
</div>