AWSTemplateFormatVersion: "2010-09-09"

Description: Amazon Transcribe Post Call Analytics - PCA UI - Api

Transform: AWS::Serverless-2016-10-31

Parameters:
  AudioBucket:
    Type: String

  DataBucket:
    Type: String

  TableName:
    Type: String

  UserPoolId:
    Type: String

Resources:
  Api:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      Auth:
        AddDefaultAuthorizerToCorsPreflight: False
        DefaultAuthorizer: Cognito
        Authorizers:
          Cognito:
            AuthType: COGNITO_USER_POOLS
            UserPoolArn: !Sub arn:aws:cognito-idp:${AWS::Region}:${AWS::AccountId}:userpool/${UserPoolId}
            AuthorizationScopes:
              - openid
      Cors:
        AllowOrigin: "'*'"
        AllowMethods: "'OPTIONS,GET,PUT'"
        AllowHeaders: "'Content-Type,Authorization'"
      GatewayResponses:
        DEFAULT_4XX:
          ResponseParameters:
            Headers:
              Access-Control-Allow-Origin: "'*'"
              Access-Control-Allow-Methods: "'OPTIONS,GET,PUT'"
              Access-Control-Allow-Headers: "'Content-Type,Authorization'"
        DEFAULT_5XX:
          ResponseParameters:
            Headers:
              Access-Control-Allow-Origin: "'*'"
              Access-Control-Allow-Methods: "'OPTIONS,GET,PUT'"
              Access-Control-Allow-Headers: "'Content-Type,Authorization'"

  HeadFunction:
    Type: "AWS::Serverless::Function"
    Properties:
      CodeUri:  ../../src/lambda
      Handler: head.handler
      Environment:
        Variables:
          TableName: !Ref TableName
      Events:
        APIEvent:
          Type: Api
          Properties:
            RestApiId: !Ref Api
            Method: GET
            Path: /head/{key+}
      Policies:
        - DynamoDBReadPolicy:
            TableName: !Ref TableName
      Runtime: nodejs14.x

  GetFunction:
    Type: "AWS::Serverless::Function"
    Properties:
      CodeUri:  ../../src/lambda
      Handler: get.handler
      Environment:
        Variables:
          DataBucket: !Ref DataBucket
          AudioBucket: !Ref AudioBucket
      Events:
        APIEvent:
          Type: Api
          Properties:
            RestApiId: !Ref Api
            Method: GET
            Path: /get/{key+}
      Policies:
        - S3ReadPolicy:
            BucketName: !Ref DataBucket
        - S3ReadPolicy:
            BucketName: !Ref AudioBucket
      Runtime: nodejs14.x

  ListFunction:
    Type: "AWS::Serverless::Function"
    Properties:
      CodeUri:  ../../src/lambda
      Handler: list.handler
      Environment:
        Variables:
          TableName: !Ref TableName
      Events:
        APIEvent:
          Type: Api
          Properties:
            RestApiId: !Ref Api
            Method: GET
            Path: /list
      Policies:
        - DynamoDBReadPolicy:
            TableName: !Ref TableName
      Runtime: nodejs14.x

  SearchFunction:
    Type: "AWS::Serverless::Function"
    Properties:
      CodeUri:  ../../src/lambda
      Handler: search.handler
      Environment:
        Variables:
          TableName: !Ref TableName
      Events:
        APIEvent:
          Type: Api
          Properties:
            RestApiId: !Ref Api
            Method: GET
            Path: /search
      Policies:
        - DynamoDBReadPolicy:
            TableName: !Ref TableName
      Runtime: nodejs14.x

  EntitiesFunction:
    Type: "AWS::Serverless::Function"
    Properties:
      CodeUri:  ../../src/lambda
      Handler: entities.handler
      Environment:
        Variables:
          TableName: !Ref TableName
      Events:
        APIEvent:
          Type: Api
          Properties:
            RestApiId: !Ref Api
            Method: GET
            Path: /entities
      Policies:
        - DynamoDBReadPolicy:
            TableName: !Ref TableName
      Runtime: nodejs14.x

  LanguagesFunction:
    Type: "AWS::Serverless::Function"
    Properties:
      CodeUri:  ../../src/lambda
      Handler: languages.handler
      Environment:
        Variables:
          TableName: !Ref TableName
      Events:
        APIEvent:
          Type: Api
          Properties:
            RestApiId: !Ref Api
            Method: GET
            Path: /languages
      Policies:
        - DynamoDBReadPolicy:
            TableName: !Ref TableName
      Runtime: nodejs14.x

  SwapFunction:
    Type: "AWS::Serverless::Function"
    Properties:
      CodeUri:  ../../src/lambda
      Handler: swap.handler
      Environment:
        Variables:
          TableName: !Ref TableName
          DataBucket: !Ref DataBucket
      Events:
        APIEvent:
          Type: Api
          Properties:
            RestApiId: !Ref Api
            Method: PUT
            Path: /swap/{key+}
      Policies:
        - S3CrudPolicy:
            BucketName: !Ref DataBucket
        - DynamoDBCrudPolicy:
            TableName: !Ref TableName
      Runtime: nodejs14.x

Outputs:
  Uri:
    Value: !Sub https://${Api}.execute-api.${AWS::Region}.amazonaws.com/Prod

  RolesForKMSKey:
    Value: !Join
      - ', '
      - - !Sub '"${GetFunctionRole.Arn}"'
        - !Sub '"${SwapFunctionRole.Arn}"'