module "fp_schema_secrets" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each = toset(var.snowflake_creds_secret_names)

  environment        = var.environment
  application_family = var.application_family
  additional_tags    = var.additional_tags
  service_name       = local.schema_based_service_name
  secret_name        = each.value
}

module "snowflake_sink_fp_schema_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.schema_based_service_name
  non_ecr_image                         = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/${var.ecr_name}:latest"
  aws_region                            = var.aws_region
  desired_task_count                    = 0
  minimum_capacity                      = 0
  maximum_capacity                      = 0
  task_cpu                              = 1024
  task_memory                           = 3072
  container_port                        = 8083
  health_check_grace_period_seconds     = 180
  container_start_period_seconds        = 180
  blocking_waf_enabled                  = true
  health_check_path                     = "/connectors/${local.schema_based_service_name}"
  web_service_health_check_command      = "[[ $(curl -s http://localhost:8083/connectors/${local.schema_based_service_name}/status | jq -r '.tasks[].state,.connector.state' | sort | uniq) == 'RUNNING' ]] || exit 1"
  vpc_id                                = module.vpc_info.vpc_id
  https_listener_certificate_id         = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  propagate_tags                        = true
  fargate_spot_capacity_provider_weight = 100
  task_placement_failure_alert_enabled  = false

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets   = module.vpc_info.default_private_subnet_ids
  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"
    },
    {
      SERVICE_NAME = local.schema_based_service_name
    },
    {
      DD_ENV = var.environment
    },
    {
      CONNECTOR_TYPE = "snowflake_sink"
    },
  ]
  environment_variables = [
    {
      Environment = var.environment
    },
    {
      CONNECTOR_NAME = local.schema_based_service_name
    },
    {
      CONNECT_GROUP_UNIQUE_IDENTIFIER = "${var.environment}-${local.schema_based_service_name}"
    },
    {
      CONSUMER_PARTITION_ASSIGNMENT_STRATEGY = "org.apache.kafka.clients.consumer.RoundRobinAssignor"
    },
    {
      CONNECT_VALUE_CONVERTER = "io.confluent.connect.json.JsonSchemaConverter"
    },
    {
      CONNECT_VALUE_CONVERTER_SCHEMA_REGISTRY_URL = var.schema_registry_url
    },
    {
      MAX_TASKS = 3
    },
    {
      BUFFER_COUNT_RECORDS = 10000
    },
    {
      BUFFER_SIZE_BYTES = 5000000
    },
    {
      BUFFER_FLUSH_TIME = 240
    },
    {
      CONNECT_BOOTSTRAP_SERVERS = data.aws_msk_cluster.kafka_infra.bootstrap_brokers_tls
    },
    {
      SNOWFLAKE_HOST = "delphi.us-east-1.snowflakecomputing.com:443"
    },
    {
      SNOWFLAKE_USER = "QA_KAFKA_CONNECT_SNOWFLAKE_SINK_MG_NO_GSR"
    },
    {
      SNOWFLAKE_DATABASE = "FACTS"
    },
    {
      SNOWFLAKE_SCHEMA = "QA"
    },
    {
      SERVICE_NAME = local.schema_based_service_name
    },
    {
      KAFKA_TOPICS = join(",", [
        "cdc.musicGraphV5.acrid",
        "cdc.musicGraphV5.fingerprintedAs",
        "cdc.musicGraphV5.orchardAsset",
        "cdc.musicGraphV5.containsFingerprint",
        "cdc.musicGraphV5.deletedContainsFingerprint",
        "cdc.musicGraphV5.orchardSoundRecording",

      ])
    },
    {
      SNOWFLAKE_TOPIC_TABLE_MAP = join(",", [
        "cdc.musicGraphV5.acrid:CDC__MUSIC_GRAPH_V5__ACRID",
        "cdc.musicGraphV5.fingerprintedAs:CDC__MUSIC_GRAPH_V5__FINGERPRINTED_AS",
        "cdc.musicGraphV5.orchardAsset:CDC__MUSIC_GRAPH_V5__ORCHARD_ASSET",
        "cdc.musicGraphV5.containsFingerprint:CDC__MUSIC_GRAPH_V5__CONTAINS_FINGERPRINT",
        "cdc.musicGraphV5.deletedContainsFingerprint:CDC__MUSIC_GRAPH_V5__DELETED_CONTAINS_FINGERPRINT",
        "cdc.musicGraphV5.orchardSoundRecording:CDC__MUSIC_GRAPH_V5__ORCHARD_SOUND_RECORDING",

      ])
    },
    {
      DLQ_TOPIC_NAME = "dlq.sfsink.fp.schema"
    },
  ]
}

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


module "fargate_service_dashboard_fp_schema" {
  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
  environment_type   = "fargate"
  service_name       = local.schema_based_service_name
  connector_type     = "default_sink"

  notification_endpoints            = "@slack-kafka-data-highway-alerts"
  escalation_notification_endpoints = "@slack-data-alerts-qa"
  monitor_kafka_topics = [
    "cdc.musicGraphV5.acrid",
    "cdc.musicGraphV5.fingerprintedAs",
    "cdc.musicGraphV5.orchardAsset",
    "cdc.musicGraphV5.containsFingerprint",
    "cdc.musicGraphV5.deletedContainsFingerprint",
    "cdc.musicGraphV5.orchardSoundRecording",

  ]
  dlq_topic_name = "dlq.sfsink.fp.schema"

  healthy_tasks_monitor_enabled   = false
  service_cpu_monitor_enabled     = false
  error_monitor_enabled           = false
  healthy_tasks_evaluation_window = "last_1h"
  service_cpu_time_window         = "last_1h"
  # Alert on the number of times the metric shows unhealthy tasks within the last hour
  healthy_tasks_critical_number = 5
}
