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

package db

import (
	"context"
	"encoding/json"
)

const completeTask = `-- name: CompleteTask :one
UPDATE tasks
SET status = 'completed', result = $2, error = NULL, updated_at = now()
WHERE id::text = $1
RETURNING id, title, source, status, payload, result, error, created_at, updated_at, external_provider, external_id, external_url, external_metadata, metadata
`

type CompleteTaskParams struct {
	ID     string          `json:"id"`
	Result json.RawMessage `json:"result"`
}

func (q *Queries) CompleteTask(ctx context.Context, arg CompleteTaskParams) (Task, error) {
	row := q.db.QueryRow(ctx, completeTask, arg.ID, arg.Result)
	var i Task
	err := row.Scan(
		&i.ID,
		&i.Title,
		&i.Source,
		&i.Status,
		&i.Payload,
		&i.Result,
		&i.Error,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ExternalProvider,
		&i.ExternalID,
		&i.ExternalUrl,
		&i.ExternalMetadata,
		&i.Metadata,
	)
	return i, err
}

const createTask = `-- name: CreateTask :one
INSERT INTO tasks (title, source, payload, metadata, external_provider, external_id, external_url, external_metadata)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (external_provider, external_id) WHERE external_provider IS NOT NULL AND external_id IS NOT NULL
DO UPDATE SET updated_at = tasks.updated_at
RETURNING id, title, source, status, payload, result, error, created_at, updated_at, external_provider, external_id, external_url, external_metadata, metadata
`

type CreateTaskParams struct {
	Title            string          `json:"title"`
	Source           string          `json:"source"`
	Payload          json.RawMessage `json:"payload"`
	Metadata         json.RawMessage `json:"metadata"`
	ExternalProvider *string         `json:"external_provider"`
	ExternalID       *string         `json:"external_id"`
	ExternalUrl      *string         `json:"external_url"`
	ExternalMetadata json.RawMessage `json:"external_metadata"`
}

func (q *Queries) CreateTask(ctx context.Context, arg CreateTaskParams) (Task, error) {
	row := q.db.QueryRow(ctx, createTask,
		arg.Title,
		arg.Source,
		arg.Payload,
		arg.Metadata,
		arg.ExternalProvider,
		arg.ExternalID,
		arg.ExternalUrl,
		arg.ExternalMetadata,
	)
	var i Task
	err := row.Scan(
		&i.ID,
		&i.Title,
		&i.Source,
		&i.Status,
		&i.Payload,
		&i.Result,
		&i.Error,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ExternalProvider,
		&i.ExternalID,
		&i.ExternalUrl,
		&i.ExternalMetadata,
		&i.Metadata,
	)
	return i, err
}

const failTask = `-- name: FailTask :one
UPDATE tasks
SET status = 'failed', error = $2, updated_at = now()
WHERE id::text = $1
RETURNING id, title, source, status, payload, result, error, created_at, updated_at, external_provider, external_id, external_url, external_metadata, metadata
`

type FailTaskParams struct {
	ID    string  `json:"id"`
	Error *string `json:"error"`
}

func (q *Queries) FailTask(ctx context.Context, arg FailTaskParams) (Task, error) {
	row := q.db.QueryRow(ctx, failTask, arg.ID, arg.Error)
	var i Task
	err := row.Scan(
		&i.ID,
		&i.Title,
		&i.Source,
		&i.Status,
		&i.Payload,
		&i.Result,
		&i.Error,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ExternalProvider,
		&i.ExternalID,
		&i.ExternalUrl,
		&i.ExternalMetadata,
		&i.Metadata,
	)
	return i, err
}

const getTask = `-- name: GetTask :one
SELECT id, title, source, status, payload, result, error, created_at, updated_at, external_provider, external_id, external_url, external_metadata, metadata
FROM tasks
WHERE id::text = $1
`

func (q *Queries) GetTask(ctx context.Context, id string) (Task, error) {
	row := q.db.QueryRow(ctx, getTask, id)
	var i Task
	err := row.Scan(
		&i.ID,
		&i.Title,
		&i.Source,
		&i.Status,
		&i.Payload,
		&i.Result,
		&i.Error,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ExternalProvider,
		&i.ExternalID,
		&i.ExternalUrl,
		&i.ExternalMetadata,
		&i.Metadata,
	)
	return i, err
}

