module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=1.0.0"
  environment        = var.Environment
  application_family = var.application_family
  service_name       = var.service_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-product-digital/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

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

  environment = var.Environment
}

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

data "aws_route53_zone" "route53_zone" {
  name = "theorchard.io"
}

# access to split api keys
data "aws_iam_policy_document" "secrets_manager_policy" {
  statement {
    effect = "Allow"
    actions = [
      "secretsmanager:GetSecretValue"
    ]

    resources = [
      "arn:aws:secretsmanager:*:*:secret:qa/python-orchard-features/*",
      "arn:aws:secretsmanager:*:*:secret:qa/python-orchard-features/"
    ]
  }
}

resource "aws_iam_policy" "secrets_manager_policy" {
  name   = "${var.Environment}-ows-product-digital-secret-manager-policy"
  policy = data.aws_iam_policy_document.secrets_manager_policy.json

  tags = {
    environment        = var.Environment
    service_name       = var.service_name
    application_family = var.application_family
    terraformed        = true
  }
}

# Setup fargate environment.
module "ows_product_digital_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.5.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                       = 2
  task_cpu                                 = 2048
  task_memory                              = 4096
  maximum_capacity                         = 4
  minimum_capacity                         = 2
  health_check_path                        = "/hello/"
  route53_zone_id                          = data.aws_route53_zone.route53_zone.zone_id
  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]

  # overriding here to add in the graphql_switchboard_vpc (10.110.0.0/16) private subnets
  qa_https_listener_allow_cidr_blocks = [
    "192.168.31.0/24",
    "192.168.32.0/24",
    "192.168.33.0/24",
    "192.168.40.0/22",
    "10.30.0.0/22",
    "10.40.0.0/22",
    "10.110.0.0/24",
    "10.110.1.0/24",
  ]

  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/Dynamo-qa-ows-product-dig-owsrequest",
    "arn:aws:iam::437795906767:policy/SQS-qa-cloudsearch-etl-releases-update-q-RW",
    "arn:aws:iam::437795906767:policy/SQS-qa-upc_repository-minimal-policy",
    aws_iam_policy.secrets_manager_policy.arn
  ]

  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
    },
    {
      KAFKA_BOOTSTRAP_SERVERS = var.kafka_bootstrap_servers
    },
    {
      KAFKA_DIGITAL_PRODUCT_TOPIC = var.kafka_digital_product_topic
    },
    {
      SCHEMA_REGISTRY_URL = var.schema_registry_url
    },
    {
      DIGITAL_PRODUCT_SCHEMA_VERSION = 1
    },
    {
      SPLITIO_USE_PREFORKED_INITIALIZATION = var.splitio_preforked
    },
    {
      NEO4J_MAX_RETRY_TIME = var.NEO4J_MAX_RETRY_TIME
    },
    {
      NEO4J_SETUP_READY = var.NEO4J_SETUP_READY
    },
    {
      NEO4J_URL = var.NEO4J_URL
    },
  ]
}

# Setup datadog dashboards.
module "fargate_service_dashboard" {
  source                            = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.10.7"
  environment                       = var.Environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  service_4xx_monitor_enabled       = false
  service_5xx_monitor_enabled       = false
  application_family                = var.application_family
  notification_endpoints            = ""
  escalation_notification_endpoints = ""
}

module "ows_product_digital_secrets" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.3.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
}
