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-vectororder/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
}

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

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

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

  environment_name   = var.environment
  service_name       = var.service_name
  policy_description = "Dynamo-${var.environment}-ows-vectororder-owsrequest"
}

module "ows_vectororder_sentry" {
  source             = "git@github.com:theorchard/terraform-sentry.git//?ref=4.1.2"
  service_name       = var.service_name
  environment        = var.environment
  application_family = var.application_family
  platform           = "python"
}


# Reuse default policy that gives RW access to this OpenSearch domain.
data "aws_iam_policy" "os_default_rw_policy" {
  name = "Elasticsearch-${var.environment}-${var.opensearch_name}-RW-policy"
}

data "aws_iam_policy" "s3_delivery_xml_bucket_default_ro_policy" {
  name = "S3-${var.delivery_xml_bucket_name}-read-only"
}

# Opensearch domain to write to.
data "aws_opensearch_domain" "dd_search_domain" {
  domain_name = "${var.environment}-${var.opensearch_name}"
}

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

  providers = {
    aws.dns = aws.networking
  }

  environment                              = var.environment
  service_name                             = var.service_name
  aws_region                               = var.aws_region
  application_family                       = var.application_family
  commit_sha                               = "latest"
  container_port                           = "8080"
  task_type                                = "web_service"
  desired_task_count                       = 2
  task_cpu                                 = 512
  task_memory                              = 1024
  maximum_capacity                         = 4
  minimum_capacity                         = 2
  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]
  fargate_service_subnets                  = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets                    = module.vpc_info.default_private_subnet_ids

  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/Dynamo-qa-ows-vectororder-owsrequest",
    "arn:aws:iam::437795906767:policy/Dynamo-qa-vector-orders-search-requests-RW",
    "arn:aws:iam::437795906767:policy/Dynamo-qa-vector-orders-RO",
    data.aws_iam_policy.os_default_rw_policy.arn,
    data.aws_iam_policy.s3_delivery_xml_bucket_default_ro_policy.arn
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      SENTRY_DSN = module.ows_vectororder_sentry.sentry_key_dsn_public_output
    },
    {
      AWS_DEFAULT_REGION = var.aws_region
    },
    {
      AR_MYSQL_PORT = var.mysql_port
    },
    {
      AR_MYSQL_USER = var.mysql_user
    },
    {
      AR_MYSQL_HOST = var.ar_mysql_host
    },
    {
      DD_MYSQL_DATABASE = var.dd_mysql_database
    },
    {
      DD_MYSQL_PORT = var.mysql_port
    },
    {
      DD_MYSQL_USER = var.mysql_user
    },
    {
      DD_MYSQL_HOST = var.dd_mysql_host
    },
    {
      DDB_ORDERS_TABLE = var.ddb_orders_table
    },
    {
      DDB_SEARCH_REQUESTS_TABLE = var.ddb_search_requests_table
    },
    {
      OPENSEARCH_HOST = data.aws_opensearch_domain.dd_search_domain.endpoint
    },
    {
      OPENSEARCH_PORT = 443
    },
    {
      OPENSEARCH_DEFAULT_TIMEOUT = 120
    },
    {
      OPENSEARCH_POOL_MAXSIZE = 10
    },
    {
      OPENSEARCH_IGNORE_CACHE = true
    },
    {
      DELIVERY_XML_S3_BUCKET_NAME = var.delivery_xml_bucket_name
    }
  ]
}

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

module "secrets_ows_vectororder" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each = toset(var.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 "vector_orders_search_requests" {
  source = "git@github.com:theorchard/terraform-dynamodb.git//?ref=3.3.1"

  env                   = var.environment
  application_family    = var.application_family
  table_name            = "vector-orders-search-requests"
  hash_key              = "id"
  use_on_demand         = false
  read_capacity         = 5
  write_capacity        = 5
  use_custom_table_name = true
  custom_table_name     = var.ddb_search_requests_table

  attribute = [
    {
      name = "id"
      type = "S"
    }
  ]
}
