AWSTemplateFormatVersion: '2010-09-09'
Conditions:
  IsProd:
    Fn::Equals:
      - Ref: StageName
      - production
Globals:
  Function:
    Environment:
      Variables:
        TABLE_ACCOUNT:
          Ref: TableAccount
        TABLE_APPLICATION_DATA:
          Ref: TableApplicationData
        TABLE_USER_SESSIONS:
          Ref: TableUserSessions
    Handler: index.handler
    MemorySize: 256
    Runtime: nodejs18.x
    Timeout: 30
    Tracing: Active
Outputs:
  TableAccount:
    Description: The name of the DynamoDB table for TableAccount
    Export:
      Name:
        Fn::Sub: ${AWS::StackName}-TableAccount
    Value:
      Ref: TableAccount
  TableApplicationData:
    Description: The name of the DynamoDB table for TableApplicationData
    Export:
      Name:
        Fn::Sub: ${AWS::StackName}-TableApplicationData
    Value:
      Ref: TableApplicationData
  TableUserSessions:
    Description: The name of the DynamoDB table for TableUserSessions
    Export:
      Name:
        Fn::Sub: ${AWS::StackName}-TableUserSessions
    Value:
      Ref: TableUserSessions
Parameters:
  StageName:
    AllowedValues:
      - development
      - production
      - test
    Default: development
    Description: The name of the stage
    Type: String
Resources:
  TableAccount:
    Properties:
      AttributeDefinitions:
        - AttributeName: pk
          AttributeType: S
        - AttributeName: sk
          AttributeType: S
        - AttributeName: gsi1pk
          AttributeType: S
        - AttributeName: gsi1sk
          AttributeType: S
      BillingMode: PAY_PER_REQUEST
      GlobalSecondaryIndexes:
        - IndexName: gsi1
          KeySchema:
            - AttributeName: gsi1pk
              KeyType: HASH
            - AttributeName: gsi1sk
              KeyType: RANGE
          Projection:
            ProjectionType: ALL
      KeySchema:
        - AttributeName: pk
          KeyType: HASH
        - AttributeName: sk
          KeyType: RANGE
      PointInTimeRecoverySpecification:
        PointInTimeRecoveryEnabled:
          Fn::If:
            - IsProd
            - true
            - false
      SSESpecification:
        SSEEnabled:
          Fn::If:
            - IsProd
            - true
            - false
      Tags:
        - Key: StageName
          Value:
            Ref: StageName
        - Key: TableName
          Value: TableAccount
    Type: AWS::DynamoDB::Table
  TableApplicationData:
    Properties:
      AttributeDefinitions:
        - AttributeName: pk
          AttributeType: S
        - AttributeName: sk
          AttributeType: S
        - AttributeName: gsi1pk
          AttributeType: S
        - AttributeName: gsi1sk
          AttributeType: S
        - AttributeName: token
          AttributeType: S
        - AttributeName: publicId
          AttributeType: S
      BillingMode: PAY_PER_REQUEST
      GlobalSecondaryIndexes:
        - IndexName: gsi1
          KeySchema:
            - AttributeName: gsi1pk
              KeyType: HASH
            - AttributeName: gsi1sk
              KeyType: RANGE
          Projection:
            ProjectionType: KEYS_ONLY
        - IndexName: token
          KeySchema:
            - AttributeName: token
              KeyType: HASH
          Projection:
            ProjectionType: ALL
        - IndexName: publicId
          KeySchema:
            - AttributeName: publicId
              KeyType: HASH
          Projection:
            ProjectionType: KEYS_ONLY
      KeySchema:
        - AttributeName: pk
          KeyType: HASH
        - AttributeName: sk
          KeyType: RANGE
      PointInTimeRecoverySpecification:
        PointInTimeRecoveryEnabled:
          Fn::If:
            - IsProd
            - true
            - false
      SSESpecification:
        SSEEnabled:
          Fn::If:
            - IsProd
            - true
            - false
      StreamSpecification:
        StreamViewType: NEW_AND_OLD_IMAGES
      Tags:
        - Key: StageName
          Value:
            Ref: StageName
        - Key: TableName
          Value: TableApplicationData
    Type: AWS::DynamoDB::Table
  TableUserSessions:
    Properties:
      AttributeDefinitions:
        - AttributeName: pk
          AttributeType: S
        - AttributeName: publicId
          AttributeType: S
      BillingMode: PAY_PER_REQUEST
      GlobalSecondaryIndexes:
        - IndexName: publicId
          KeySchema:
            - AttributeName: publicId
              KeyType: HASH
          Projection:
            ProjectionType: ALL
      KeySchema:
        - AttributeName: pk
          KeyType: HASH
      SSESpecification:
        SSEEnabled:
          Fn::If:
            - IsProd
            - true
            - false
      StreamSpecification:
        StreamViewType: NEW_AND_OLD_IMAGES
      Tags:
        - Key: StageName
          Value:
            Ref: StageName
        - Key: TableName
          Value: TableUserSessions
      TimeToLiveSpecification:
        AttributeName: ttl
        Enabled: true
    Type: AWS::DynamoDB::Table
Transform: AWS::Serverless-2016-10-31
