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

package sqlcv1

import (
	"context"

	"github.com/google/uuid"
)

const listWorkflowsByNames = `-- name: ListWorkflowsByNames :many
SELECT DISTINCT ON("workflowId")
    "workflowId",
    workflowVersions."id" AS "workflowVersionId",
    workflow."name" AS "workflowName"
FROM
    "WorkflowVersion" as workflowVersions
JOIN
    "Workflow" as workflow ON workflow."id" = workflowVersions."workflowId"
WHERE
    workflow."tenantId" = $1::uuid
    AND workflowVersions."deletedAt" IS NULL
    AND workflow."name" = ANY($2::text[])
ORDER BY "workflowId", "order" DESC
`

type ListWorkflowsByNamesParams struct {
	Tenantid      uuid.UUID `json:"tenantid"`
	Workflownames []string  `json:"workflownames"`
}

type ListWorkflowsByNamesRow struct {
	WorkflowId        uuid.UUID `json:"workflowId"`
	WorkflowVersionId uuid.UUID `json:"workflowVersionId"`
	WorkflowName      string    `json:"workflowName"`
}

func (q *Queries) ListWorkflowsByNames(ctx context.Context, db DBTX, arg ListWorkflowsByNamesParams) ([]*ListWorkflowsByNamesRow, error) {
	rows, err := db.Query(ctx, listWorkflowsByNames, arg.Tenantid, arg.Workflownames)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []*ListWorkflowsByNamesRow
	for rows.Next() {
		var i ListWorkflowsByNamesRow
		if err := rows.Scan(&i.WorkflowId, &i.WorkflowVersionId, &i.WorkflowName); err != nil {
			return nil, err
		}
		items = append(items, &i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listWorkflowsForEvents = `-- name: ListWorkflowsForEvents :many
WITH latest_versions AS (
    SELECT DISTINCT ON("workflowId")
        "workflowId",
        workflowVersions."id" AS "workflowVersionId",
        workflow."name" AS "workflowName"
    FROM
        "WorkflowVersion" as workflowVersions
    JOIN
        "Workflow" as workflow ON workflow."id" = workflowVersions."workflowId"
    WHERE
        workflow."tenantId" = $1::uuid
        AND workflowVersions."deletedAt" IS NULL
    ORDER BY "workflowId", "order" DESC
), event_keys AS (
    SELECT
        UNNEST($2::TEXT[]) AS event_key
)

SELECT
    latest_versions."workflowVersionId",
    latest_versions."workflowId",
    latest_versions."workflowName",
    eventRef."eventKey" as "workflowTriggeringEventKeyPattern",
    k.event_key::TEXT as "incomingEventKey"
FROM
    latest_versions
JOIN
    "WorkflowTriggers" as triggers ON triggers."workflowVersionId" = latest_versions."workflowVersionId"
JOIN
    "WorkflowTriggerEventRef" as eventRef ON eventRef."parentId" = triggers."id"
JOIN event_keys k ON k.event_key LIKE REPLACE(eventRef."eventKey", '*', '%')
`

type ListWorkflowsForEventsParams struct {
	Tenantid  uuid.UUID `json:"tenantid"`
	Eventkeys []string  `json:"eventkeys"`
}

type ListWorkflowsForEventsRow struct {
	WorkflowVersionId                 uuid.UUID `json:"workflowVersionId"`
	WorkflowId                        uuid.UUID `json:"workflowId"`
	WorkflowName                      string    `json:"workflowName"`
	WorkflowTriggeringEventKeyPattern string    `json:"workflowTriggeringEventKeyPattern"`
	IncomingEventKey                  string    `json:"incomingEventKey"`
}

// Get all of the latest workflow versions
// select the workflow versions that have the event trigger
func (q *Queries) ListWorkflowsForEvents(ctx context.Context, db DBTX, arg ListWorkflowsForEventsParams) ([]*ListWorkflowsForEventsRow, error) {
	rows, err := db.Query(ctx, listWorkflowsForEvents, arg.Tenantid, arg.Eventkeys)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []*ListWorkflowsForEventsRow
	for rows.Next() {
		var i ListWorkflowsForEventsRow
		if err := rows.Scan(
			&i.WorkflowVersionId,
			&i.WorkflowId,
			&i.WorkflowName,
			&i.WorkflowTriggeringEventKeyPattern,
			&i.IncomingEventKey,
		); err != nil {
			return nil, err
		}
		items = append(items, &i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
