AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31

Parameters:
  UserTable:
    Type: String
    Default: betaeducacao-prod-users_d2o3r5gmm4it7j
  EnrollmentTable:
    Type: String
    Default: betaeducacao-prod-enrollments
  CourseTable:
    Type: String
    Default: saladeaula_courses

Globals:
  Function:
    CodeUri: app/
    Runtime: python3.13
    Tracing: Active
    Architectures:
      - x86_64
    Layers:
      - !Sub arn:aws:lambda:sa-east-1:336641857101:layer:layercake:100
    Environment:
      Variables:
        TZ: America/Sao_Paulo
        LOG_LEVEL: DEBUG
        DYNAMODB_PARTITION_KEY: id
        POWERTOOLS_LOGGER_SAMPLE_RATE: 0.1
        POWERTOOLS_LOGGER_LOG_EVENT: true
        USER_TABLE: !Ref UserTable
        ENROLLMENT_TABLE: !Ref EnrollmentTable
        COURSE_TABLE: !Ref CourseTable
        KONVIVA_API_URL: https://lms.saladeaula.digital
        KONVIVA_SECRET_KEY: '{{resolve:ssm:/betaeducacao/konviva/secret_key/str}}'

Resources:
  EventLog:
    Type: AWS::Logs::LogGroup
    Properties:
      RetentionInDays: 90

  HttpLog:
    Type: AWS::Logs::LogGroup
    Properties:
      RetentionInDays: 90

  HttpApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      CorsConfiguration:
        AllowOrigins: ['*']
        AllowMethods: [POST, OPTIONS]
        AllowHeaders: [Content-Type, X-Requested-With]

  HttpApiFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: app.lambda_handler
      LoggingConfig:
        LogGroup: !Ref HttpLog
      Policies:
        - DynamoDBCrudPolicy:
            TableName: !Ref EnrollmentTable
        - DynamoDBReadPolicy:
            TableName: !Ref CourseTable
      Events:
        Post:
          Type: HttpApi
          Properties:
            Path: /
            Method: POST
            ApiId: !Ref HttpApi

  EventCreateUserFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: events.create_user.lambda_handler
      LoggingConfig:
        LogGroup: !Ref EventLog
      Policies:
        - DynamoDBWritePolicy:
            TableName: !Ref UserTable
      Events:
        DynamoDBEvent:
          Type: EventBridgeRule
          Properties:
            Pattern:
              resources: [!Ref UserTable]
              detail-type: [INSERT]
              detail:
                new_image:
                  sk: ['0']
                  cnpj:
                    - exists: false
                  metadata__konviva_user_id:
                    - exists: false

  EventCreateOrgFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: events.create_org.lambda_handler
      LoggingConfig:
        LogGroup: !Ref EventLog
      Policies:
        - DynamoDBWritePolicy:
            TableName: !Ref UserTable
      Events:
        DynamoDBEvent:
          Type: EventBridgeRule
          Properties:
            Pattern:
              resources: [!Ref UserTable]
              detail-type: [INSERT]
              detail:
                new_image:
                  sk: ['0']
                  cnpj:
                    - exists: true

  EventUpdateUserFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: events.update_user.lambda_handler
      LoggingConfig:
        LogGroup: !Ref EventLog
      Policies:
        - DynamoDBReadPolicy:
            TableName: !Ref UserTable
      Events:
        DynamoDBEvent:
          Type: EventBridgeRule
          Properties:
            Pattern:
              resources: [!Ref UserTable]
              detail-type: [MODIFY]
              detail:
                new_image:
                  sk: ['0']
                  metadata__konviva_user_id:
                    - exists: true
                changes: [name, email, cpf]

  EventEnrollFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: events.enroll.lambda_handler
      LoggingConfig:
        LogGroup: !Ref EventLog
      Policies:
        - DynamoDBWritePolicy:
            TableName: !Ref EnrollmentTable
      Events:
        DynamoDBEvent:
          Type: EventBridgeRule
          Properties:
            Pattern:
              resources: [!Ref EnrollmentTable]
              detail-type: [INSERT]
              detail:
                new_image:
                  sk: [konviva, KONVIVA]
                  enrollment_id:
                    - exists: false

  EventCancelFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: events.cancel.lambda_handler
      LoggingConfig:
        LogGroup: !Ref EventLog
      Policies:
        - DynamoDBReadPolicy:
            TableName: !Ref EnrollmentTable
      Events:
        DynamoDBEvent:
          Type: EventBridgeRule
          Properties:
            Pattern:
              resources: [!Ref EnrollmentTable]
              detail-type: [MODIFY]
              detail:
                new_image:
                  sk: ['0']
                  status: [CANCELED]
                old_image:
                  status: [PENDING]
