// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.30.0
// source: events.sql

package dbgen

import (
	"context"
	"time"
)

const getEventsSince = `-- name: GetEventsSince :many
SELECT seq, event_type, channel_id, payload, created_at
FROM events
WHERE seq > ?
ORDER BY seq ASC
LIMIT ?
`

type GetEventsSinceParams struct {
	Seq   int64 `json:"seq"`
	Limit int64 `json:"limit"`
}

type GetEventsSinceRow struct {
	Seq       int64     `json:"seq"`
	EventType string    `json:"eventType"`
	ChannelID int64     `json:"channelId"`
	Payload   []byte    `json:"payload"`
	CreatedAt time.Time `json:"createdAt"`
}

func (q *Queries) GetEventsSince(ctx context.Context, arg GetEventsSinceParams) ([]GetEventsSinceRow, error) {
	rows, err := q.db.QueryContext(ctx, getEventsSince, arg.Seq, arg.Limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetEventsSinceRow{}
	for rows.Next() {
		var i GetEventsSinceRow
		if err := rows.Scan(
			&i.Seq,
			&i.EventType,
			&i.ChannelID,
			&i.Payload,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getMaxEventSeq = `-- name: GetMaxEventSeq :one
SELECT CAST(COALESCE(MAX(seq), 0) AS INTEGER) AS max_seq FROM events
`

func (q *Queries) GetMaxEventSeq(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, getMaxEventSeq)
	var max_seq int64
	err := row.Scan(&max_seq)
	return max_seq, err
}

const persistEvent = `-- name: PersistEvent :exec
INSERT INTO events (seq, event_type, channel_id, payload) VALUES (?, ?, ?, ?)
`

type PersistEventParams struct {
	Seq       int64  `json:"seq"`
	EventType string `json:"eventType"`
	ChannelID int64  `json:"channelId"`
	Payload   []byte `json:"payload"`
}

// seq is supplied by the hub so the row seq matches the wrapped-payload seq.
func (q *Queries) PersistEvent(ctx context.Context, arg PersistEventParams) error {
	_, err := q.db.ExecContext(ctx, persistEvent,
		arg.Seq,
		arg.EventType,
		arg.ChannelID,
		arg.Payload,
	)
	return err
}

const pruneEventsOlderThan = `-- name: PruneEventsOlderThan :execrows
DELETE FROM events WHERE created_at < ?
`

func (q *Queries) PruneEventsOlderThan(ctx context.Context, createdAt time.Time) (int64, error) {
	result, err := q.db.ExecContext(ctx, pruneEventsOlderThan, createdAt)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}
