// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: notification.sql

package dbgen

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const CountUnreadNotifications = `-- name: CountUnreadNotifications :one
SELECT count(id)
FROM notifications
WHERE recipient_id = $1
    AND is_read = false
`

func (q *Queries) CountUnreadNotifications(ctx context.Context, recipientID int64) (int64, error) {
	row := q.db.QueryRow(ctx, CountUnreadNotifications, recipientID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const CreateNotification = `-- 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 id, recipient_id, type, level, error_severity, reciever, is_read, delivery_status, delivery_channel, payload, priority, version, timestamp, metadata
`

type CreateNotificationParams struct {
	ID              string             `json:"id"`
	RecipientID     int64              `json:"recipient_id"`
	Type            string             `json:"type"`
	Level           string             `json:"level"`
	ErrorSeverity   pgtype.Text        `json:"error_severity"`
	Reciever        string             `json:"reciever"`
	IsRead          bool               `json:"is_read"`
	DeliveryStatus  string             `json:"delivery_status"`
	DeliveryChannel pgtype.Text        `json:"delivery_channel"`
	Payload         []byte             `json:"payload"`
	Priority        pgtype.Int4        `json:"priority"`
	Timestamp       pgtype.Timestamptz `json:"timestamp"`
	Metadata        []byte             `json:"metadata"`
}

func (q *Queries) CreateNotification(ctx context.Context, arg CreateNotificationParams) (Notification, error) {
	row := q.db.QueryRow(ctx, CreateNotification,
		arg.ID,
		arg.RecipientID,
		arg.Type,
		arg.Level,
		arg.ErrorSeverity,
		arg.Reciever,
		arg.IsRead,
		arg.DeliveryStatus,
		arg.DeliveryChannel,
		arg.Payload,
		arg.Priority,
		arg.Timestamp,
		arg.Metadata,
	)
	var i Notification
	err := row.Scan(
		&i.ID,
		&i.RecipientID,
		&i.Type,
		&i.Level,
		&i.ErrorSeverity,
		&i.Reciever,
		&i.IsRead,
		&i.DeliveryStatus,
		&i.DeliveryChannel,
		&i.Payload,
		&i.Priority,
		&i.Version,
		&i.Timestamp,
		&i.Metadata,
	)
	return i, err
}

const GetAllNotifications = `-- name: GetAllNotifications :many
SELECT id, recipient_id, type, level, error_severity, reciever, is_read, delivery_status, delivery_channel, payload, priority, version, timestamp, metadata
FROM notifications
ORDER BY timestamp DESC
LIMIT $1 OFFSET $2
`

type GetAllNotificationsParams struct {
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

func (q *Queries) GetAllNotifications(ctx context.Context, arg GetAllNotificationsParams) ([]Notification, error) {
	rows, err := q.db.Query(ctx, GetAllNotifications, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Notification
	for rows.Next() {
		var i Notification
		if err := rows.Scan(
			&i.ID,
			&i.RecipientID,
			&i.Type,
			&i.Level,
			&i.ErrorSeverity,
			&i.Reciever,
			&i.IsRead,
			&i.DeliveryStatus,
			&i.DeliveryChannel,
			&i.Payload,
			&i.Priority,
			&i.Version,
			&i.Timestamp,
			&i.Metadata,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const GetNotification = `-- name: GetNotification :one
SELECT id, recipient_id, type, level, error_severity, reciever, is_read, delivery_status, delivery_channel, payload, priority, version, timestamp, metadata
FROM notifications
WHERE id = $1
LIMIT 1
`

func (q *Queries) GetNotification(ctx context.Context, id string) (Notification, error) {
	row := q.db.QueryRow(ctx, GetNotification, id)
	var i Notification
	err := row.Scan(
		&i.ID,
		&i.RecipientID,
		&i.Type,
		&i.Level,
		&i.ErrorSeverity,
		&i.Reciever,
		&i.IsRead,
		&i.DeliveryStatus,
		&i.DeliveryChannel,
		&i.Payload,
		&i.Priority,
		&i.Version,
		&i.Timestamp,
		&i.Metadata,
	)
	return i, err
}

const GetNotificationCounts = `-- 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
`

type GetNotificationCountsRow struct {
	Total  int64 `json:"total"`
	Read   int64 `json:"read"`
	Unread int64 `json:"unread"`
}

func (q *Queries) GetNotificationCounts(ctx context.Context) ([]GetNotificationCountsRow, error) {
	rows, err := q.db.Query(ctx, GetNotificationCounts)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetNotificationCountsRow
	for rows.Next() {
		var i GetNotificationCountsRow
		if err := rows.Scan(&i.Total, &i.Read, &i.Unread); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const ListFailedNotifications = `-- name: ListFailedNotifications :many
SELECT id, recipient_id, type, level, error_severity, reciever, is_read, delivery_status, delivery_channel, payload, priority, version, timestamp, metadata
FROM notifications
WHERE delivery_status = 'failed'
    AND timestamp < NOW() - INTERVAL '1 hour'
ORDER BY timestamp ASC
LIMIT $1
`

func (q *Queries) ListFailedNotifications(ctx context.Context, limit int32) ([]Notification, error) {
	rows, err := q.db.Query(ctx, ListFailedNotifications, limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Notification
	for rows.Next() {
		var i Notification
		if err := rows.Scan(
			&i.ID,
			&i.RecipientID,
			&i.Type,
			&i.Level,
			&i.ErrorSeverity,
			&i.Reciever,
			&i.IsRead,
			&i.DeliveryStatus,
			&i.DeliveryChannel,
			&i.Payload,
			&i.Priority,
			&i.Version,
			&i.Timestamp,
			&i.Metadata,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const ListNotifications = `-- name: ListNotifications :many
SELECT id, recipient_id, type, level, error_severity, reciever, is_read, delivery_status, delivery_channel, payload, priority, version, timestamp, metadata
FROM notifications
WHERE recipient_id = $1
ORDER BY timestamp DESC
LIMIT $2 OFFSET $3
`

type ListNotificationsParams struct {
	RecipientID int64 `json:"recipient_id"`
	Limit       int32 `json:"limit"`
	Offset      int32 `json:"offset"`
}

func (q *Queries) ListNotifications(ctx context.Context, arg ListNotificationsParams) ([]Notification, error) {
	rows, err := q.db.Query(ctx, ListNotifications, arg.RecipientID, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Notification
	for rows.Next() {
		var i Notification
		if err := rows.Scan(
			&i.ID,
			&i.RecipientID,
			&i.Type,
			&i.Level,
			&i.ErrorSeverity,
			&i.Reciever,
			&i.IsRead,
			&i.DeliveryStatus,
			&i.DeliveryChannel,
			&i.Payload,
			&i.Priority,
			&i.Version,
			&i.Timestamp,
			&i.Metadata,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const ListRecipientIDsByReceiver = `-- name: ListRecipientIDsByReceiver :many
SELECT recipient_id
FROM notifications
WHERE reciever = $1
`

func (q *Queries) ListRecipientIDsByReceiver(ctx context.Context, reciever string) ([]int64, error) {
	rows, err := q.db.Query(ctx, ListRecipientIDsByReceiver, reciever)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []int64
	for rows.Next() {
		var recipient_id int64
		if err := rows.Scan(&recipient_id); err != nil {
			return nil, err
		}
		items = append(items, recipient_id)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const UpdateNotificationStatus = `-- name: UpdateNotificationStatus :one
UPDATE notifications
SET delivery_status = $2,
    is_read = $3,
    metadata = $4
WHERE id = $1
RETURNING id, recipient_id, type, level, error_severity, reciever, is_read, delivery_status, delivery_channel, payload, priority, version, timestamp, metadata
`

type UpdateNotificationStatusParams struct {
	ID             string `json:"id"`
	DeliveryStatus string `json:"delivery_status"`
	IsRead         bool   `json:"is_read"`
	Metadata       []byte `json:"metadata"`
}

func (q *Queries) UpdateNotificationStatus(ctx context.Context, arg UpdateNotificationStatusParams) (Notification, error) {
	row := q.db.QueryRow(ctx, UpdateNotificationStatus,
		arg.ID,
		arg.DeliveryStatus,
		arg.IsRead,
		arg.Metadata,
	)
	var i Notification
	err := row.Scan(
		&i.ID,
		&i.RecipientID,
		&i.Type,
		&i.Level,
		&i.ErrorSeverity,
		&i.Reciever,
		&i.IsRead,
		&i.DeliveryStatus,
		&i.DeliveryChannel,
		&i.Payload,
		&i.Priority,
		&i.Version,
		&i.Timestamp,
		&i.Metadata,
	)
	return i, err
}
