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

package db

import (
	"context"
	"database/sql"
)

const createLog = `-- name: CreateLog :one
INSERT INTO logs (
    id,
    session_id,
    timestamp,
    level,
    message,
    attributes
) VALUES (
    ?,
    ?,
    ?,
    ?,
    ?,
    ?
) RETURNING id, session_id, timestamp, level, message, attributes, created_at, updated_at
`

type CreateLogParams struct {
	ID         string         `json:"id"`
	SessionID  sql.NullString `json:"session_id"`
	Timestamp  string         `json:"timestamp"`
	Level      string         `json:"level"`
	Message    string         `json:"message"`
	Attributes sql.NullString `json:"attributes"`
}

func (q *Queries) CreateLog(ctx context.Context, arg CreateLogParams) (Log, error) {
	row := q.queryRow(ctx, q.createLogStmt, createLog,
		arg.ID,
		arg.SessionID,
		arg.Timestamp,
		arg.Level,
		arg.Message,
		arg.Attributes,
	)
	var i Log
	err := row.Scan(
		&i.ID,
		&i.SessionID,
		&i.Timestamp,
		&i.Level,
		&i.Message,
		&i.Attributes,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const listAllLogs = `-- name: ListAllLogs :many
SELECT id, session_id, timestamp, level, message, attributes, created_at, updated_at FROM logs
ORDER BY timestamp DESC
LIMIT ?
`

func (q *Queries) ListAllLogs(ctx context.Context, limit int64) ([]Log, error) {
	rows, err := q.query(ctx, q.listAllLogsStmt, listAllLogs, limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Log{}
	for rows.Next() {
		var i Log
		if err := rows.Scan(
			&i.ID,
			&i.SessionID,
			&i.Timestamp,
			&i.Level,
			&i.Message,
			&i.Attributes,
			&i.CreatedAt,
			&i.UpdatedAt,
		); 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 listLogsBySession = `-- name: ListLogsBySession :many
SELECT id, session_id, timestamp, level, message, attributes, created_at, updated_at FROM logs
WHERE session_id = ?
ORDER BY timestamp DESC
`

func (q *Queries) ListLogsBySession(ctx context.Context, sessionID sql.NullString) ([]Log, error) {
	rows, err := q.query(ctx, q.listLogsBySessionStmt, listLogsBySession, sessionID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Log{}
	for rows.Next() {
		var i Log
		if err := rows.Scan(
			&i.ID,
			&i.SessionID,
			&i.Timestamp,
			&i.Level,
			&i.Message,
			&i.Attributes,
			&i.CreatedAt,
			&i.UpdatedAt,
		); 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
}
