---
AWSTemplateFormatVersion: 2010-09-09
Description: Mavvrik AWS Billing Onboarding

Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Label: Inputs
        Parameters: [host, tenant, token, accessType, transferSubjectId]
    ParameterLabels:
      host: { default: Host }
      tenant: { default: Tenant }
      token: { default: Token (Optional) }
      accessType: { default: Access Type (Optional) }
      transferSubjectId: { default: Transfer Subject ID }

Parameters:
  host:
    Type: String
    AllowedPattern: ^https://api\.mavvrik\.(dev|app|ai)$
    ConstraintDescription: must be one of https://api.mavvrik.dev, https://api.mavvrik.app, or https://api.mavvrik.ai
    Description: Mavvrik's API Server Hostname (https://api.mavvrik.dev | .app | .ai)
  tenant:
    Type: String
    MinLength: 3
    Description: Mavvrik Assigned Tenant ID
  token:
    Type: String
    NoEcho: true
    Description: Not supposed to be changed manually. Leave empty if its not automatically populated
  accessType:
    Type: String
    Default: ""
    AllowedPattern: ^(|x-loginasadmin|x-loginasroot)$
    ConstraintDescription: must be empty, x-loginasadmin, or x-loginasroot
    Description: Not supposed to be changed manually. Leave empty if its not automatically populated
  transferSubjectId:
    Type: String
    Default: ""
    AllowedPattern: ^([0-9]+)?$
    ConstraintDescription: must be a numeric Google IAM uniqueId (or empty)
    Description: Not supposed to be changed manually. Auto-populated by the Mavvrik console

Conditions:
  TokenAvailable: !Not [!Equals [!Ref token, ""]]

Resources:
  CURLookupStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://mavvrik-assets.s3.amazonaws.com/cloudwiz-io/billing/cur-lookup.yaml
      Tags: [{ Key: "app", Value: "Mavvrik" }]
      TimeoutInMinutes: 15

  CURDefineStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://mavvrik-assets.s3.amazonaws.com/cloudwiz-io/billing/cur-define.yaml
      Parameters:
        DExTenantId: !Ref tenant
        DExTransferSubjectId: !Ref transferSubjectId
        ExistingCURName: !GetAtt CURLookupStack.Outputs.CUReportName
        ExistingCURS3Bucket: !GetAtt CURLookupStack.Outputs.CURS3Bucket
        ExistingCURS3Prefix: !GetAtt CURLookupStack.Outputs.CURS3Prefix
        NewCURName: !Sub ["mavvrik-${AWS::AccountId}-cur", {}]
        NewCURS3Bucket: !Sub ["mavvrik-${AWS::AccountId}-cur", {}]
        NewCURS3Prefix: cureports
      Tags: [{ Key: "app", Value: "Mavvrik" }]
      TimeoutInMinutes: 15

  OnboardStack:
    Type: AWS::CloudFormation::Stack
    Condition: TokenAvailable
    Properties:
      TemplateURL: https://mavvrik-assets.s3.amazonaws.com/cloudwiz-io/billing/onboard.yaml
      Parameters:
        DExHost: !Ref host
        DExTenantId: !Ref tenant
        DExToken: !Ref token
        DExAccessType: !Ref accessType
        CURName: !GetAtt CURDefineStack.Outputs.CUReportName
        CURS3Bucket: !GetAtt CURDefineStack.Outputs.CURS3Bucket
        CURS3Prefix: !GetAtt CURDefineStack.Outputs.CURS3Prefix
        IAMRole: !GetAtt CURDefineStack.Outputs.IAMRole
        StackId: !Ref "AWS::StackId"
      Tags: [{ Key: "app", Value: "Mavvrik" }]
      TimeoutInMinutes: 20

  UsageAccountOnboarding:
    Type: AWS::CloudFormation::Stack
    DependsOn: OnboardStack
    Properties:
      TemplateURL: https://mavvrik-assets.s3.amazonaws.com/cloudwiz-io/usage/v3-min/template.yaml
      Parameters:
        tenant: !Ref tenant
        token: !Ref token
        accessType: !Ref accessType
        parentStackId: !Ref "AWS::StackId"
        parentStackOrgUnitIds: ""
      Tags: [{ Key: "app", Value: "Mavvrik" }, { Key: "version", Value: "v3-min" }]
      TimeoutInMinutes: 20

Outputs:
  TenantID:
    Value: !Ref tenant
  AccountID:
    Value: !Ref AWS::AccountId
  BillingRoleARN:
    Value: !GetAtt CURDefineStack.Outputs.IAMRole
  UsageRoleARN:
    Value: !GetAtt UsageAccountOnboarding.Outputs.RoleARN
  BucketName:
    Value: !GetAtt CURDefineStack.Outputs.CURS3Bucket
  ReportName:
    Value: !GetAtt CURDefineStack.Outputs.CUReportName
  ReportPathPrefix:
    Value:
      !Sub [
        "${CURS3Prefix}/${CURName}",
        {
          CURS3Prefix: !GetAtt CURDefineStack.Outputs.CURS3Prefix,
          CURName: !GetAtt CURDefineStack.Outputs.CUReportName,
        },
      ]