module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=2.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
  team_name          = var.team_name
}

provider "aws" {
  region = var.region

  default_tags {
    tags = module.default_tags.tags
  }
}

provider "aws" {
  region  = var.region
  alias   = "networking"
  profile = "networking"

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "qa-songwhip-terraform-state"
    key     = "qa/songwhip-admin/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

module "vpc_info" {
  source = "git@github.com:theorchard/terraform-vpc-info.git?ref=3.1.0"

  environment = var.environment
}

module "songwhip_admin_sentry_project" {
  source = "git@github.com:theorchard/terraform-sentry.git//?ref=5.0.0"

  environment        = var.environment
  platform           = "javascript"
  service_name       = var.service_name
  application_family = var.application_family
}

module "songwhip_admin_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=6.4.1"

  providers = {
    aws.dns = aws.networking
  }

  application_family                       = var.application_family
  environment                              = var.environment
  service_name                             = var.service_name
  secrets_manager_service_name             = "songwhip-api"
  aws_region                               = var.region
  commit_sha                               = "latest"
  container_port                           = "8080"
  task_type                                = "web_service"
  desired_task_count                       = 2
  minimum_capacity                         = 2
  maximum_capacity                         = 2
  task_cpu                                 = 2048
  task_memory                              = 4096
  datadog_task_memory                      = 512
  fluentbit_task_memory                    = 256
  load_balancer_access_logs_s3_bucket_name = "qa-songwhip-lb-logs"
  blocking_waf_enabled                     = true
  https_listener_certificate_id            = "6b43a296-ec6b-4168-81ca-dbdf718ec29a"
  route53_record_creation_enabled          = false # Disable automatic Route53 record creation since the module prepends "qa-songwhip-" to the record name, which is superfluous.
  vpc_id                                   = module.vpc_info.vpc_id
  fargate_service_subnets                  = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets                    = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    { TZ = "UTC" },
    { PORT = "8080" },
    { ENV = var.environment },
    { NODE_ENV = "production" },
    { SONGWHIP_ENV = "staging" },
    { DD_TRACE_ENABLED = "true" },
    { DD_LOGS_INJECTION = "true" },
    { TRUSTED_ORIGINS = "https://songwhip.qapdesuite.com,http://localhost:8080" },
    { AUTH0_DOMAIN = "qa-orchard.auth0.com" },
    { AUTH0_CLIENT_ID = "aSFSFtIDItx09RhSAHDOa3UlupVkBoEc" },
    { AUTH0_ISSUER_BASE_URL = "https://qalogin.theorchard.com" },
    { AUTH0_M2M_CLIENT_ID = "3c91h3aTp3xNvk1Wj5gHHjLm1rl7VlCP" },
    { AUTH0_M2M_AUDIENCE = "https://workstation.qaorch.com/api" },
    { AUTH0_M2M_BASE_URL = "https://qa-orchard.auth0.com" },
    { CLOUDFLARE_ACCOUNT_EMAIL = "songwhip_cf_service@theorchard.com" },
    { CLOUDFLARE_ACCOUNT_ID = "229e27eb2e8337e21e3a1e26b1daba51" },
    { CLOUDFLARE_CUSTOM_DOMAINS_WORKER_NAME = "songwhip-web-staging" },
    { CLOUDFLARE_KV_NAMESPACE_ID = "e735ab8d926b450b9bba3ef706300c21" },
    { CLOUDFLARE_ZONE_ID = "e76b7d9dca7f8b72c3310805a236271a" },
    { ORCHARD_OWS_URL = "https://qa-ows-grass.theorchard.io" },
    { POSTGRES_HOST = "qa-songwhip-api.cluster-ch72m6xtmre9.us-east-1.rds.amazonaws.com" },
    { POSTGRES_DB = "songwhip_api" },
    { POSTGRES_PORT = "5432" },
    { POSTGRES_SSL = "true" },
    { POSTGRES_USER = "songwhip_api" },
    { SENTRY_DSN = module.songwhip_admin_sentry_project.sentry_key_dsn_public_output },
    { SONGWHIP_LOOKUP_URL = "https://lookup.songwhip.com" },
    { SONGWHIP_RELEASE_TASKS_URL = "https://release-tasks-staging.songwhip.com" },
    { SONGWHIP_QUEUE_URL = "https://queue-staging.songwhip.com" },
  ]

  secrets = [
    { AUTH0_CLIENT_SECRET = "${var.environment}/songwhip-api/AUTH0_CLIENT_SECRET" },
    { AUTH0_M2M_CLIENT_SECRET = "${var.environment}/songwhip-api/AUTH0_M2M_CLIENT_SECRET" },
    { POSTGRES_PASSWORD = "${var.environment}/songwhip-api/POSTGRES_PASSWORD" },
    { CLOUDFLARE_TOKEN = "${var.environment}/songwhip-api/CLOUDFLARE_TOKEN" },
    { CLOUDFLARE_ACCOUNT_TOKEN = "${var.environment}/songwhip-api/CLOUDFLARE_ACCOUNT_TOKEN" },
    { SONGWHIP_RELEASE_TASKS_API_KEY = "${var.environment}/songwhip-api/SONGWHIP_RELEASE_TASKS_API_KEY" },
    { SONGWHIP_LOOKUP_API_KEY = "${var.environment}/songwhip-api/SONGWHIP_LOOKUP_API_KEY" },
    { SONGWHIP_QUEUE_API_KEY = "${var.environment}/songwhip-api/SONGWHIP_QUEUE_API_KEY" }
  ]
}

# Create a CNAME record for "admin.aws-staging.songwhip.com"
# pointing to the Fargate service's load balancer
resource "aws_route53_record" "songwhip_admin_route53_record" {
  provider = aws

  name    = "admin"
  zone_id = "Z04991872X4ZB2H2NYTU9"
  type    = "CNAME"
  ttl     = 60

  records = [module.songwhip_admin_fargate_environment.fargate_load_balancer_dns_name]
}

module "songwhip_admin_dashboard" {
  source                            = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.16.1"
  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  application_family                = var.application_family
  notification_endpoints            = "@slack-songwhip-alerts-dev"
  escalation_notification_endpoints = "@slack-songwhip-devs"
}
