-- name: CreateNotification :one
INSERT INTO notifications (
    user_id,
    type,
    level,
    channel,
    title,
    message,
    payload
)
VALUES (
    $1,
    $2,
    $3,
    $4,
    $5,
    $6,
    $7
)
RETURNING *;

-- name: GetNotification :one
SELECT *
FROM notifications
WHERE id = $1
LIMIT 1;

-- name: GetAllNotifications :many
SELECT *
FROM notifications
ORDER BY created_at DESC
LIMIT $1 OFFSET $2;

-- name: GetTotalNotificationCount :one
SELECT COUNT(*)
FROM notifications;

-- name: GetUserNotifications :many
SELECT *
FROM notifications
WHERE user_id = $1
ORDER BY created_at DESC
LIMIT $2 OFFSET $3;

-- name: GetUserNotificationCount :one
SELECT COUNT(*)
FROM notifications
WHERE user_id = $1;

-- name: CountUnreadNotifications :one
SELECT COUNT(*)
FROM notifications
WHERE user_id = $1
  AND is_read = FALSE;

-- name: MarkNotificationAsRead :one
UPDATE notifications
SET is_read = TRUE,
    read_at = NOW()
WHERE id = $1
RETURNING *;

-- name: MarkAllUserNotificationsAsRead :exec
UPDATE notifications
SET is_read = TRUE,
    read_at = NOW()
WHERE user_id = $1
  AND is_read = FALSE;

-- name: MarkNotificationAsUnread :one
UPDATE notifications
SET is_read = FALSE,
    read_at = NULL
WHERE id = $1
RETURNING *;

-- name: MarkAllUserNotificationsAsUnread :exec
UPDATE notifications
SET is_read = FALSE,
    read_at = NULL
WHERE user_id = $1
  AND is_read = TRUE;

-- name: DeleteUserNotifications :exec
DELETE FROM notifications
WHERE user_id = $1;
