AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Serverless balance sheet analyzer using Textract and a serverless API

Conditions:
  CreateProdResources: !Equals
    - !Ref AWS::AccountId
    - 572540046516 # Dennis account ID (Production)

Globals:
  Function:
    Runtime: python3.7
    Handler: app.lambda_handler
    Architectures:
      - x86_64
    Timeout: 20
    MemorySize: 128
    Tracing: Active

Resources:
  S3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !If
        - CreateProdResources
        - finance-parser-data
        - sandbox-finance-parser-data
      NotificationConfiguration:
        EventBridgeConfiguration:
          EventBridgeEnabled: true

  StateMachine:
    Type: AWS::Serverless::StateMachine
    Properties:
      Tracing:
        Enabled: true
      DefinitionUri: statemachine/statemachine.asl.json
      DefinitionSubstitutions:
        AnalyzeDocumentFunctionArn: !GetAtt AnalyzeDocumentFunction.Arn
        ProcessDocumentFunctionArn: !GetAtt ProcessDocumentFunction.Arn
        UploadDocumentFunctionArn: !GetAtt UploadDocumentFunction.Arn
      Events:
        StateChange:
          Type: EventBridgeRule
          Properties:
            Pattern:
              source:
                - aws.s3
              detail-type:
                - Object Created
              detail:
                bucket:
                  name:
                    - !Ref S3Bucket
                object:
                  key:
                    - "prefix": "unprocessed/"
    Connectors:
      StateMachineConnector:
        Properties:
          Destination:
            - Id: AnalyzeDocumentFunction
            - Id: ProcessDocumentFunction
            - Id: UploadDocumentFunction
          Permissions:
            - Write

  AnalyzeDocumentFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: analyze_document/
      Handler: app.lambda_handler
      Runtime: python3.7
      Policies:
        - Version: "2012-10-17"
          Statement:
            - Effect: Allow
              Action:
                - textract:AnalyzeDocument
              Resource: "*"
    Connectors:
      S3Connector:
        Properties:
          Destination:
            Id: S3Bucket
          Permissions:
            - Read
            - Write

  ProcessDocumentFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: process_document/
      Handler: app.lambda_handler
      Runtime: python3.7
    Connectors:
      S3Connector:
        Properties:
          Destination:
            Id: S3Bucket
          Permissions:
            - Read
            - Write

  UploadDocumentFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: upload_document/
      Handler: app.lambda_handler
      Runtime: python3.7
    Connectors:
      DynamoConnector:
        Properties:
          Destination:
            Id: DynamoTable
          Permissions:
            - Write
      S3Connector:
        Properties:
          Destination:
            Id: S3Bucket
          Permissions:
            - Read

  DynamoTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: FinanceParser
      BillingMode: PAY_PER_REQUEST
      DeletionProtectionEnabled: !If
        - CreateProdResources
        - True
        - False
      KeySchema:
        - AttributeName: pk
          KeyType: HASH
        - AttributeName: sk
          KeyType: RANGE
      AttributeDefinitions:
        - AttributeName: pk
          AttributeType: S
        - AttributeName: sk
          AttributeType: S