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.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

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

  default_tags {
    tags = module.default_tags.tags
  }
}

# Terraform backends cannot contain interpolations.
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "qa/ows-contributor/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

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

  environment = var.environment
}

data "aws_acm_certificate" "theorchard_io" {
  domain   = "*.theorchard.io"
  statuses = ["ISSUED"]
}

module "ows_service_owsrequest" {
  source = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.1.0"

  environment_name = var.environment
  service_name     = var.service_name
}

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

  providers = {
    aws.dns = aws.networking
  }

  environment                              = var.environment
  service_name                             = var.service_name
  application_family                       = var.application_family
  aws_region                               = var.aws_region
  commit_sha                               = "latest"
  container_port                           = "8080"
  task_type                                = "web_service"
  desired_task_count                       = 1
  task_cpu                                 = 512
  task_memory                              = 1024
  maximum_capacity                         = 2
  minimum_capacity                         = 1
  health_check_path                        = "/hello/"
  load_balancer_access_logs_s3_bucket_name = "orch-elb-logs"
  vpc_id                                   = module.vpc_info.vpc_id
  https_listener_certificate_id            = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  blocking_waf_enabled                     = false

  iam_managed_policy_attachments = [
    module.ows_service_owsrequest.policy_arn_output,
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets   = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      DD_LOGS_INJECTION = "true"
    },
    {
      DD_TRACE_HEADER_TAGS = "x-trace-id:http.x_trace_id"
    },
    {
      LOGGER_DSN = "https://qa-fluentd-applications.theorchard.io:8888/application"
    },
    {
      SENTRY_DSN = module.ows_contributor_sentry.sentry_key_dsn_public_output
    },
    {
      NEO4J_URL = "neo4j+s://${var.environment}-neo4j-cluster.theorchard.io:7687"
    },
    {
      NEO4J_USERNAME = var.service_name
    },
    {
      NEO4J_DATABASE_NAME = "graph.db"
    },
    {
      AR_DB_HOST = "qa.db.qaorch.com"
    },
    {
      AR_DB_NAME = "art_relations"
    },
    {
      AR_DB_USER = var.service_name
    },
    {
      SPOTIFY_CLIENT_ID = var.spotify_client_id
    },
    {
      APPLE_MUSIC_CLIENT_ID = var.apple_music_client_id
    },
    {
      APPLE_MUSIC_TEAM_ID = var.apple_music_team_id
    },
    {
      REDIS_HOST = module.redis_elasticache.redis_primary_endpoint_address
    },
    {
      REDIS_PORT = "6379"
    }
  ]

  secrets = [
    {
      DD_API_KEY = "${var.environment}/datadog/DD_API_KEY"
    },
    {
      DD_APP_KEY = "${var.environment}/datadog/DD_APP_KEY"
    },
    {
      AR_DB_PASS = "${var.environment}/${var.service_name}/AR_DB_PASS"
    },
    {
      NEO4J_PASSWORD = "${var.environment}/${var.service_name}/NEO4J_PASSWORD"
    },
    {
      SPOTIFY_CLIENT_SECRET = "${var.environment}/${var.service_name}/SPOTIFY_CLIENT_SECRET"
    },
    {
      APPLE_MUSIC_PRIVATE_KEY = "${var.environment}/${var.service_name}/APPLE_MUSIC_PRIVATE_KEY"
    }
  ]
}

module "ows_contributor_secrets" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each = toset(["AR_DB_PASS", "NEO4J_PASSWORD", "SPOTIFY_CLIENT_SECRET", "APPLE_MUSIC_PRIVATE_KEY"])

  application_family = var.application_family
  environment        = var.environment
  service_name       = var.service_name
  secret_name        = each.value
}

module "fargate_service_dashboard" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.18.1"

  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  application_family                = var.application_family
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
  service_cpu_monitor_enabled       = false
}

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

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