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

package sqlcv1

import (
	"context"

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

type CreateDAGDataParams struct {
	DagID              int64              `json:"dag_id"`
	DagInsertedAt      pgtype.Timestamptz `json:"dag_inserted_at"`
	Input              []byte             `json:"input"`
	AdditionalMetadata []byte             `json:"additional_metadata"`
}

const createDAGs = `-- name: CreateDAGs :many
WITH input AS (
    SELECT
        unnest($1::uuid[]) AS tenant_id,
        unnest($2::uuid[]) AS external_id,
        unnest($3::text[]) AS display_name,
        unnest($4::uuid[]) AS workflow_id,
        unnest($5::uuid[]) AS workflow_version_id,
        unnest($6::uuid[]) AS parent_task_external_id,
        unnest($7::jsonb[]) AS desired_worker_labels
)
INSERT INTO v1_dag (
    tenant_id,
    external_id,
    display_name,
    workflow_id,
    workflow_version_id,
    parent_task_external_id,
    desired_worker_labels
)
SELECT
    i.tenant_id,
    i.external_id,
    i.display_name,
    i.workflow_id,
    i.workflow_version_id,
    NULLIF(i.parent_task_external_id, '00000000-0000-0000-0000-000000000000'::uuid),
    i.desired_worker_labels
FROM
    input i
RETURNING
    id, inserted_at, tenant_id, external_id, display_name, workflow_id, workflow_version_id, parent_task_external_id, desired_worker_labels
`

type CreateDAGsParams struct {
	Tenantids             []uuid.UUID `json:"tenantids"`
	Externalids           []uuid.UUID `json:"externalids"`
	Displaynames          []string    `json:"displaynames"`
	Workflowids           []uuid.UUID `json:"workflowids"`
	Workflowversionids    []uuid.UUID `json:"workflowversionids"`
	Parenttaskexternalids []uuid.UUID `json:"parenttaskexternalids"`
	Desiredworkerlabels   [][]byte    `json:"desiredworkerlabels"`
}

func (q *Queries) CreateDAGs(ctx context.Context, db DBTX, arg CreateDAGsParams) ([]*V1Dag, error) {
	rows, err := db.Query(ctx, createDAGs,
		arg.Tenantids,
		arg.Externalids,
		arg.Displaynames,
		arg.Workflowids,
		arg.Workflowversionids,
		arg.Parenttaskexternalids,
		arg.Desiredworkerlabels,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []*V1Dag
	for rows.Next() {
		var i V1Dag
		if err := rows.Scan(
			&i.ID,
			&i.InsertedAt,
			&i.TenantID,
			&i.ExternalID,
			&i.DisplayName,
			&i.WorkflowID,
			&i.WorkflowVersionID,
			&i.ParentTaskExternalID,
			&i.DesiredWorkerLabels,
		); err != nil {
			return nil, err
		}
		items = append(items, &i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getDAGData = `-- name: GetDAGData :many
WITH input AS (
    SELECT
        unnest($1::bigint[]) AS dag_id,
        unnest($2::timestamptz[]) AS dag_inserted_at
)
SELECT dd.dag_id, dd.dag_inserted_at, dd.input, dd.additional_metadata, d.desired_worker_labels, d.external_id
FROM v1_dag d
JOIN v1_dag_data dd ON (d.id, d.inserted_at) = (dd.dag_id, dd.dag_inserted_at)
WHERE (d.id, d.inserted_at) IN (
    SELECT dag_id, dag_inserted_at
    FROM input
)
`

type GetDAGDataParams struct {
	Dagids         []int64              `json:"dagids"`
	Daginsertedats []pgtype.Timestamptz `json:"daginsertedats"`
}

type GetDAGDataRow struct {
	DagID               int64              `json:"dag_id"`
	DagInsertedAt       pgtype.Timestamptz `json:"dag_inserted_at"`
	Input               []byte             `json:"input"`
	AdditionalMetadata  []byte             `json:"additional_metadata"`
	DesiredWorkerLabels []byte             `json:"desired_worker_labels"`
	ExternalID          uuid.UUID          `json:"external_id"`
}

func (q *Queries) GetDAGData(ctx context.Context, db DBTX, arg GetDAGDataParams) ([]*GetDAGDataRow, error) {
	rows, err := db.Query(ctx, getDAGData, arg.Dagids, arg.Daginsertedats)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []*GetDAGDataRow
	for rows.Next() {
		var i GetDAGDataRow
		if err := rows.Scan(
			&i.DagID,
			&i.DagInsertedAt,
			&i.Input,
			&i.AdditionalMetadata,
			&i.DesiredWorkerLabels,
			&i.ExternalID,
		); err != nil {
			return nil, err
		}
		items = append(items, &i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
