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

package db

import (
	"context"

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

const createTaskToken = `-- name: CreateTaskToken :one
INSERT INTO task_token (token_hash, task_id, agent_id, workspace_id, user_id, expires_at)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, token_hash, task_id, agent_id, workspace_id, user_id, expires_at, created_at
`

type CreateTaskTokenParams struct {
	TokenHash   string             `json:"token_hash"`
	TaskID      pgtype.UUID        `json:"task_id"`
	AgentID     pgtype.UUID        `json:"agent_id"`
	WorkspaceID pgtype.UUID        `json:"workspace_id"`
	UserID      pgtype.UUID        `json:"user_id"`
	ExpiresAt   pgtype.Timestamptz `json:"expires_at"`
}

func (q *Queries) CreateTaskToken(ctx context.Context, arg CreateTaskTokenParams) (TaskToken, error) {
	row := q.db.QueryRow(ctx, createTaskToken,
		arg.TokenHash,
		arg.TaskID,
		arg.AgentID,
		arg.WorkspaceID,
		arg.UserID,
		arg.ExpiresAt,
	)
	var i TaskToken
	err := row.Scan(
		&i.ID,
		&i.TokenHash,
		&i.TaskID,
		&i.AgentID,
		&i.WorkspaceID,
		&i.UserID,
		&i.ExpiresAt,
		&i.CreatedAt,
	)
	return i, err
}

const deleteExpiredTaskTokens = `-- name: DeleteExpiredTaskTokens :exec
DELETE FROM task_token WHERE expires_at <= now()
`

func (q *Queries) DeleteExpiredTaskTokens(ctx context.Context) error {
	_, err := q.db.Exec(ctx, deleteExpiredTaskTokens)
	return err
}

const deleteTaskTokensByTask = `-- name: DeleteTaskTokensByTask :exec
DELETE FROM task_token WHERE task_id = $1
`

func (q *Queries) DeleteTaskTokensByTask(ctx context.Context, taskID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteTaskTokensByTask, taskID)
	return err
}

const getTaskTokenByHash = `-- name: GetTaskTokenByHash :one
SELECT id, token_hash, task_id, agent_id, workspace_id, user_id, expires_at, created_at FROM task_token
WHERE token_hash = $1 AND expires_at > now()
`

func (q *Queries) GetTaskTokenByHash(ctx context.Context, tokenHash string) (TaskToken, error) {
	row := q.db.QueryRow(ctx, getTaskTokenByHash, tokenHash)
	var i TaskToken
	err := row.Scan(
		&i.ID,
		&i.TokenHash,
		&i.TaskID,
		&i.AgentID,
		&i.WorkspaceID,
		&i.UserID,
		&i.ExpiresAt,
		&i.CreatedAt,
	)
	return i, err
}
