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

module "multiple_secrets_with_same_settings" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.3"
  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_label_audit_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.6.0"

  providers = {
    aws.dns = aws.networking
  }

  application_family                       = var.application_family
  environment                              = var.environment
  service_name                             = var.service_name
  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]
  health_check_path                        = "/hello/"

  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/qa-ows-label-audit"
  ]

  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
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      DD_LOGS_INJECTION = "true"
    },
    {
      AUDIT_GENERATION_RUN_INTERVAL = "300"
    },
    {
      AWS_REGION = var.aws_region
    },
    {
      BASE_URL = "https://vectorapi.qaorch.com"
    },
    {
      CACHE_ENABLED = "1"
    },
    {
      CLIENT_ID = "5656789872"
    },
    {
      OAUTHLIB_INSECURE_TRANSPORT = "1"
    },
    {
      REDIRECT_URI = "https://oa.qaorch.com"
    },
    {
      REDIS_HOST = "redis://${module.ows_label_audit_elasticache.redis_primary_endpoint_address}:6379"
    },
    {
      USER_ID = "179"
    },
    {
      USER_TYPE = "oa"
    }
  ]

  secrets = [
    {
      CLIENT_SECRET = "${var.environment}/${var.service_name}/CLIENT_SECRET"
    },
    {
      GOOGLE_SERVICE_EMAIL = "${var.environment}/${var.service_name}/GOOGLE_SERVICE_EMAIL"
    },
    {
      SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN"
    },
    {
      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"
    },
  ]
}

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

# Create IAM user ows-label-audit-user used by celery worker."
resource "aws_iam_user" "ows-label-audit-user" {
  name = "qa-${var.service_name}-user"
  tags = {
    terraformed = true
    role        = "service-user"
  }
}

# Attach iam policies to qa-ows-label-audit-user iam user.
resource "aws_iam_user_policy_attachment" "iam-user-policy-attachment" {
  user       = aws_iam_user.ows-label-audit-user.name
  count      = length(var.iam_policy_arn)
  policy_arn = var.iam_policy_arn[count.index]
}

module "ows_label_audit_elasticache" {
  # checkov:skip=CKV_AWS_30:Rule disabled for QA environment
  # checkov:skip=CKV_AWS_31:Rule disabled for QA environment
  source = "git@github.com:theorchard/terraform-elasticache.git//?ref=3.1.1"

  providers = {
    aws.dns = aws.networking
  }

  environment                      = var.environment
  service_name                     = var.service_name
  application_family               = var.application_family
  cache_engine                     = "redis"
  cache_subnet_group_name          = "${var.environment}-elasticache-subnet-group"
  cache_node_count                 = "1"
  cache_node_type                  = "cache.t4g.micro"
  cache_parameter_group_name       = "default.redis5.0"
  cache_transit_encryption_enabled = false
  vpc_id                           = module.vpc_info.vpc_id
  route53_zone_id                  = data.aws_route53_zone.route53_zone.zone_id
}
