terraform {
  backend "s3" {
    bucket  = "qa-permissions-platform-terraform-state"
    key     = "qa/kafka-pp/connectors/source/neo4j/sme-label/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

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       = local.sme_label_changes_service_name
  team_name          = var.team_name
}

provider "aws" {
  region = "us-east-1"

  default_tags {
    tags = module.default_tags.tags
  }
}

provider "aws" {
  region  = "us-east-1"
  alias   = "networking"
  profile = "networking"

  default_tags {
    tags = module.default_tags.tags
  }
}

data "aws_caller_identity" "current" {}

module "vpc_info" {
  source      = "git@github.com:theorchard/terraform-vpc-info.git?ref=3.1.0"
  environment = var.environment
}

module "service_info" {
  for_each     = toset(local.downstream_services)
  source       = "git@github.com:theorchard/terraform-service-info.git//?ref=1.0.0"
  service_name = each.value
  environment  = var.environment
}

data "aws_msk_cluster" "kafka_pp" {
  cluster_name = "${var.environment}-kafka-pp"
}

module "service_credentials_secret" {
  source = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"

  environment                    = var.environment
  application_family             = var.application_family
  additional_tags                = var.additional_tags
  service_name                   = local.sme_label_changes_service_name
  secret_name                    = "NEO4J_CREDENTIALS"
  secret_recovery_window_in_days = 7
}

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

  providers = {
    aws.dns = aws.networking
  }

  environment                       = var.environment
  application_family                = var.application_family
  additional_tags                   = var.additional_tags
  service_name                      = local.sme_label_changes_service_name
  non_ecr_image                     = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/${local.ecr_repo_name}:latest"
  aws_region                        = var.aws_region
  service_platform_version          = "1.4.0"
  desired_task_count                = 1
  minimum_capacity                  = 1
  maximum_capacity                  = 1
  health_check_grace_period_seconds = 180
  container_start_period_seconds    = 180
  task_cpu                          = 1024
  task_memory                       = 3072
  container_port                    = 8083
  blocking_waf_enabled              = true
  health_check_path                 = "/connectors/neo4j_source"
  web_service_health_check_command  = "[ \"$(curl -s -f http://localhost:8083/connectors/neo4j_source/tasks/0/status | jq -r .state)\" == 'RUNNING' ] || exit 1"

  vpc_id                  = module.vpc_info.vpc_id
  fargate_service_subnets = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets   = module.vpc_info.default_private_subnet_ids

  https_listener_allow_security_group_ids = flatten([
    [for service in module.service_info : service.security_groups],
  ])

  datadog_custom_image = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/orchard-datadog-agent-jmx:latest"
  # datadog agent crashes due to OutOfMemoryError with JMX. So increase it from 256
  datadog_task_memory = 512
  datadog_agent_environment_variables = [
    {
      JMX_PORT = "9095"
    },
    {
      DD_ENV = var.environment
    },
    {
      SERVICE_NAME = local.sme_label_changes_service_name
    },
    {
      CONNECTOR_TYPE = "default_source"
    },
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      AWS_SECRETS_MANAGER = "true"
    },
    {
      CONNECT_GROUP_UNIQUE_IDENTIFIER = "neo4j_source_sme_label_changes"
    },
    {
      CONNECT_BOOTSTRAP_SERVERS = data.aws_msk_cluster.kafka_pp.bootstrap_brokers_tls
    },
    {
      NEO4J_SERVER_URI = "neo4j+s://qa-neo4j-cluster.theorchard.io:7687"
    },
    {
      SERVICE_NAME = local.sme_label_changes_service_name
    },
    {
      NEO4J_KEY_CONVERTER = "org.apache.kafka.connect.storage.StringConverter"
    },
    {
      NEO4J_VALUE_CONVERTER = "org.apache.kafka.connect.json.JsonConverter"
    },
    {
      NEO4J_KEY_CONVERTER_SCHEMAS_ENABLE = "false"
    },
    {
      NEO4J_VALUE_CONVERTER_SCHEMAS_ENABLE = "false"
    },
    {
      KAFKA_TOPIC = "cdc.musicGraph.smeLabel"
    },
    {
      NEO4J_QUERY = replace(file("sme_label_changes.cypher"), "\n", " ")
    },
    {
      NEO4J_DATABASE_NAME = "graph.db"
    },
    {
      NEO4J_POLL_INTERVAL = "5s"
    },
    {
      NEO4J_BATCH_SIZE = 150000
    },
    {
      EXTRACT_VALUE_TO_KEY = "uuid"
    },
    {
      TRACKING_PROPERTY_NAME = "lastModifiedAt"
    },
    {
      NEO4J_IGNORE_STORED_OFFSET = "true"
    }
  ]
}

# This rule will allow kafka-connect tasks to communicate with each other in distributed mode
resource "aws_security_group_rule" "allow_intra_cluster_communication" {
  from_port         = 8083
  to_port           = 8083
  protocol          = "tcp"
  security_group_id = module.neo4j_source_connector_sme_label_fargate_environment.fargate_security_group_id
  self              = true
  type              = "ingress"
}

data "aws_ec2_managed_prefix_list" "orchard_prod_private" {
  name = "prod-orcd-private-subnet-prefix-list"
}

# This rule will allow AKHQ to communicate with this kafka-connect service (LB)
resource "aws_security_group_rule" "allow_orchard_prod_private" {
  type              = "ingress"
  from_port         = 443
  to_port           = 443
  protocol          = "TCP"
  security_group_id = module.neo4j_source_connector_sme_label_fargate_environment.fargate_load_balancer_security_group_id
  prefix_list_ids = [
    data.aws_ec2_managed_prefix_list.orchard_prod_private.id
  ]
  depends_on = [module.neo4j_source_connector_sme_label_fargate_environment]
}


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

  environment        = var.environment
  environment_type   = "fargate"
  service_name       = local.sme_label_changes_service_name
  application_family = var.application_family
  teams              = [var.application_family]
  connector_type     = "default_source"

  notification_endpoints            = "@slack-accounts-team-alerts"
  escalation_notification_endpoints = "@slack-accounts-team-alerts @slack-kafka-data-highway-alerts"
  monitor_kafka_topics              = ["cdc.musicGraph.smeLabel"]
  monitor_kafka_topics_cluster_name = "qa-kafka-pp"

  healthy_tasks_monitor_enabled        = true
  service_cpu_monitor_enabled          = true
  error_monitor_enabled                = true
  error_monitor_critical_number_errors = 5
  healthy_tasks_evaluation_window      = "last_15m"
  service_cpu_time_window              = "last_15m"
}



data "datadog_monitor" "kafka_connect_error_monitor" {
  name_filter = "${local.sme_label_changes_service_name} error monitor"
  monitor_tags_filter = [
    "application_family:${var.application_family}",
    "environment:${var.environment}",
  ]
}

# Downtime Monitor Schedule for QA Neo4j refresh
resource "datadog_downtime_schedule" "error_monitor_refresh_downtime" {
  scope = "*"
  monitor_identifier {
    monitor_id = data.datadog_monitor.kafka_connect_error_monitor.id
  }
  recurring_schedule {
    recurrence {
      duration = "1h"
      rrule    = "FREQ=DAILY;INTERVAL=1"
      start    = "2024-08-19T18:00:00"
    }
    timezone = "America/New_York"
  }
  display_timezone                 = "America/New_York"
  message                          = "Neo4j Refresh is running on https://pipeline.theorchard.io/job/neo4j-refresh-pipeline/"
  mute_first_recovery_notification = true
}

