<?php
/**
 * تحديث حالة الإشعار كمقروء
 * endpoint: POST /api/inspector/mark_notification_read.php
 */

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    http_response_code(200);
    exit();
}

require_once '../config/database.php';
require_once 'includes/auth.php';

$user = validateToken();
$data = json_decode(file_get_contents('php://input'), true);

$notificationId = $data['notification_id'] ?? 0;

if (!$notificationId) {
    http_response_code(400);
    echo json_encode(['error' => 'معرف الإشعار مطلوب']);
    exit();
}

try {
    $database = new Database();
    $db = $database->getConnection();
    
    $stmt = $db->prepare("
        UPDATE notifications 
        SET is_read = 1 
        WHERE id = :id AND user_id = :user_id
    ");
    $stmt->execute([
        ':id' => $notificationId,
        ':user_id' => $user['user_id']
    ]);
    
    echo json_encode(['success' => true]);
    
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['error' => 'خطأ في قاعدة البيانات: ' . $e->getMessage()]);
}
?>