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     = "prod/ows-reporting/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_reporting_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-reporting-owsrequest"
}

module "ows_reporting_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.6.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       = "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
  blocking_waf_enabled = false

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

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

  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://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      MYSQL_DATABASE = "${var.mysql_database}"
    },
    {
      MYSQL_HOST = "${var.mysql_host}"
    },
    {
      MYSQL_USER = "${var.mysql_user}"
    },
    {
      SNOWFLAKE_ACCOUNT = "${var.snowflake_account}"
    },
    {
      SNOWFLAKE_DATABASE = "${var.snowflake_database}"
    },
    {
      SNOWFLAKE_ROLE = "${var.snowflake_role}"
    },
    {
      SNOWFLAKE_SCHEMA = "${var.snowflake_schema}"
    },
    {
      SNOWFLAKE_USER = "${var.snowflake_user}"
    },
    {
      SNOWFLAKE_WAREHOUSE = "${var.snowflake_warehouse}"
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    }
  ]
}

# Setup datadog dashboards.
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
  service_4xx_monitor_enabled       = true
  service_4xx_monitor_silenced      = true
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints

  critical_number_5xx          = 20
  critical_recovery_number_5xx = 15
  warning_number_5xx           = 10
  warning_recovery_number_5xx  = 5
}
