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

package sqlc

import (
	"context"
)

const outboxInsert = `-- name: OutboxInsert :exec
INSERT INTO outbox (order_id, topic, "key", payload)
VALUES ($1, $2, $3, $4::jsonb)
`

type OutboxInsertParams struct {
	OrderID int64
	Topic   string
	Key     *string
	Column4 []byte
}

func (q *Queries) OutboxInsert(ctx context.Context, arg *OutboxInsertParams) error {
	_, err := q.db.Exec(ctx, outboxInsert,
		arg.OrderID,
		arg.Topic,
		arg.Key,
		arg.Column4,
	)
	return err
}

const outboxMarkError = `-- name: OutboxMarkError :execrows
UPDATE outbox
SET    status = 'error'
WHERE  id = ANY($1::bigint[])
`

func (q *Queries) OutboxMarkError(ctx context.Context, dollar_1 []int64) (int64, error) {
	result, err := q.db.Exec(ctx, outboxMarkError, dollar_1)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const outboxMarkSent = `-- name: OutboxMarkSent :execrows
UPDATE outbox
SET    status  = 'sent',
       sent_at = now()
WHERE  id = ANY($1::bigint[])
`

func (q *Queries) OutboxMarkSent(ctx context.Context, dollar_1 []int64) (int64, error) {
	result, err := q.db.Exec(ctx, outboxMarkSent, dollar_1)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const outboxSelectForPublish = `-- name: OutboxSelectForPublish :many
SELECT id, order_id, topic, "key", payload
FROM   outbox
WHERE  status = 'new'
ORDER  BY created_at
LIMIT  $1
FOR UPDATE SKIP LOCKED
`

type OutboxSelectForPublishRow struct {
	ID      int64
	OrderID int64
	Topic   string
	Key     *string
	Payload []byte
}

func (q *Queries) OutboxSelectForPublish(ctx context.Context, limit int32) ([]*OutboxSelectForPublishRow, error) {
	rows, err := q.db.Query(ctx, outboxSelectForPublish, limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []*OutboxSelectForPublishRow
	for rows.Next() {
		var i OutboxSelectForPublishRow
		if err := rows.Scan(
			&i.ID,
			&i.OrderID,
			&i.Topic,
			&i.Key,
			&i.Payload,
		); err != nil {
			return nil, err
		}
		items = append(items, &i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
