{
	"Comment": "Product scoring and auto-approval step function",
	"StartAt": "validate-parallel",
	"States": {
		"validate-parallel": {
			"Type": "Parallel",
			"Branches": [
				{
					"StartAt": "validate-product-logic",
					"States": {
						"validate-product-logic": {
							"Type": "Task",
							"Resource": "arn:aws:states:::lambda:invoke",
							"Parameters": {
								"FunctionName": "${lambda_prefix}-${validate_product_logic}",
								"Payload.$": "$"
							},
							"ResultSelector": {
								"ExecutedVersion.$": "$.ExecutedVersion",
								"StatusCode.$": "$.StatusCode",
								"Payload.$": "$.Payload"
							},
							"ResultPath": "$.ProductValidationTaskResponse",
							"Retry": [
								{
									"ErrorEquals": [
										"States.ALL"
									],
									"IntervalSeconds": 3,
									"MaxAttempts": 7,
									"BackoffRate": 1.5
								}
							],
							"End": true
						}
					}
				},
				{
					"StartAt": "apple-sg-validator",
					"States": {
						"apple-sg-validator": {
							"Type": "Task",
							"Resource": "arn:aws:states:::states:startExecution.sync:2",
							"Parameters": {
								"StateMachineArn": "${APPLE_SG_VALIDATOR_SFN_ARN}",
								"Input.$": "$"
							},
							"Catch": [
								{
									"ErrorEquals": [
										"States.ALL"
									],
									"Next": "apple-sg-validator-failed",
									"ResultPath": "$.AppleSgError"
								}
							],
							"End": true
						},
						"apple-sg-validator-failed": {
							"Type": "Pass",
							"End": true
						}
					}
				}
			],
			"ResultPath": "$.ParallelTaskOutput",
			"Catch": [
				{
					"ErrorEquals": [
						"States.ALL"
					],
					"Next": "indexing-choice-state",
					"ResultPath": "$.ValidateTaskError"
				}
			],
			"Next": "extract-validation-result"
		},
		"extract-validation-result": {
			"Type": "Pass",
			"InputPath": "$.ParallelTaskOutput[0].ProductValidationTaskResponse",
			"ResultPath": "$.ProductValidationTaskResponse",
			"Next": "check-integration-test"
		},
		"check-integration-test": {
			"Type": "Choice",
			"Choices": [
				{
					"And": [
						{
							"Variable": "$.data.is_integration_test",
							"IsPresent": true
						},
						{
							"Variable": "$.data.is_integration_test",
							"BooleanEquals": true
						}
					],
					"Next": "Success"
				}
			],
			"Default": "is-eligible-for-auto-approval"
		},
		"is-eligible-for-auto-approval": {
			"Type": "Choice",
			"Choices": [
				{
					"And": [
						{
							"Variable": "$.data.use_auto_approval",
							"BooleanEquals": true
						},
						{
							"Variable": "$.ProductValidationTaskResponse.Payload.validations",
							"StringEquals": "pass"
						},
						{
							"Or": [
								{
									"Variable": "$.data.sqs_update_queue_message",
									"IsPresent": false
								},
								{
									"Variable": "$.data.sqs_update_queue_message",
									"IsNull": true
								},
								{
									"Variable": "$.data.sqs_update_queue_message.operation",
									"StringEquals": "add"
								}
							]
						}
					],
					"Next": "auto-approve"
				}
			],
			"Default": "post-results"
		},
		"auto-approve": {
			"Type": "Task",
			"Resource": "arn:aws:states:::lambda:invoke",
			"Parameters": {
				"FunctionName": "${lambda_prefix}-${auto_approve}",
				"Payload": {
					"product_id.$": "$.data.product_id",
					"review_queue_id.$": "$.data.review_queue_id",
					"use_auto_approval.$": "$.data.use_auto_approval"
				}
			},
			"ResultSelector": {
				"ExecutedVersion.$": "$.ExecutedVersion",
				"StatusCode.$": "$.StatusCode",
				"Payload.$": "$.Payload"
			},
			"ResultPath": "$.AutoapproveTaskResponse",
			"Retry": [
				{
					"ErrorEquals": [
						"States.ALL"
					],
					"IntervalSeconds": 3,
					"MaxAttempts": 7,
					"BackoffRate": 1.5
				}
			],
			"Catch": [
				{
					"ErrorEquals": [
						"States.ALL"
					],
					"Next": "post-results",
					"ResultPath": "$.AutoapproveTaskError"
				}
			],
			"Next": "post-results"
		},
		"post-results": {
			"Type": "Task",
			"Resource": "arn:aws:states:::lambda:invoke",
			"Parameters": {
				"FunctionName": "${lambda_prefix}-${log_auto_approve_results}",
				"Payload.$": "$"
			},
			"ResultSelector": {
				"ExecutedVersion.$": "$.ExecutedVersion",
				"StatusCode.$": "$.StatusCode",
				"Payload.$": "$.Payload"
			},
			"ResultPath": "$.PostResultsTaskResponse",
			"Retry": [
				{
					"ErrorEquals": [
						"States.ALL"
					],
					"IntervalSeconds": 3,
					"MaxAttempts": 7,
					"BackoffRate": 1.5
				}
			],
			"Catch": [
				{
					"ErrorEquals": [
						"States.ALL"
					],
					"Next": "indexing-choice-state",
					"ResultPath": "$.PostResultsTaskError"
				}
			],
			"Next": "indexing-choice-state"
		},
		"indexing-choice-state": {
			"Type": "Choice",
			"Choices": [
				{
					"And": [
						{
							"Variable": "$.AutoapproveTaskResponse",
							"IsPresent": true
						}
					],
					"Next": "Do Not Index"
				},
				{
					"And": [
						{
							"Variable": "$.data.sqs_update_queue_message",
							"IsPresent": true
						},
						{
							"Variable": "$.data.sqs_update_queue_message",
							"IsNull": false
						},
						{
							"Variable": "$.AutoapproveTaskResponse",
							"IsPresent": false
						}
					],
					"Next": "publish-sqs-reindex-product"
				},
				{
					"And": [
						{
							"Variable": "$.data.indexing_inputs",
							"IsPresent": true
						},
						{
							"Variable": "$.data.indexing_inputs",
							"IsNull": false
						},
						{
							"Variable": "$.AutoapproveTaskResponse",
							"IsPresent": false
						}
					],
					"Next": "trigger-indexing-sfn"
				}
			],
			"Default": "Do Not Index"
		},
		"Do Not Index": {
			"Type": "Pass",
			"Next": "Check failures"
		},
		"trigger-indexing-sfn": {
			"Type": "Task",
			"Resource": "arn:aws:states:::states:startExecution.sync:2",
			"Parameters": {
				"StateMachineArn": "${INDEXING_STATE_MACHINE_ARN}",
				"Name.$": "States.Format('index-product-metadata-{}', States.UUID())",
				"Input": {
					"data": {
						"indexing_inputs.$": "$.data.indexing_inputs"
					},
					"AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID": "$$.Execution.Id"
				}
			},
			"Retry": [
				{
					"ErrorEquals": [
						"States.ALL"
					],
					"IntervalSeconds": 3,
					"MaxAttempts": 7,
					"BackoffRate": 1.5
				}
			],
			"ResultPath": "$.IndexingTaskResponse",
			"Next": "Check failures"
		},
		"publish-sqs-reindex-product": {
			"Type": "Task",
			"Resource": "arn:aws:states:::sqs:sendMessage",
			"Parameters": {
				"MessageBody": "$.sqs_update_queue_message",
				"QueueUrl": "https://sqs.${REGION}.amazonaws.com/${ACCOUNT_ID}/${ENVIRONMENT}-content-review-index-update-queue"
			},
			"ResultPath": "$.SqsReindexTaskResponse",
			"Next": "Check failures"
		},
		"Check failures": {
			"Type": "Choice",
			"Choices": [
				{
					"Variable": "$.PostResultsTaskError",
					"IsPresent": true,
					"Next": "Fail"
				},
				{
					"Variable": "$.AutoapproveTaskError",
					"IsPresent": true,
					"Next": "Fail"
				},
				{
					"Variable": "$.ValidateTaskError",
					"IsPresent": true,
					"Next": "Fail"
				}
			],
			"Default": "Success"
		},
		"Fail": {
			"Type": "Fail"
		},
		"Success": {
			"Type": "Succeed"
		}
	}
}