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/daemon-notifications-delivery/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
}

module "daemon_notifications_delivery_notifications_queue" {
  source = "git@github.com:theorchard/terraform-sqs.git//?ref=2.4.1"

  environment                    = var.environment
  application_family             = var.application_family
  custom_queue_name              = "${var.environment}-notifications"
  sqs_delay_seconds              = 0
  sqs_max_message_size           = 262144
  sqs_message_retention_seconds  = 345600
  sqs_visibility_timeout_seconds = 30
  sqs_receive_wait_time_seconds  = 0
}

module "daemon_notifications_delivery_owsrequest" {
  source = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.1.0"

  environment_name = var.environment
  service_name     = var.service_name
}

module "daemon_notifications_delivery_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.6.0"

  providers = {
    aws.dns = aws.networking
  }

  environment              = var.environment
  service_name             = var.service_name
  aws_region               = var.aws_region
  application_family       = var.application_family
  service_platform_version = "1.4.0"
  commit_sha               = "latest"
  container_port           = "8080"
  task_type                = "worker"
  desired_task_count       = 1
  task_cpu                 = 1024
  task_memory              = 2048
  maximum_capacity         = 1
  minimum_capacity         = 1
  vpc_id                   = module.vpc_info.vpc_id
  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/SES-SendOnly",
    "arn:aws:iam::437795906767:policy/SQS-prod-notifications-RW",
    module.daemon_notifications_delivery_owsrequest.policy_arn_output
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      AWS_DEFAULT_REGION = var.aws_region
    },
    {
      DD_LOGS_INJECTION = "true"
    },
    {
      API_EXECUTOR_THREADS = "5"
    },
    {
      BFF_NOTIFICATIONS_URL = "https://workstation.theorchard.com/api/bff-notifications"
    },
    {
      LOGGER_DSN = "https://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      MAX_SQS_MESSAGES = "10"
    },
    {
      QUEUE_READER_THREADS = "5"
    },
    {
      SQS_QUEUE_URL = module.daemon_notifications_delivery_notifications_queue.queue_url
    },
    {
      WORKSTATION_HOST = "workstation.theorchard.com"
    },
  ]

  secrets = [
    {
      SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN"
    },
    {
      SEGMENT_WRITE_KEY = "${var.environment}/${var.service_name}/SEGMENT_WRITE_KEY"
    },
  ]
}

module "daemon_notifications_delivery_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 = false
  service_5xx_monitor_enabled = false

  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.notification_endpoints
}
