data "fivetran_group" "prod_warehouse" {
  id = "gentry_invitro"
}

resource "fivetran_connector" "main" {
  group_id = data.fivetran_group.prod_warehouse.id
  service  = "aurora"

  destination_schema {
    prefix = "art_relations_prod"
  }

  config {
    host        = module.art_relations_rds.rds_cluster_endpoint
    port        = module.art_relations_rds.rds_cluster_port
    user        = "fivetran"
    tunnel_host = "fivetran.theorchard.io"
    tunnel_port = "2222"
    tunnel_user = "fivetran"
  }
}

resource "fivetran_connector_schedule" "main" {
  connector_id      = fivetran_connector.main.id
  sync_frequency    = 5
  pause_after_trial = true
}

resource "fivetran_connector_schema_config" "main" {
  connector_id           = fivetran_connector.main.id
  schema_change_handling = "ALLOW_COLUMNS"
  schemas = {
    art_relations = {
      enabled = true
      tables = { for table in var.art_relations_tables_to_sync_to_snowflake : table => {
        enabled = true
      } }
    }
    art_relations_log = {
      enabled = true
      tables = { for table in var.art_relations_log_tables_to_sync_to_snowflake : table => {
        enabled = true
      } }
    }
    maxwell_neo4j_kinesis = {
      enabled = true
      tables = { for table in var.maxwell_neo4j_kinesis_tables_to_sync_to_snowflake : table => {
        enabled = true
      } }
    }
    maxwell_switchboard = {
      enabled = true
      tables = { for table in var.maxwell_switchboard_tables_to_sync_to_snowflake : table => {
        enabled = true
      } }
    }
    physical_delivery = {
      enabled = true
      tables = { for table in var.physical_delivery_tables_to_sync_to_snowflake : table => {
        enabled = true
      } }
    }
    publishing = {
      enabled = true
      tables = { for table in var.publishing_tables_to_sync_to_snowflake : table => {
        enabled = true
      } }
    }
  }
}

resource "datadog_monitor" "fivetran_art_relations_prod_replication_errors" {
  name    = "Fivetran replication for art_relations_prod_art_relations is reporting non-OK events"
  type    = "log alert"
  message = <<EOT
The Fivetran connector: {{log.attributes.connector_name}} is reporting errors or warnings, which started at: {{first_triggered_at}}.There have been {{value}} of these events in the last 5 minutes.
Check for logs about the tables: {{log.attributes.data.table_name}} or {{log.attributes.data.name}}.
Notify: @slack-The_Orchard-data-team @slack-The_Orchard-data-alarm
EOT
  escalation_message = "Fivetran connector {{log.attributes.connector_name}} continues to generate non-info logs. Investigation is required as critical integrations are at risk. Notify: @slack-data-alerts @slack-data-products-cross-squad"

  query = "logs(\"source:fivetran @hostname:ORCHARD_APP_REPORTING_V2_DESTINATION @connector_name:art_relations_prod @data.schema:art_relations_prod_art_relations -status:info\").index(\"*\").rollup(\"count\").last(\"5m\") > 2"

  monitor_thresholds {
    critical = 2
    warning = 1
    warning_recovery = 0
    critical_recovery = 1
  }

  enable_logs_sample  = true
  include_tags        = true
  notify_no_data      = false
  notify_audit        = false
  renotify_interval   = 15
  renotify_statuses = ["alert"]
  timeout_h           = 1
  require_full_window = false

  priority = 2

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}",
    "application_family:data-platform",
  ]
}
