AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: StatiCart thin API — checkout, webhook, stock check

Globals:
  Function:
    Runtime: nodejs20.x
    Timeout: 15
    MemorySize: 256
    Environment:
      Variables:
        DYNAMODB_TABLE: !Ref StatiCartTable
        STRIPE_SECRET_KEY: !Ref StripeSecretKey
        STRIPE_WEBHOOK_SECRET: !Ref StripeWebhookSecret
        BUILD_HOOK_URL: !Ref BuildHookUrl
        SITE_ORIGIN: !Ref SiteOrigin
        JWT_SECRET: !Ref JwtSecret
    Policies:
      - DynamoDBCrudPolicy:
          TableName: !Ref StatiCartTable

Parameters:
  StripeSecretKey:
    Type: String
    NoEcho: true
  StripeWebhookSecret:
    Type: String
    NoEcho: true
  BuildHookUrl:
    Type: String
    Default: ''
  JwtSecret:
    Type: String
    NoEcho: true
  SiteOrigin:
    Type: String
    Default: '*'

Resources:
  StatiCartApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      CorsConfiguration:
        AllowOrigins:
          - !Ref SiteOrigin
        AllowHeaders:
          - Content-Type
          - Stripe-Signature
          - Authorization
        AllowMethods:
          - POST
          - GET
          - OPTIONS
      StageName: prod

  CheckoutFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: checkout.handler
      CodeUri: api/
      Events:
        Post:
          Type: HttpApi
          Properties:
            ApiId: !Ref StatiCartApi
            Path: /api/checkout
            Method: POST

  WebhookFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: webhook.handler
      CodeUri: api/
      Events:
        Post:
          Type: HttpApi
          Properties:
            ApiId: !Ref StatiCartApi
            Path: /api/webhook
            Method: POST

  StockFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: stock.handler
      CodeUri: api/
      Events:
        Get:
          Type: HttpApi
          Properties:
            ApiId: !Ref StatiCartApi
            Path: /api/stock/{sku}
            Method: GET

  OrdersFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: orders.handler
      CodeUri: api/
      Events:
        Get:
          Type: HttpApi
          Properties:
            ApiId: !Ref StatiCartApi
            Path: /api/orders
            Method: GET

  AuthChallengeFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: auth-challenge.handler
      CodeUri: api/
      Events:
        Post:
          Type: HttpApi
          Properties:
            ApiId: !Ref StatiCartApi
            Path: /api/auth/challenge
            Method: POST

  AuthRegisterFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: auth-register.handler
      CodeUri: api/
      Events:
        Post:
          Type: HttpApi
          Properties:
            ApiId: !Ref StatiCartApi
            Path: /api/auth/register
            Method: POST

  AuthVerifyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: auth-verify.handler
      CodeUri: api/
      Events:
        Post:
          Type: HttpApi
          Properties:
            ApiId: !Ref StatiCartApi
            Path: /api/auth/verify
            Method: POST

  SessionFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: session.handler
      CodeUri: api/
      Events:
        Get:
          Type: HttpApi
          Properties:
            ApiId: !Ref StatiCartApi
            Path: /api/session/{id}
            Method: GET

  StatiCartTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: staticart
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: PK
          AttributeType: S
        - AttributeName: SK
          AttributeType: S
        - AttributeName: email
          AttributeType: S
      KeySchema:
        - AttributeName: PK
          KeyType: HASH
        - AttributeName: SK
          KeyType: RANGE
      GlobalSecondaryIndexes:
        - IndexName: email-index
          KeySchema:
            - AttributeName: email
              KeyType: HASH
            - AttributeName: PK
              KeyType: RANGE
          Projection:
            ProjectionType: ALL

Outputs:
  ApiUrl:
    Value: !Sub 'https://${StatiCartApi}.execute-api.${AWS::Region}.amazonaws.com'
