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

package sqlcv1

import (
	"context"

	"github.com/google/uuid"
	"github.com/jackc/pgx/v5/pgtype"
)

const getLogLinePointMetrics = `-- name: GetLogLinePointMetrics :many
SELECT
    DATE_BIN(
        COALESCE($1::INTERVAL, '1 minute'),
        created_at,
        TIMESTAMPTZ '1970-01-01 00:00:00+00'
    )::TIMESTAMPTZ AS minute_bucket,
    COUNT(*) FILTER (WHERE level = 'DEBUG') AS debug_count,
    COUNT(*) FILTER (WHERE level = 'INFO')  AS info_count,
    COUNT(*) FILTER (WHERE level = 'WARN')  AS warn_count,
    COUNT(*) FILTER (WHERE level = 'ERROR') AS error_count
FROM v1_log_line
WHERE
    tenant_id = $2::UUID
    AND created_at BETWEEN $3::TIMESTAMPTZ AND $4::TIMESTAMPTZ
    AND ($5::TEXT IS NULL OR message ILIKE CONCAT('%', $5::TEXT, '%'))
    AND ($6::v1_log_line_level[] IS NULL OR level = ANY($6::v1_log_line_level[]))
    AND ($7::BIGINT[] IS NULL OR task_id = ANY($7::BIGINT[]))
    AND ($8::UUID[] IS NULL OR workflow_id = ANY($8::UUID[]))
    AND ($9::UUID[] IS NULL OR step_id = ANY($9::UUID[]))
GROUP BY minute_bucket
ORDER BY minute_bucket
`

type GetLogLinePointMetricsParams struct {
	Interval      pgtype.Interval    `json:"interval"`
	Tenantid      uuid.UUID          `json:"tenantid"`
	Createdafter  pgtype.Timestamptz `json:"createdafter"`
	Createdbefore pgtype.Timestamptz `json:"createdbefore"`
	Search        pgtype.Text        `json:"search"`
	Levels        []V1LogLineLevel   `json:"levels"`
	TaskIds       []int64            `json:"taskIds"`
	WorkflowIds   []uuid.UUID        `json:"workflowIds"`
	StepIds       []uuid.UUID        `json:"stepIds"`
}

type GetLogLinePointMetricsRow struct {
	MinuteBucket pgtype.Timestamptz `json:"minute_bucket"`
	DebugCount   int64              `json:"debug_count"`
	InfoCount    int64              `json:"info_count"`
	WarnCount    int64              `json:"warn_count"`
	ErrorCount   int64              `json:"error_count"`
}

func (q *Queries) GetLogLinePointMetrics(ctx context.Context, db DBTX, arg GetLogLinePointMetricsParams) ([]*GetLogLinePointMetricsRow, error) {
	rows, err := db.Query(ctx, getLogLinePointMetrics,
		arg.Interval,
		arg.Tenantid,
		arg.Createdafter,
		arg.Createdbefore,
		arg.Search,
		arg.Levels,
		arg.TaskIds,
		arg.WorkflowIds,
		arg.StepIds,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []*GetLogLinePointMetricsRow
	for rows.Next() {
		var i GetLogLinePointMetricsRow
		if err := rows.Scan(
			&i.MinuteBucket,
			&i.DebugCount,
			&i.InfoCount,
			&i.WarnCount,
			&i.ErrorCount,
		); err != nil {
			return nil, err
		}
		items = append(items, &i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

type InsertLogLineParams struct {
	TenantID       uuid.UUID          `json:"tenant_id"`
	TaskID         int64              `json:"task_id"`
	TaskInsertedAt pgtype.Timestamptz `json:"task_inserted_at"`
	Message        string             `json:"message"`
	Metadata       []byte             `json:"metadata"`
	RetryCount     int32              `json:"retry_count"`
	Level          V1LogLineLevel     `json:"level"`
	WorkflowID     *uuid.UUID         `json:"workflow_id"`
	StepID         *uuid.UUID         `json:"step_id"`
}

const listLogLines = `-- name: ListLogLines :many
SELECT
    id, created_at, tenant_id, task_id, task_inserted_at, message, level, metadata, retry_count, workflow_id, step_id
FROM
    v1_log_line l
WHERE
    l.tenant_id = $1::UUID
    AND ($2::BIGINT[] IS NULL OR l.task_id = ANY($2::BIGINT[]))
    AND ($3::TEXT IS NULL OR l.message ILIKE CONCAT('%', $3::TEXT, '%'))
    AND ($4::TIMESTAMPTZ IS NULL OR l.created_at > $4::TIMESTAMPTZ)
    AND ($5::TIMESTAMPTZ IS NULL OR l.created_at < $5::TIMESTAMPTZ)
    AND ($6::v1_log_line_level[] IS NULL OR l.level = ANY($6::v1_log_line_level[]))
    AND ($7::INTEGER IS NULL OR l.retry_count = ($7::INTEGER - 1))
    AND ($8::UUID[] IS NULL OR l.workflow_id = ANY($8::UUID[]))
    AND ($9::UUID[] IS NULL OR l.step_id = ANY($9::UUID[]))
ORDER BY
    CASE WHEN $10::TEXT = 'DESC' THEN l.created_at END DESC,
    CASE WHEN $10::TEXT = 'ASC' THEN l.created_at END ASC
LIMIT COALESCE($12::BIGINT, 1000)
OFFSET COALESCE($11::BIGINT, 0)
`

type ListLogLinesParams struct {
	Tenantid         uuid.UUID          `json:"tenantid"`
	TaskIds          []int64            `json:"taskIds"`
	Search           pgtype.Text        `json:"search"`
	Since            pgtype.Timestamptz `json:"since"`
	Until            pgtype.Timestamptz `json:"until"`
	Levels           []V1LogLineLevel   `json:"levels"`
	Attempt          pgtype.Int4        `json:"attempt"`
	WorkflowIds      []uuid.UUID        `json:"workflowIds"`
	StepIds          []uuid.UUID        `json:"stepIds"`
	Orderbydirection string             `json:"orderbydirection"`
	Offset           pgtype.Int8        `json:"offset"`
	Limit            pgtype.Int8        `json:"limit"`
}

func (q *Queries) ListLogLines(ctx context.Context, db DBTX, arg ListLogLinesParams) ([]*V1LogLine, error) {
	rows, err := db.Query(ctx, listLogLines,
		arg.Tenantid,
		arg.TaskIds,
		arg.Search,
		arg.Since,
		arg.Until,
		arg.Levels,
		arg.Attempt,
		arg.WorkflowIds,
		arg.StepIds,
		arg.Orderbydirection,
		arg.Offset,
		arg.Limit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []*V1LogLine
	for rows.Next() {
		var i V1LogLine
		if err := rows.Scan(
			&i.ID,
			&i.CreatedAt,
			&i.TenantID,
			&i.TaskID,
			&i.TaskInsertedAt,
			&i.Message,
			&i.Level,
			&i.Metadata,
			&i.RetryCount,
			&i.WorkflowID,
			&i.StepID,
		); err != nil {
			return nil, err
		}
		items = append(items, &i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
