<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/middleware.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/notifications.php';
header('Content-Type: application/json');
require_customer();
start_session();
$uid = (int)($_SESSION['user']['id'] ?? 0);
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
if ($method === 'GET') {
  $rows = $pdo->prepare("SELECT * FROM notifications WHERE user_id=? ORDER BY id DESC LIMIT 50");
  $rows->execute([$uid]); echo json_encode(['notifications'=>$rows->fetchAll()]); exit;
}
$payload = json_decode(file_get_contents('php://input') ?: '[]', true) ?: [];
if ($method === 'POST' && ($payload['action'] ?? '')==='read') {
  $id = (int)($payload['id'] ?? 0);
  mark_notification_read($pdo, $uid, $id);
  echo json_encode(['ok'=>true]); exit;
}
http_response_code(405); echo json_encode(['error'=>'Unsupported']); 
