terraform {
  backend "s3" {
    bucket  = "qa-fansifter-terraform-state"
    key     = "qa/kafka-cluster/connectors/snowflake-sink/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       = var.service_name
  team_name          = var.application_family
}

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
  }
}

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

  environment = var.environment
}

data "aws_route53_zone" "route53_zone" {
  name = var.domain_name
}

data "aws_msk_cluster" "kafka_fansifter" {
  cluster_name = "${var.environment}-${var.msk_cluster_name}"
}

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

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

  providers = {
    aws.dns = aws.networking
  }

  environment                       = var.environment
  service_name                      = var.service_name
  application_family                = var.application_family
  additional_tags                   = var.additional_tags
  aws_region                        = var.aws_region
  non_ecr_image                     = "086679231553.dkr.ecr.us-east-1.amazonaws.com/kafka-connect-sfsink:latest"
  service_platform_version          = "1.4.0"
  desired_task_count                = 1
  minimum_capacity                  = 1
  maximum_capacity                  = 2
  task_cpu                          = 1024
  task_memory                       = 3072
  container_port                    = 8083
  health_check_grace_period_seconds = 180
  blocking_waf_enabled              = true
  health_check_path                 = "/connectors/${var.service_name}"
  web_service_health_check_command  = "[[ $(curl -s http://localhost:8083/connectors/${var.service_name}/status | jq -r '.tasks[].state' | sort | uniq) == '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
  route53_zone_id                   = data.aws_route53_zone.route53_zone.zone_id
  propagate_tags                    = true
  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 = var.service_name
    },
    {
      CONNECTOR_TYPE = "default_sink"
    },
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      CONNECT_GROUP_UNIQUE_IDENTIFIER = "${var.environment}-${var.service_name}"
    },
    {
      CONNECTOR_NAME = var.service_name
    },
    {
      MAX_TASKS = 1
    },
    {
      BUFFER_COUNT_RECORDS = 20000
    },
    {
      BUFFER_SIZE_BYTES = 5000000
    },
    {
      CONNECT_BOOTSTRAP_SERVERS = data.aws_msk_cluster.kafka_fansifter.bootstrap_brokers_tls
    },
    {
      SNOWFLAKE_HOST = "delphi.us-east-1.snowflakecomputing.com:443"
    },
    {
      SNOWFLAKE_USER = "${upper(var.environment)}_KAFKA_CONNECT_FS"
    },
    {
      SNOWFLAKE_DATABASE = "FANSIFTER_APP_REPORTING"
    },
    {
      SNOWFLAKE_SCHEMA = upper(var.environment)
    },
    {
      DEBUG_MODE = "false"
    },
    {
      SERVICE_NAME = var.service_name
    },
    {
      KAFKA_TOPICS = join(",", [
        "event.songwhip.presave.processed",
        "event.songwhip.fan.consent",
        "event.songwhip.fan.consent.doubleOptIn",
        "event.songwhip.page.unlock",
        "event.songwhip.guardian.review",
        "event.sendgrid.webhook.general",
        "event.sendgrid.webhook.inbound",
        "event.sendgrid.webhook.fanResponse",
        "event.owsPreferenceCenter.fanUpdate",
        "event.owsPreferenceCenter.fanDelete",
        "event.owsPreferenceCenter.subscriptionsUpdate",
        "event.owsPreferenceCenter.fanSubscriptionsUpdate",
        "event.owsUrlShortener.click",
        "event.twilio.webhook.outbound",
        "event.twilio.webhook.inbound",
        "event.twilio.message.send",
        "event.twilio.fan.subscriptionStatusUpdate",
        "event.owsPreferenceCenter.doubleOptInConfirmation",
      ])
    },
    {
      SNOWFLAKE_TOPIC_TABLE_MAP = join(",", [
        "event.songwhip.presave.processed:EVENT_SONGWHIP_PRESAVE_PROCESSED",
        "event.songwhip.fan.consent:EVENT_SONGWHIP_FAN_CONSENT",
        "event.songwhip.fan.consent.doubleOptIn:EVENT_SONGWHIP_FAN_CONSENT_DOUBLE_OPT_IN",
        "event.songwhip.page.unlock:EVENT_SONGWHIP_PAGE_UNLOCK",
        "event.songwhip.guardian.review:EVENT_SONGWHIP_GUARDIAN_REVIEW",
        "event.sendgrid.webhook.general:EVENT_SENDGRID_WEBHOOK_GENERAL",
        "event.sendgrid.webhook.inbound:EVENT_SENDGRID_WEBHOOK_INBOUND",
        "event.sendgrid.webhook.fanResponse:EVENT_SENDGRID_WEBHOOK_FAN_RESPONSE",
        "event.owsPreferenceCenter.fanUpdate:EVENT_PREFERENCE_CENTER_FAN_UPDATE",
        "event.owsPreferenceCenter.fanDelete:EVENT_PREFERENCE_CENTER_FAN_DELETE",
        "event.owsPreferenceCenter.subscriptionsUpdate:EVENT_PREFERENCE_CENTER_SUBSCRIPTIONS_UPDATE",
        "event.owsPreferenceCenter.fanSubscriptionsUpdate:EVENT_PREFERENCE_CENTER_FAN_SUBSCRIPTIONS_UPDATE",
        "event.owsUrlShortener.click:EVENT_URL_SHORTENER_CLICK",
        "event.twilio.webhook.outbound:EVENT_TWILIO_WEBHOOK_OUTBOUND",
        "event.twilio.webhook.inbound:EVENT_TWILIO_WEBHOOK_INBOUND",
        "event.twilio.message.send:EVENT_TWILIO_MESSAGE_SEND",
        "event.twilio.fan.subscriptionStatusUpdate:EVENT_TWILIO_FAN_SUBSCRIPTION_STATUS_UPDATE",
        "event.owsPreferenceCenter.doubleOptInConfirmation:EVENT_PREFERENCE_CENTER_DOUBLE_OPT_IN_CONFIRMATION",
      ])
    }
  ]
}

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

  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
  secret_name        = each.value

  additional_tags = var.additional_tags
}

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

# 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.snowflake_sink_fargate.fargate_load_balancer_security_group_id
  prefix_list_ids = [
    data.aws_ec2_managed_prefix_list.orchard_prod_private.id
  ]
  depends_on = [module.snowflake_sink_fargate]
}

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

  environment        = var.environment
  environment_type   = "fargate"
  service_name       = var.service_name
  application_family = var.application_family
  teams              = [var.application_family]
  connector_type     = "default_sink"

  notification_endpoints            = "@slack-fansifter-alerts"
  escalation_notification_endpoints = "@slack-fansifter"
  monitor_kafka_topics = [
    "event.songwhip.presave.processed",
    "event.songwhip.fan.consent",
    "event.songwhip.fan.consent.doubleOptIn",
  ]

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