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

targetScope = 'subscription'

param version string
param environment Environment
param containerImageTag string
param redisConnection string
param primaryApiKey string
param secondaryApiKey string

param sentryEnvironment string
param sentryDSN string
param sentryEnabled string
param sentryTraceSampleRate string

param timestamp string = utcNow()

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

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

resource mi 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' existing = {
  name: 'mi-redis-api-${environment}'
  scope: rgRedisApi
}

module containerApp 'containerApp.bicep' = {
  name: 'containerApp'
  scope: rgRedisApi
  params: {
    location: location
    environment: environment
    userAssignedIdentityId: mi.id
    containerAppName: 'ca-redis-api-${environment}'
    containerImage: 'acrscandicfrontend.azurecr.io/redis-api:${containerImageTag}'
    containerPort: 3001
    envVars: [
      { name: 'REDIS_CONNECTION', value: redisConnection }
      { name: 'PRIMARY_API_KEY', value: primaryApiKey }
      { name: 'SECONDARY_API_KEY', value: secondaryApiKey }
      { name: 'SENTRY_ENVIRONMENT', value: sentryEnvironment }
      { name: 'SENTRY_DSN', value: sentryDSN }
      { name: 'SENTRY_ENABLED', value: sentryEnabled }
      { name: 'SENTRY_TRACE_SAMPLE_RATE', value: sentryTraceSampleRate }
      { name: 'VERSION', value: version }

      { name: 'timestamp', value: timestamp }
    ]
  }
}