const getTaskByExternalRef = `-- name: GetTaskByExternalRef :one
SELECT id, title, source, status, payload, result, error, created_at, updated_at, external_provider, external_id, external_url, external_metadata, metadata
FROM tasks
WHERE external_provider = $1 AND external_id = $2
`

type GetTaskByExternalRefParams struct {
	ExternalProvider *string `json:"external_provider"`
	ExternalID       *string `json:"external_id"`
}

func (q *Queries) GetTaskByExternalRef(ctx context.Context, arg GetTaskByExternalRefParams) (Task, error) {
	row := q.db.QueryRow(ctx, getTaskByExternalRef, arg.ExternalProvider, arg.ExternalID)
	var i Task
	err := row.Scan(
		&i.ID,
		&i.Title,
		&i.Source,
		&i.Status,
		&i.Payload,
		&i.Result,
		&i.Error,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ExternalProvider,
		&i.ExternalID,
		&i.ExternalUrl,
		&i.ExternalMetadata,
		&i.Metadata,
	)
	return i, err
}

const listTasks = `-- name: ListTasks :many
SELECT id, title, source, status, payload, result, error, created_at, updated_at, external_provider, external_id, external_url, external_metadata, metadata
FROM tasks
ORDER BY created_at DESC
LIMIT $1
`

func (q *Queries) ListTasks(ctx context.Context, limit int32) ([]Task, error) {
	rows, err := q.db.Query(ctx, listTasks, limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Task
	for rows.Next() {
		var i Task
		if err := rows.Scan(
			&i.ID,
			&i.Title,
			&i.Source,
			&i.Status,
			&i.Payload,
			&i.Result,
			&i.Error,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.ExternalProvider,
			&i.ExternalID,
			&i.ExternalUrl,
			&i.ExternalMetadata,
			&i.Metadata,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateTaskMetadata = `-- name: UpdateTaskMetadata :one
UPDATE tasks
SET metadata = $2, updated_at = now()
WHERE id::text = $1
RETURNING id, title, source, status, payload, result, error, created_at, updated_at, external_provider, external_id, external_url, external_metadata, metadata
`

type UpdateTaskMetadataParams struct {
	ID       string          `json:"id"`
	Metadata json.RawMessage `json:"metadata"`
}

func (q *Queries) UpdateTaskMetadata(ctx context.Context, arg UpdateTaskMetadataParams) (Task, error) {
	row := q.db.QueryRow(ctx, updateTaskMetadata, arg.ID, arg.Metadata)
	var i Task
	err := row.Scan(
		&i.ID,
		&i.Title,
		&i.Source,
		&i.Status,
		&i.Payload,
		&i.Result,
		&i.Error,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ExternalProvider,
		&i.ExternalID,
		&i.ExternalUrl,
		&i.ExternalMetadata,
		&i.Metadata,
	)
	return i, err
}

const updateTaskStatus = `-- name: UpdateTaskStatus :one
UPDATE tasks
SET status = $2, updated_at = now()
WHERE id::text = $1
RETURNING id, title, source, status, payload, result, error, created_at, updated_at, external_provider, external_id, external_url, external_metadata, metadata
`

type UpdateTaskStatusParams struct {
	ID     string `json:"id"`
	Status string `json:"status"`
}

func (q *Queries) UpdateTaskStatus(ctx context.Context, arg UpdateTaskStatusParams) (Task, error) {
	row := q.db.QueryRow(ctx, updateTaskStatus, arg.ID, arg.Status)
	var i Task
	err := row.Scan(
		&i.ID,
		&i.Title,
		&i.Source,
		&i.Status,
		&i.Payload,
		&i.Result,
		&i.Error,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ExternalProvider,
		&i.ExternalID,
		&i.ExternalUrl,
		&i.ExternalMetadata,
		&i.Metadata,
	)
	return i, err
}
