{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "API Gateway resource stack creation using Amplify CLI",
    <% if (props.dependsOn) { %>
    "Parameters": {
        "authRoleName": {
            "Type":  "String"
        },
        "unauthRoleName": {
            "Type":  "String"
        },
        "env": {
            "Type": "String"
        }<%if (props.dependsOn && props.dependsOn.length > 0) { %>,<% } %>
        <% for(var i=0; i < props.dependsOn.length; i++) { %>
        <% for(var j=0; j < props.dependsOn[i].attributes.length; j++) { %>
          "<%= props.dependsOn[i].category %><%= props.dependsOn[i].resourceName %><%= props.dependsOn[i].attributes[j] %>": {
            "Type": "String",
            "Default": "<%= props.dependsOn[i].category %><%= props.dependsOn[i].resourceName %><%= props.dependsOn[i].attributes[j] %>"
          }<%if (i !== props.dependsOn.length - 1 || j !== props.dependsOn[i].attributes.length - 1) { %>,<% } %>
        
          <% } %>
          <% } %>
    <% } %>
    },
     "Conditions": {
        "ShouldNotCreateEnvResources": {
            "Fn::Equals": [
                {
                    "Ref": "env"
                },
                "NONE"
            ]
        }
    },
    "Resources": {
      "AdminQueries": {
        "Type": "AWS::ApiGateway::RestApi",
        "Properties": {
          "Description": "",
          "Name": "AdminQueries",
          "Body": {
            "swagger": "2.0",
            "info": {
              "version": "2018-05-24T17:52:00Z",
              "title": "AdminQueries"
            },
            "host": {
                "Fn::Join": [
                  "",
                  [
                    "apigateway.",
                    {
                      "Ref": "AWS::Region"
                    },
                    ".amazonaws.com"
                  ]
                ]
              },
            "basePath": {
              "Fn::If": [
                "ShouldNotCreateEnvResources",
                "/Prod",
                {
                  "Fn::Join": [
                      "",
                      [
                          "/",
                          {
                              "Ref": "env"
                          }
                      ]
                  ]
                } 
              ]
            },
            "schemes": [
              "https"
            ],
              "paths": {
                    "/{proxy+}": {
                      "options": {
                        "consumes": [
                          "application/json"
                        ],
                        "produces": [
                          "application/json"
                        ],
                        "responses": {
                          "200": {
                            "description": "200 response",
                            "schema": {
                              "$ref": "#/definitions/Empty"
                            },
                            "headers": {
                              "Access-Control-Allow-Origin": {
                                "type": "string"
                              },
                              "Access-Control-Allow-Methods": {
                                "type": "string"
                              },
                              "Access-Control-Allow-Headers": {
                                "type": "string"
                              }
                            }
                          }
                        },
                        "x-amazon-apigateway-integration": {
                          "responses": {
                            "default": {
                              "statusCode": "200",
                              "responseParameters": {
                                "method.response.header.Access-Control-Allow-Methods": "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'",
                                "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
                                "method.response.header.Access-Control-Allow-Origin": "'*'"
                              }
                            }
                          },
                          "passthroughBehavior": "when_no_match",
                          "requestTemplates": {
                            "application/json": "{\"statusCode\": 200}"
                          },
                          "type": "mock"
                        }
                      },
                      "x-amazon-apigateway-any-method": {
                        "produces": [
                          "application/json"
                        ],
                        "parameters": [
                          {
                            "name": "proxy",
                            "in": "path",
                            "required": true,
                            "type": "string"
                          },
                          {
                            "name": "Authorization",
                            "in": "header",
                            "required": false,
                            "type": "string"
                          }
                        ],
                        "responses": {},
                        "security": [
                          {
                            "Cognito": [
                              "aws.cognito.signin.user.admin"
                            ]
                          }
                        ],
                        "x-amazon-apigateway-request-validator": "Validate query string parameters and headers",
                          "x-amazon-apigateway-integration": {
                            "uri": {
                            "Fn::Join": [
                              "",
                              [
                                "arn:aws:apigateway:",
                                {
                                  "Ref": "AWS::Region"
                                },
                                ":lambda:path/2015-03-31/functions/",
                                {
                                    
                                    "Ref": "function<%= props.functionName %>Arn"
                                },
                                "/invocations"
                              ]
                            ]
                          },
                          "passthroughBehavior": "when_no_match",
                          "httpMethod": "POST",
                          "cacheNamespace": "n40eb9",
                          "cacheKeyParameters": [
                            "method.request.path.proxy"
                          ],
                          "contentHandling": "CONVERT_TO_TEXT",
                          "type": "aws_proxy"
                        }
                      }
                    }
                  },
                  "securityDefinitions": {
                    "Cognito": {
                      "type": "apiKey",
                      "name": "Authorization",
                      "in": "header",
                      "x-amazon-apigateway-authtype": "cognito_user_pools",
                      "x-amazon-apigateway-authorizer": {
                        "providerARNs": [
                            {
                              "Fn::Join": [
                                "",
                                [
                                  "arn:aws:cognito-idp:",
                                  {
                                    "Ref": "AWS::Region"
                                  },
                                  ":",
                                  {
                                    "Ref": "AWS::AccountId"
                                  },
                                  ":userpool/",
                                  {
                                                  
                                    "Ref": "auth<%= props.authResourceName %>UserPoolId"
                                  }

                                ]
                              ]
                            }
                        ],
                        "type": "cognito_user_pools"
                      }
                    }
                  },
                  "definitions": {
                    "Empty": {
                      "type": "object",
                      "title": "Empty Schema"
                    }
                  },
                  "x-amazon-apigateway-request-validators": {
                    "Validate query string parameters and headers": {
                      "validateRequestParameters": true,
                      "validateRequestBody": false
                    }
                  }
                
                }
            }

      },   
      "DeploymentAPIGWAdminQueries": {
        "Type": "AWS::ApiGateway::Deployment",
        "Properties": {
          "Description": "The Development stage deployment of your API.",
          "StageName": {
            "Fn::If": [
              "ShouldNotCreateEnvResources",
              "Prod", 
              {
                "Ref": "env"
              } 
            ]
          },
          "RestApiId": {
            "Ref": "AdminQueries"
          }
        }
      },
      "AdminQueriesAPIGWPolicyForLambda": {
        "Type": "AWS::Lambda::Permission",
        "Properties": {
            "FunctionName": 
                {
                  "Ref": "function<%= props.functionName %>Name"
                },
                
                "Action": "lambda:InvokeFunction",
                "Principal": "apigateway.amazonaws.com",
                "SourceArn": {
                "Fn::Join": [
                    "",
                    [
                        "arn:aws:execute-api:",
                        {
                            "Ref": "AWS::Region"
                        },
                        ":",
                        {
                            "Ref": "AWS::AccountId"
                        },
                        ":",
                        {
                            "Ref": "AdminQueries"
                        },
                        "/*/*/*"
                    ]
                ]
            }
        }
    }
    },
    "Outputs": {
        "RootUrl": {
            "Description": "Root URL of the API gateway",
            "Value": {"Fn::Join": ["", ["https://", {"Ref": "AdminQueries"}, ".execute-api.", {"Ref": "AWS::Region"}, ".amazonaws.com/", {"Fn::If": ["ShouldNotCreateEnvResources","Prod", {"Ref": "env"} ]}]]}
        },
        "ApiName": {
          "Description": "API Friendly name",
          "Value": "AdminQueries"
        },
        "ApiId": {
          "Description": "AdminQueries's API ID",
          "Value": {"Ref": "AdminQueries"}
        }
    }
  }
