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-permissions/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_permissions_owsrequest" {
  source             = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.1.0"
  policy_description = "A policy to access dynamo owsrequest table"
  environment_name   = var.environment
  service_name       = var.service_name
}

module "ows_permissions_cache" {
  source = "git@github.com:theorchard/terraform-elasticache.git//?ref=4.0.0"

  providers = {
    aws.dns = aws.networking
  }

  environment                    = var.environment
  service_name                   = var.service_name
  application_family             = var.application_family
  cache_engine                   = "redis"
  redis_engine_version           = "7.1"
  cache_subnet_group_name        = "${var.environment}-elasticache-subnet-group"
  cache_node_type                = "cache.t4g.micro"
  cache_parameter_group_name     = "default.redis7"
  vpc_id                         = module.vpc_info.vpc_id
  additional_cidr_blocks_enabled = "true"
  additional_cidr_blocks = [
    "10.40.0.0/22",
    "10.141.0.0/29",
    "192.168.31.0/24"
  ]
}

module "datadog_elasticache" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/elasticache?ref=6.16.1"

  environment        = var.environment
  service_name       = var.service_name
  application_family = var.application_family
  teams              = var.teams

  notification_endpoints            = var.application_alert_channel
  escalation_notification_endpoints = var.application_alert_channel
}

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

  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                       = 3
  task_cpu                                 = 1024
  task_memory                              = 2048
  maximum_capacity                         = 8
  minimum_capacity                         = 3
  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]
  iam_policy_file_enabled                  = true

  iam_managed_policy_attachments = [
    module.ows_permissions_owsrequest.policy_arn_output,
  ]

  https_listener_allow_prefix_list_names = [
    "uat-accounting-private-subnet-prefix-list",
    "qa-accounting-private-subnet-prefix-list",
  ]

  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
    },
    {
      SENTRY_DSN = module.sentry_ows_permissions_qa.sentry_key_dsn_public_output
    },
    {
      NEO4J_MAX_RETRY_TIME = var.NEO4J_MAX_RETRY_TIME
    },
    {
      NEO4J_SETUP_READY = var.NEO4J_SETUP_READY
    },
    {
      AUTH0_DOMAIN = "qa-orchard.auth0.com"
    },
    {
      AUTH0_CONNECTION = "art-relations"
    },
    {
      REDIS_URL = module.ows_permissions_cache.redis_primary_endpoint_address
    },
    {
      REDIS_CACHE_TTL = var.REDIS_CACHE_TTL
    },
    {
      SPLITIO_USE_PREFORKED_INITIALIZATION = var.splitio_preforked
    },
    {
      DD_VERSION = var.service_version
    },
    {
      DD_LOGS_INJECTION = "true"
    },
    {
      DD_PROFILING_ENABLED = "true"
    },
    {
      DD_TRACE_ENABLED = true
    },
    {
      DD_TRACE_WRITER_BUFFER_SIZE_BYTES = "9388608"
    },
    {
      DD_TRACE_WRITER_MAX_PAYLOAD_SIZE_BYTES = "9388608"
    },
  ]

  secrets = [
    {
      SEGMENT_WRITE_KEY = "${var.environment}/${var.service_name}/SEGMENT_WRITE_KEY"
    }
  ]
}

module "secrets" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each = toset(var.secrets_manager_secret_names)

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

module "fargate_service_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            = var.application_alert_channel
  escalation_notification_endpoints = var.application_alert_channel
}

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

  environment        = var.environment
  service_name       = var.service_name
  application_family = var.application_family
}

# Get the ec2 prefix list for permissions-platform-qa AWS account
data "aws_ec2_managed_prefix_list" "permissions_platform_qa" {
  name = "${var.environment}-permissions-platform-private-subnet-prefix-list"
}

# Allow qa-ows-pdp (which is in permissions-platform-qa AWS account)
# interact with qa-ows-permissions
resource "aws_security_group_rule" "allow_permissions_platform_qa" {
  type              = "ingress"
  from_port         = 443
  to_port           = 443
  protocol          = "TCP"
  security_group_id = module.ows_permissions_fargate_environment.fargate_load_balancer_security_group_id
  prefix_list_ids = [
    data.aws_ec2_managed_prefix_list.permissions_platform_qa.id
  ]
  depends_on = [module.ows_permissions_fargate_environment]
}
