AWSTemplateFormatVersion: '2010-09-09'
Description: |
  Language Services - S3 Buckets Only
  Use this template to create just the S3 buckets for an existing deployment

Parameters:
  AppName:
    Type: String
    Default: language-services
    Description: Name of the application

  Environment:
    Type: String
    Default: production
    Description: Deployment environment

  InputBucketRetentionDays:
    Type: Number
    Default: 30
    Description: Days to retain files in input bucket

  OutputBucketRetentionDays:
    Type: Number
    Default: 90
    Description: Days to retain files in output bucket

Resources:
  InputBucket:
    Type: AWS::S3::Bucket
    DeletionPolicy: Retain
    UpdateReplacePolicy: Retain
    Properties:
      BucketName: !Sub ${AppName}-${Environment}-input-${AWS::AccountId}
      BucketEncryption:
        ServerSideEncryptionConfiguration:
          - ServerSideEncryptionByDefault:
              SSEAlgorithm: AES256
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true
      VersioningConfiguration:
        Status: Enabled
      LifecycleConfiguration:
        Rules:
          - Id: DeleteOldFiles
            Status: Enabled
            ExpirationInDays: !Ref InputBucketRetentionDays
          - Id: DeleteIncompleteMultipartUploads
            Status: Enabled
            AbortIncompleteMultipartUpload:
              DaysAfterInitiation: 7
      CorsConfiguration:
        CorsRules:
          - AllowedHeaders: ['*']
            AllowedMethods: [GET, PUT, POST, DELETE, HEAD]
            AllowedOrigins: ['*']
            MaxAge: 3000
      Tags:
        - Key: Name
          Value: !Sub ${AppName}-${Environment}-input
        - Key: Application
          Value: !Ref AppName
        - Key: Environment
          Value: !Ref Environment

  OutputBucket:
    Type: AWS::S3::Bucket
    DeletionPolicy: Retain
    UpdateReplacePolicy: Retain
    Properties:
      BucketName: !Sub ${AppName}-${Environment}-output-${AWS::AccountId}
      BucketEncryption:
        ServerSideEncryptionConfiguration:
          - ServerSideEncryptionByDefault:
              SSEAlgorithm: AES256
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true
      VersioningConfiguration:
        Status: Enabled
      LifecycleConfiguration:
        Rules:
          - Id: DeleteOldFiles
            Status: Enabled
            ExpirationInDays: !Ref OutputBucketRetentionDays
          - Id: DeleteIncompleteMultipartUploads
            Status: Enabled
            AbortIncompleteMultipartUpload:
              DaysAfterInitiation: 7
      CorsConfiguration:
        CorsRules:
          - AllowedHeaders: ['*']
            AllowedMethods: [GET, PUT, POST, DELETE, HEAD]
            AllowedOrigins: ['*']
            MaxAge: 3000
      Tags:
        - Key: Name
          Value: !Sub ${AppName}-${Environment}-output
        - Key: Application
          Value: !Ref AppName
        - Key: Environment
          Value: !Ref Environment

Outputs:
  InputBucketName:
    Description: S3 Input Bucket Name
    Value: !Ref InputBucket
    Export:
      Name: !Sub ${AppName}-${Environment}-InputBucket

  InputBucketArn:
    Description: S3 Input Bucket ARN
    Value: !GetAtt InputBucket.Arn

  OutputBucketName:
    Description: S3 Output Bucket Name
    Value: !Ref OutputBucket
    Export:
      Name: !Sub ${AppName}-${Environment}-OutputBucket

  OutputBucketArn:
    Description: S3 Output Bucket ARN
    Value: !GetAtt OutputBucket.Arn
