// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: telemetry.sql

package gen

import (
	"context"
	"database/sql"
	"time"
)

const createTelemetryEvent = `-- name: CreateTelemetryEvent :exec
INSERT INTO telemetry_event (
    id, occurred_at, name, source, level, project_id, session_id, request_id, payload_json
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`

type CreateTelemetryEventParams struct {
	ID          string
	OccurredAt  time.Time
	Name        string
	Source      string
	Level       string
	ProjectID   sql.NullString
	SessionID   sql.NullString
	RequestID   string
	PayloadJson string
}

func (q *Queries) CreateTelemetryEvent(ctx context.Context, arg CreateTelemetryEventParams) error {
	_, err := q.db.ExecContext(ctx, createTelemetryEvent,
		arg.ID,
		arg.OccurredAt,
		arg.Name,
		arg.Source,
		arg.Level,
		arg.ProjectID,
		arg.SessionID,
		arg.RequestID,
		arg.PayloadJson,
	)
	return err
}

const listTelemetryEventsSince = `-- name: ListTelemetryEventsSince :many
SELECT id, occurred_at, name, source, level, project_id, session_id, request_id, payload_json
FROM telemetry_event
WHERE occurred_at >= ?
ORDER BY occurred_at ASC
LIMIT ?
`

type ListTelemetryEventsSinceParams struct {
	OccurredAt time.Time
	Limit      int64
}

func (q *Queries) ListTelemetryEventsSince(ctx context.Context, arg ListTelemetryEventsSinceParams) ([]TelemetryEvent, error) {
	rows, err := q.db.QueryContext(ctx, listTelemetryEventsSince, arg.OccurredAt, arg.Limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []TelemetryEvent{}
	for rows.Next() {
		var i TelemetryEvent
		if err := rows.Scan(
			&i.ID,
			&i.OccurredAt,
			&i.Name,
			&i.Source,
			&i.Level,
			&i.ProjectID,
			&i.SessionID,
			&i.RequestID,
			&i.PayloadJson,
		); 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 pruneTelemetryEventsBefore = `-- name: PruneTelemetryEventsBefore :execrows
DELETE FROM telemetry_event
WHERE id IN (
    SELECT te.id
    FROM telemetry_event te
    WHERE te.occurred_at < ?
    ORDER BY te.occurred_at ASC
    LIMIT ?
)
`

type PruneTelemetryEventsBeforeParams struct {
	OccurredAt time.Time
	Limit      int64
}

func (q *Queries) PruneTelemetryEventsBefore(ctx context.Context, arg PruneTelemetryEventsBeforeParams) (int64, error) {
	result, err := q.db.ExecContext(ctx, pruneTelemetryEventsBefore, arg.OccurredAt, arg.Limit)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}
