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
  OrderTable:
    Type: String
    Default: betaeducacao-prod-orders

Globals:
  Function:
    CodeUri: app/
    Runtime: python3.13
    Tracing: Active
    Architectures:
      - x86_64
    Layers:
      - !Sub arn:aws:lambda:sa-east-1:336641857101:layer:layercake:72
    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
        ORDER_TABLE: !Ref OrderTable

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

  EventDelVacanciesFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: events.stopgap.del_vacancies.lambda_handler
      LoggingConfig:
        LogGroup: !Ref EventLog
      Policies:
        - DynamoDBReadPolicy:
            TableName: !Ref UserTable
        - DynamoDBReadPolicy:
            TableName: !Ref OrderTable
        - DynamoDBCrudPolicy:
            TableName: !Ref EnrollmentTable
      Events:
        DynamoDBEvent:
          Type: EventBridgeRule
          Properties:
            Pattern:
              resources: [!Ref OrderTable]
              detail:
                new_image:
                  sk: [generated_items]
                  status: [SUCCESS]

  EventSetStatusAsArchivedFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: events.set_status_as_archived.lambda_handler
      LoggingConfig:
        LogGroup: !Ref EventLog
      Policies:
        - DynamoDBWritePolicy:
            TableName: !Ref EnrollmentTable
      Events:
        DynamoDBEvent:
          Type: EventBridgeRule
          Properties:
            Pattern:
              resources: [!Ref EnrollmentTable]
              detail-type: [EXPIRE]
              detail:
                keys:
                  sk: [schedules#course_archived]

  EventSetStatusAsExpiredFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: events.set_status_as_expired.lambda_handler
      LoggingConfig:
        LogGroup: !Ref EventLog
      Policies:
        - DynamoDBWritePolicy:
            TableName: !Ref EnrollmentTable
      Events:
        DynamoDBEvent:
          Type: EventBridgeRule
          Properties:
            Pattern:
              resources: [!Ref EnrollmentTable]
              detail-type: [EXPIRE]
              detail:
                keys:
                  sk: [schedules#course_expired]
