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/daemon-notifications-delivery/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.0.2"

  environment = var.environment
}

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

  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
}

data "aws_iam_policy_document" "notifications_queue_access_policy" {
  statement {
    sid    = "Sid1535396395250"
    effect = "Allow"

    principals {
      type        = "AWS"
      identifiers = ["*"]
    }

    actions   = ["sqs:SendMessage"]
    resources = [module.daemon_notifications_delivery_notifications_queue.queue_arn]

    condition {
      test     = "ArnEquals"
      variable = "aws:SourceArn"
      values   = ["arn:aws:sns:${var.aws_region}:${data.aws_caller_identity.current.account_id}:${var.environment}-notifications-bounces"]
    }
  }

  statement {
    sid    = "Sid1548255409375"
    effect = "Allow"

    principals {
      type        = "AWS"
      identifiers = ["*"]
    }

    actions   = ["sqs:SendMessage"]
    resources = [module.daemon_notifications_delivery_notifications_queue.queue_arn]

    condition {
      test     = "ArnEquals"
      variable = "aws:SourceArn"
      values   = ["arn:aws:sns:${var.aws_region}:${data.aws_caller_identity.current.account_id}:${var.environment}-notifications-complaints"]
    }
  }

  statement {
    sid    = "topic-subscription-arn:aws:sns:${var.aws_region}:${data.aws_caller_identity.current.account_id}:dev_nam_test"
    effect = "Allow"

    principals {
      type        = "AWS"
      identifiers = ["*"]
    }

    actions   = ["sqs:SendMessage"]
    resources = [module.daemon_notifications_delivery_notifications_queue.queue_arn]

    condition {
      test     = "ArnEquals"
      variable = "aws:SourceArn"
      values   = ["arn:aws:sns:${var.aws_region}:${data.aws_caller_identity.current.account_id}:dev_nam_test"]
    }
  }
}

resource "aws_sqs_queue_policy" "notifications_queue_access_policy" {
  queue_url = module.daemon_notifications_delivery_notifications_queue.queue_url
  policy    = data.aws_iam_policy_document.notifications_queue_access_policy.json
}

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

  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.5.1"

  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
  fargate_spot_capacity_provider_weight = 100
  task_placement_failure_alert_enabled  = true

  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/SES-SendOnly",
    "arn:aws:iam::437795906767:policy/SQS-qa-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.qaorch.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
    },
  ]

  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
  notification_endpoints            = "@slack-notifications"
  escalation_notification_endpoints = "@slack-notifications"
}
