# "org" ensures this Service is used with the correct Serverless Framework Access Key.
org: vitos
# "app" enables Serverless Framework Dashboard features and sharing them with other Services.
app: indra-challenge
# "service" is the name of this project. This will also be added to your AWS resource names.
service: indra-challenge

plugins:
  - serverless-dynamodb
  - serverless-auto-swagger
  - serverless-offline

custom:
  serverless-dynamodb:
    stages:
      - dev
    start:
      port: 4566
      inMemory: true
      migrate: true
  autoswagger:
    title: 'Indra Challenge docs'
    apiType: 'httpApi'
    swaggerFiles:
      - 'src/features/user/docs/openapi.json'
      - 'src/features/swapi/docs/openapi.json'
    swaggerPath: 'docs'
    basePath: '/api'
    schemes: ['https']

stages:
  default:
    params:
      tableName: "users-table-${sls:stage}"

provider:
  name: aws
  runtime: nodejs20.x
  iam:
    role:
      statements:
        - Effect: Allow
          Action:
            - dynamodb:Query
            - dynamodb:Scan
            - dynamodb:GetItem
            - dynamodb:PutItem
            - dynamodb:UpdateItem
            - dynamodb:DeleteItem
          Resource:
            - Fn::GetAtt: [UsersTable, Arn]
  environment:
    ENV: ${sls:stage}
    USERS_TABLE: ${param:tableName}

resources:
  Resources:
    UsersTable:
      Type: AWS::DynamoDB::Table
      Properties:
        AttributeDefinitions:
          - AttributeName: userId
            AttributeType: S
        KeySchema:
          - AttributeName: userId
            KeyType: HASH
        BillingMode: PAY_PER_REQUEST
        TableName: ${param:tableName}

functions:
  GetSpeciesFromSwapiFunction:
    handler: src/features/swapi/handler.getSpecies
    events:
      - httpApi:
          path: /swapi/species
          method: get
  GetSpecieByIdFromSwapiFunction:
    handler: src/features/swapi/handler.getSpecieById
    events:
      - httpApi:
          path: /swapi/species/{specieId}
          method: get
  GetUsersFunction:
    handler: src/features/user/handler.getUsersHandler
    events:
      - httpApi:
          path: /users
          method: get
  GetUserByIdFunction:
    handler: src/features/user/handler.getUserByIdHandler
    events:
      - httpApi:
          path: /users/{userId}
          method: get
  CreateUserFunction:
    handler: src/features/user/handler.createUserHandler
    events:
      - httpApi:
          path: /users
          method: post