# Connector to sync MG Neo4j to Fingerprint Aura, single topic
module "kafka_mg_to_fp_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=6.5.0"

  providers = {
    aws.dns = aws.networking
  }

  environment                       = var.environment
  application_family                = var.application_family
  additional_tags                   = var.additional_tags
  service_name                      = local.mg_to_fp_service_name
  non_ecr_image                     = "086679231553.dkr.ecr.us-east-1.amazonaws.com/kc-neo-cdc-src:latest"
  aws_region                        = var.aws_region
  service_platform_version          = "1.4.0"
  desired_task_count                = 0
  minimum_capacity                  = 0
  maximum_capacity                  = 0 
  task_cpu                          = 1024
  task_memory                       = 3072
  container_port                    = 8083
  health_check_grace_period_seconds = 300
  container_start_period_seconds    = 300
  blocking_waf_enabled              = true
  health_check_path                 = "/connectors/neo4j_cdc_source"
  web_service_health_check_command  = "[[ $(curl -s http://localhost:8083/connectors/neo4j_cdc_source/status | jq -r '.tasks[].state' | sort | uniq) == 'RUNNING' ]] || exit 1"

  propagate_tags                = true
  vpc_id                        = module.vpc_info.vpc_id
  https_listener_certificate_id = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  fargate_service_subnets       = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets         = module.vpc_info.default_private_subnet_ids

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

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      CONNECT_GROUP_UNIQUE_IDENTIFIER = "${var.environment}-${local.mg_to_fp_service_name}"
    },
    {
      SERVICE_NAME = local.mg_to_fp_service_name
    },
    {
      CONNECT_BOOTSTRAP_SERVERS = data.aws_msk_cluster.kafka_infra.bootstrap_brokers_tls
    },
    {
      NEO4J_SERVER_URI = "neo4j+s://prod-neo4j-cluster.theorchard.io:7687"
    },
    {
      NEO4J_DATABASE_NAME = "graph.db"
    },
    {
      MAX_TASKS = "1"
    },
    {
      NEO4J_ENCRYPTION_ENABLED = "true"
    },
    {
      NEO4J_ENFORCE_SCHEMA = "true"
    },
    {
      NEO4J_BATCH_SIZE = "3000"
    },
    {
      NEO4J_POLL_INTERVAL = "1s"
    },
     {
      NEO4J_KEY_CONVERTER = "org.apache.kafka.connect.storage.StringConverter"
    },
    {
      NEO4J_VALUE_CONVERTER = "io.confluent.connect.avro.AvroConverter"
    },
    {
      NEO4J_KEY_CONVERTER_SCHEMAS_ENABLE = "false"
    },
    {
      NEO4J_VALUE_CONVERTER_SCHEMAS_ENABLE = "true"
    },
    {
      CONNECT_VALUE_CONVERTER_SCHEMA_REGISTRY_URL = var.schema_registry_url
    },        
    {
      AWS_SECRETS_MANAGER = "true"
    },
    {
      DEBUG_MODE = "true"
    },
    {
      NEO4J_IGNORE_STORED_OFFSET = "true"
    },
    {
      NEO4J_START_FROM = "USER_PROVIDED"
    },
    {
      NEO4J_START_FROM_VALUE = "CY7FUptJ9kmmg8enccvIpCEAAAAAKB3VCgAAAAAAAAABAAABn0V4ehg="
    },
    {
      OVERRIDE_CONFIGS = file("mg_fp_sync_config_override.json")
    }
  ]
}

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

# Secrets for Neo4j creds
module "service_credentials_secret_mg_to_fp" {
  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.mg_to_fp_service_name
  secret_name                    = "NEO4J_CREDENTIALS"
  secret_recovery_window_in_days = 7
}

# Datadog Dashboard
module "fargate_service_dashboard_mg_to_fp" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/kafka_connector?ref=6.17.1"

  environment        = var.environment
  application_family = var.application_family
  additional_tags    = var.additional_tags
  service_name       = local.mg_to_fp_service_name
  connector_type     = "default_source"

  environment_type = "fargate"

  notification_endpoints            = "@slack-kafka-data-highway-alerts"
  escalation_notification_endpoints = "@slack-kafka-data-highway-alerts"
  monitor_kafka_topics = [
    "cdc.musicGraph.mgToFp",
  ]

  healthy_tasks_monitor_enabled = false
  service_cpu_monitor_enabled   = false
  error_monitor_enabled         = false
  error_monitor_extra_query     = " -\\\"Given ChangeIdentifier describes a transaction that hasn't yet occurred\\\"" # Expected/non-issue

  healthy_tasks_evaluation_window = "last_15m"
  service_cpu_time_window         = "last_15m"
}