-- name: CreateNotification :one
INSERT INTO notifications (
        id,
        recipient_id,
        type,
        level,
        error_severity,
        reciever,
        is_read,
        delivery_status,
        delivery_channel,
        payload,
        priority,
        timestamp,
        metadata
    )
VALUES (
        $1,
        $2,
        $3,
        $4,
        $5,
        $6,
        $7,
        $8,
        $9,
        $10,
        $11,
        $12,
        $13
    )
RETURNING *;
-- name: GetNotification :one
SELECT *
FROM notifications
WHERE id = $1
LIMIT 1;
-- name: GetAllNotifications :many
SELECT *
FROM notifications
ORDER BY timestamp DESC
LIMIT $1 OFFSET $2;
-- name: ListNotifications :many
SELECT *
FROM notifications
WHERE recipient_id = $1
ORDER BY timestamp DESC
LIMIT $2 OFFSET $3;
-- name: CountUnreadNotifications :one
SELECT count(id)
FROM notifications
WHERE recipient_id = $1
    AND is_read = false;
-- name: UpdateNotificationStatus :one
UPDATE notifications
SET delivery_status = $2,
    is_read = $3,
    metadata = $4
WHERE id = $1
RETURNING *;
-- name: ListFailedNotifications :many
SELECT *
FROM notifications
WHERE delivery_status = 'failed'
    AND timestamp < NOW() - INTERVAL '1 hour'
ORDER BY timestamp ASC
LIMIT $1;
-- name: ListRecipientIDsByReceiver :many
SELECT recipient_id
FROM notifications
WHERE reciever = $1;

-- name: GetNotificationCounts :many
SELECT 
    COUNT(*) as total,
    COUNT(CASE WHEN is_read = true THEN 1 END) as read,
    COUNT(CASE WHEN is_read = false THEN 1 END) as unread
FROM notifications;
