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-carveouts/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"
}

data "aws_iam_user" "user" {
  user_name = "${var.environment}-${var.service_name}"
}

data "aws_iam_policy" "ows_machine_to_machine_policy" {
  name = "SecretsManager-${var.environment}-ows-machine-to-machine-policy"
}

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 "ows_carveouts_owsrequest" {
  source           = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.0.1"
  environment_name = var.environment
  service_name     = var.service_name
}

module "ows_carveouts_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
  aws_region                               = var.aws_region
  application_family                       = var.application_family
  commit_sha                               = "latest"
  container_port                           = "8081"
  task_type                                = "web_service"
  desired_task_count                       = 2
  task_cpu                                 = 1024
  task_memory                              = 2048
  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]
  health_check_path                        = "/hello/"
  blocking_waf_enabled                     = false

  # 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",
  ]

  https_listener_allow_prefix_list_names = [
    "${var.environment}-supply-chain-private-subnet-prefix-list",
  ]

  iam_managed_policy_attachments = [
    module.ows_carveouts_owsrequest.policy_arn_output,
    "arn:aws:iam::437795906767:policy/Dynamo-qa-carveout_change_log-RW"
  ]

  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
    },
    {
      DD_SERVICE_NAME = "${var.environment}-${var.service_name}"
    },
    {
      DD_TRACE_APP_NAME = "${var.environment}-${var.service_name}"
    },
    {
      DD_TRACE_GLOBAL_TAGS = "env:${var.environment}-${var.service_name}"
    },
    {
      DD_TRACE_ANALYTICS_ENABLED = "true"
    },
    {
      APPLICATION_ENV = var.environment
    },
    {
      AR_DBNAME = "art_relations"
    },
    {
      AR_HOST = "qa.db.qaorch.com"
    },
    {
      AR_PORT = "3306"
    },
    {
      AR_USER = "ows-carveouts"
    },
    {
      AWS_DYNAMODB_CARVEOUT = "qa-carveout_change_log"
    },
    {
      AWS_REGION = "us-east-1"
    },
    {
      DD_DBNAME = "direct_delivery"
    },
    {
      DD_HOST = "qadddb.qaorch.com"
    },
    {
      DD_PORT = "3306"
    },
    {
      DD_USER = "qametadata-ro"
    },
    {
      OWS_PRODUCT_URL = "https://qa-ows-product.theorchard.io"
    },
    {
      UDP_DSN = "udp://qa-fluentd-applications-udp.theorchard.io:5160"
    },
    {
      LOG_LEVEL = var.log_level
    }
  ]

  secrets = [
    {
      AR_PASS = "${var.environment}/${var.service_name}/AR_PASS"
    },
    {
      AWS_KEY = "${var.environment}/${var.service_name}/AWS_KEY"
    },
    {
      AWS_SECRET = "${var.environment}/${var.service_name}/AWS_SECRET"
    },
    {
      AWS_ACCESS_KEY_ID = "${var.environment}/${var.service_name}/AWS_ACCESS_KEY_ID"
    },
    {
      AWS_SECRET_ACCESS_KEY = "${var.environment}/${var.service_name}/AWS_SECRET_ACCESS_KEY"
    },
    {
      DD_PASS = "${var.environment}/${var.service_name}/DD_PASS"
    },
    {
      SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN"
    },
  ]
}

resource "aws_iam_user_policy_attachment" "user_policy_attachment" {
  user       = data.aws_iam_user.user.user_name
  policy_arn = data.aws_iam_policy.ows_machine_to_machine_policy.arn
}

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            = var.notification_endpoints
  escalation_notification_endpoints = var.notification_endpoints
}

module "ows_carveouts_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           = "php"
  teams              = [var.environment]
}
