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 {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/ows-lyrics/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

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

module "ows_lyrics_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
  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

  vpc_id                        = module.vpc_info.vpc_id
  https_listener_certificate_id = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  custom_waf_arn                = module.ows_lyrics_waf.waf_blocking_arn_output

  # overriding here to add in the graphql_switchboard_vpc (10.120.0.0/16) private subnets
  prod_https_listener_allow_cidr_blocks = [
    "192.168.31.0/24",
    "10.40.0.0/22",
    "10.10.40.0/22",
    "10.120.0.0/24",
    "10.120.1.0/24",
  ]

  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/Dynamo-prod-ows-lyrics-owsrequest"
  ]

  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
    },
    {
      LOGGER_DSN = "https://prod-fluentd-applications.theorchard.io:8888/application"
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      AR_MYSQL_DATABASE = "art_relations"
    },
    {
      AR_MYSQL_HOST = "ar-db.theorchard.com"
    },
    {
      AR_MYSQL_PORT = "3306"
    },
    {
      AR_MYSQL_USER = "ows_lyrics"
    },
    {
      DD_APM_ANALYZED_SPANS = "flask|flask.request=1,requests|requests.request=1"
    }
  ]
  secrets = [
    {
      SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN"
    },
    {
      AR_MYSQL_PASSWORD = "${var.environment}/${var.service_name}/AR_MYSQL_PASSWORD"
    }
  ]
}

module "fargate_service_dashboard" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.13.4"

  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  application_family                = var.application_family
  critical_number_5xx               = var.critical_number_5xx
  critical_recovery_number_5xx      = var.critical_recovery_number_5xx
  warning_number_5xx                = var.warning_number_5xx
  warning_recovery_number_5xx       = var.warning_recovery_number_5xx
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
  additional_tags                   = { service_type : "critical" }
  teams                             = [var.application_family]
  service_4xx_monitor_enabled       = false
  dependent_applications            = var.dependent_applications
}

# Custom WAF that allows larger query strings until we refactor the code
module "ows_lyrics_waf" {
  source            = "git@github.com:theorchard/terraform-aws-waf.git//?ref=2.0.1"
  environment       = var.environment
  service_name      = var.service_name
  aws_region        = var.aws_region
  count_waf_enabled = false
  block_waf_enabled = true
  excluded_rules = [
    "NoUserAgent_HEADER",
    "SizeRestrictions_BODY",
    "SizeRestrictions_QUERYSTRING",
    "CrossSiteScripting_BODY",
  ]
}
