import { Environment, EnvironmentVar } from 'types.bicep'

targetScope = 'subscription'

param environment Environment
param containerImageTag string = 'latest'
param version string

param primaryApiKey string
param secondaryApiKey string

param sentryDSN string
param sentryEnabled string
param sentryTraceSampleRate string

@description('The location for the resource group')
param location string = 'westeurope'

resource rgRedisApi 'Microsoft.Resources/resourceGroups@2021-04-01' = {
  name: 'rg-redis-api-${environment}'
  location: location
}

module mi 'managedIdentity.bicep' = {
  name: 'mi-redis-api-${environment}'
  scope: rgRedisApi
  params: {
    principalName: 'mi-redis-api-${environment}'
    location: location
  }
}

module redis 'cache/redis.bicep' = {
  name: 'redisCache'
  scope: rgRedisApi
  params: {
    location: location
    environment: environment
  }
}

module containerApp 'app/main.bicep' = {
  name: 'containerApp'
  params: {
    location: location
    environment: environment
    containerImageTag: containerImageTag
    redisConnection: 'default:${redis.outputs.primaryAccessKey}@${redis.outputs.hostname}:6380'
    primaryApiKey: primaryApiKey
    secondaryApiKey: secondaryApiKey
    sentryEnvironment: environment == 'prod' ? 'production' : 'test'
    sentryDSN: sentryDSN
    sentryEnabled: sentryEnabled
    sentryTraceSampleRate: sentryTraceSampleRate
    version: version
  }
}
