resource "grafana_notification_policy" "default_policy" {
  count = local.default_contact_point_name != null ? 1 : 0

  org_id             = var.org_id
  disable_provenance = var.disable_provenance
  group_by           = var.group_by
  contact_point      = local.default_contact_point_name

  group_wait      = var.group_wait
  group_interval  = var.group_interval
  repeat_interval = var.repeat_interval

  dynamic "policy" {
    for_each = var.notification_policies

    content {
      contact_point   = policy.value.contact_point
      continue        = policy.value.continue
      group_by        = policy.value.group_by
      group_wait      = policy.value.group_wait
      group_interval  = policy.value.group_interval
      repeat_interval = policy.value.repeat_interval

      dynamic "matcher" {
        for_each = policy.value.matchers

        content {
          label = matcher.value.label
          match = matcher.value.match
          value = matcher.value.value
        }
      }
      dynamic "policy" {
        for_each = try(policy.value.policies, [])

        content {
          contact_point   = policy.value.contact_point
          continue        = policy.value.continue
          group_by        = policy.value.group_by
          group_wait      = policy.value.group_wait
          group_interval  = policy.value.group_interval
          repeat_interval = policy.value.repeat_interval

          dynamic "matcher" {
            for_each = policy.value.matchers

            content {
              label = matcher.value.label
              match = matcher.value.match
              value = matcher.value.value
            }
          }
        }
      }
    }
  }

  # lifecycle {
  #   prevent_destroy = false
  #   ignore_changes  = all
  # }
}
