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

module "ows_pricing_owsrequest" {
  source           = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.0.1"
  environment_name = var.environment
  service_name     = var.service_name
}

module "ows_pricing_owsrequest_public" {
  source             = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.0.1"
  policy_description = "Dynamo-${var.environment}-${var.service_name}-owsrequest-public"
  environment_name   = var.environment
  service_name       = "${var.service_name}-public"
}

module "ows_pricing_owsrequest_admin" {
  source             = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.0.1"
  policy_description = "Dynamo-${var.environment}-${var.service_name}-owsrequest-admin"
  environment_name   = var.environment
  service_name       = "${var.service_name}-admin"
}

module "ows_pricing_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                                 = 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]
  http_listener_enabled                    = false
  health_check_path                        = "/hello/"
  load_balancer_idle_timeout               = "120"
  target_group_cookie_duration             = 300
  target_group_stickiness_enabled          = true

  # 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 = [
    module.ows_pricing_owsrequest.policy_arn_output,
    module.ows_pricing_owsrequest_admin.policy_arn_output,
    module.ows_pricing_owsrequest_public.policy_arn_output,
    "arn:aws:iam::437795906767:policy/S3-qa-cucumbers-bulk-physical-pricing-update-ingest-RW",
    "arn:aws:iam::437795906767:policy/STS-qa-ows-pricing-AssumeRole",
    "arn:aws:iam::437795906767:policy/S3-qa-cucumber-PutItem"
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  load_balancer_subnets = module.vpc_info.default_private_subnet_ids

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

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      LOGGER_DSN = "https://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      LOGGER_LEVEL = "30"
    },
    {
      PRICING_BUCKET = "qa-cucumbers"
    },
    {
      PRICING_FOLDER = "bulk-physical-pricing-update-ingest"
    },
    {
      IAM_ROLE = "arn:aws:iam::437795906767:role/qa-ows-pricing-upload-role"
    },
    {
      REDIS_URL = "qa-ows-pricing.oudhyr.0001.use1.cache.amazonaws.com"
    },
    {
      S3_BUCKET = "qa-cucumbers"
    },
    {
      STS_TOKEN_DURATION = "3600"
    },
    {
      AR_DB_HOSTNAME = "qa-art-relations-db.theorchard.io"
    },
    {
      DB_HOSTNAME = module.pricing_rds.aws_route53_record_rds_cluster_cname
    },
  ]

  secrets = concat(
    [{ SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN" }],
    [for secret in toset(var.secrets) : { "${secret}" = "${var.environment}/${var.service_name}/${secret}" }]
  )
}

data "aws_s3_bucket" "s3_bucket" {
  bucket = "${var.environment}-cucumbers"
}

resource "aws_s3_bucket_accelerate_configuration" "bucket_accelerate" {
  bucket = data.aws_s3_bucket.s3_bucket.id
  status = "Enabled"
}

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
  application_family                = var.application_family
  service_4xx_monitor_enabled       = false
  service_5xx_monitor_enabled       = false
  notification_endpoints            = var.application_alert_channel
  escalation_notification_endpoints = var.application_alert_channel
}

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

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