module "tags" {
  source = "../../../modules/tags/defaults_plat_env"

  env_prefix    = var.env_prefix
  project_group = local.project_group
}
locals {
  // Constants
  project_group = "delphi"

  name_prefix         = "${var.env_prefix}-${local.project_group}"
  account_id          = data.aws_caller_identity.current.account_id
  secrets_name_prefix = "${local.project_group}/${var.env_prefix}"
  aggregated_tag      = module.tags.aggregated_tag

  common_tags = module.tags.default_tags

  docker_image_env_tag = {
    dev   = "develop",
    qa    = "qa",
    stage = "release",
    uat   = "uat",
    prod  = "master",
  }
}

data "aws_caller_identity" "current" {}

module "aws_accounts_list" {
  source = "../../../modules/iam/aws_accounts_list"
}

data "aws_iam_role" "api_role" {
  name = "${local.name_prefix}-api_role"
}

module "api" {
  source = "../../modules/api"

  env_prefix   = var.env_prefix
  env_suffix   = "mobile"
  api_role_arn = data.aws_iam_role.api_role.arn

  api_repo_url  = data.aws_ecr_repository.api.repository_url
  api_image_tag = "stage-mobile"
  api_dns       = "stage-mobile-delphi-api.delphiplatform.io"

  nginx_repo_url  = data.aws_ecr_repository.nginx.repository_url
  nginx_image_tag = "stage-mobile"
  nginx_dns       = "stage-mobile-delphi-api-ext.delphiplatform.io"


  api_datadog_enabled   = true
  nginx_datadog_enabled = true

  autoscaling_enabled   = true
  autoscaling_max_tasks = 10
  autoscaling_min_tasks = 1
  default_task_count    = 1
  autoscaling_memory    = false

  enable_execute_command = true
  init_process_enabled   = true

  api_env = {
    #"AUTH0_API_AUDIENCE"           = "stage-mobile-delphi-api"
    "ENABLE_FAKE_DATA"          = "false"
    "MAIN_DATABASE_SECRET_NAME" = "delphi/stage/api/frozen/db_params"
    "SPOTIFY_API_SECRET_NAME"   = "delphi/stage/apd/spotify"
  }
  nginx_env = {
    "ACCESS_LOG_STATUS"       = "/var/log/nginx/access.log json_custom"
    "PROXY_CACHE_INACTIVE"    = "15m"
    "DISABLED_ENDPOINT"       = "/v3/ads/"
    "ADS_PERFOMANCE_ENDPOINT" = "/non/existing/endpoint"
  }

  tags = merge(
    local.common_tags,
    {
      project                  = "Apps API",
      service                  = "ECS",
      plat_env_project_service = "${local.aggregated_tag}_APPA_ECS"
    }
  )
}
