-- name: InsertNotification :exec
INSERT INTO notifications (id, user_id, topic, severity, title, body, link, metadata, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);

-- name: ListNotificationsByUser :many
SELECT id, user_id, topic, severity, title, body, link, metadata, read_at, created_at
FROM notifications
WHERE user_id = $1
  AND ($2::bool = false OR read_at IS NULL)
ORDER BY created_at DESC
LIMIT $3;

-- name: CountUnreadByUser :one
SELECT COUNT(*)::int FROM notifications WHERE user_id = $1 AND read_at IS NULL;

-- name: MarkRead :exec
UPDATE notifications SET read_at = now()
WHERE id = $1 AND user_id = $2 AND read_at IS NULL;

-- name: MarkAllReadByUser :exec
UPDATE notifications SET read_at = now()
WHERE user_id = $1 AND read_at IS NULL;
