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

Globals:
  Function:
    MemorySize: 1024
    Architectures: ["x86_64"]
    Runtime: provided.al2
    Timeout: 30
    Tracing: Active
    Environment:
      Variables:
        PRODUCT_TABLE_NAME: !Ref Table

Resources:
  GetProductsFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./
      Handler: bootstrap
      Events:
        Api:
          Type: HttpApi
          Properties:
            Path: /
            Method: GET
      Policies:
        - DynamoDBReadPolicy:
            TableName:
              !Ref Table
    Metadata:
      BuildMethod: makefile

  GetProductFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./
      Handler: GetProduct
      Events:
        Api:
          Type: HttpApi
          Properties:
            Path: /{id}
            Method: GET
      Policies:
        - Version: "2012-10-17"
          Statement:
            - Effect: Allow
              Action: dynamodb:GetItem
              Resource: !GetAtt Table.Arn
    Metadata:
      BuildMethod: makefile

  DeleteProductFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./
      Handler: DeleteProduct
      Events:
        Api:
          Type: HttpApi
          Properties:
            Path: /{id}
            Method: DELETE
      Policies:
        - Version: "2012-10-17"
          Statement:
            - Effect: Allow
              Action:
                - dynamodb:DeleteItem
                - dynamodb:GetItem
              Resource: !GetAtt Table.Arn
    Metadata:
      BuildMethod: makefile

  PutProductFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./
      Handler: PutProduct
      Events:
        Api:
          Type: HttpApi
          Properties:
            Path: /{id}
            Method: PUT
      Policies:
        - Version: "2012-10-17"
          Statement:
            - Effect: Allow
              Action: dynamodb:PutItem
              Resource: !GetAtt Table.Arn
    Metadata:
      BuildMethod: makefile

  GenerateLoadTestResults:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./GenerateLoadTestResults/
      Handler: GenerateLoadTestResults::GenerateLoadTestResults.Function::FunctionHandler
      Runtime: dotnet6
      Events:
        Api:
          Type: HttpApi
          Properties:
            Path: /test-results
            Method: GET
      Environment:
        Variables:
          LOG_GROUP_PREFIX: !Sub "/aws/lambda/net-8-base-"
          LOAD_TEST_TYPE: "NET 8"
          LAMBDA_ARCHITECTURE: "x86_64"
      Policies:
        - Version: "2012-10-17"
          Statement:
            - Sid: AllowStartQueries
              Effect: Allow
              Action:
                - logs:DescribeLogGroups
                - logs:StartQuery
              Resource: "*"
            - Sid: AllowGetQueryResults
              Effect: Allow
              Action: logs:GetQueryResults
              Resource: "*"

  Table:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      BillingMode: PAY_PER_REQUEST
      KeySchema:
        - AttributeName: id
          KeyType: HASH

Outputs:
  ApiUrl:
    Description: "API Gateway endpoint URL"
    Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com/"