resource "grafana_contact_point" "contact_points" {
  for_each = { for cp in var.contact_points : "${cp.name}_${cp.type}" => cp }

  org_id             = var.org_id
  name               = each.value.name
  disable_provenance = var.disable_provenance

  dynamic "telegram" {
    for_each = each.value.type == "telegram" ? [1] : []
    content {
      chat_id    = each.value.settings["chat_id"]
      token      = each.value.settings["bot_token"]
      message    = local.contact_point_templates[each.value.name].template
      parse_mode = "HTML"
    }
  }

  dynamic "slack" {
    for_each = each.value.type == "slack" ? [1] : []
    content {
      url   = each.value.settings["webhook_url"]
      title = try(local.contact_point_templates[each.value.name].title, null)
      text  = local.contact_point_templates[each.value.name].template
    }
  }
}
