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

-- 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;

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

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