provider "aws" {
  region = var.aws_region
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/playwright-monitors/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

resource "datadog_monitor" "Prod_Test_Monitor" {
  for_each = { for monitor in var.monitor_list : monitor.pipelineName => monitor }

  name = "${title(each.value.application)} Prod E2E playwright Test Monitor"
  type = "ci-tests alert"
  tags = [
    "dependent_application:${each.value.tagName}",
    "environment:${var.environment}",
    "application_family:${var.application_family}",
  ]

  query = <<-EOT
    ci-tests("test_level:test @test.status:fail @test.is_retry:true env:${var.environment} @ci.pipeline.name:${each.value.pipelineName} @git.branch:master @test.service:playwright-tests").rollup("cardinality", "@ci.pipeline.id").last("35m") > 1
  EOT

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

  require_full_window = false
  priority            = 2
  include_tags        = false

  message = <<-EOT
    **${title(each.value.application)} Prod E2E Tests**

    **Pipeline:** `${each.value.pipelineName}` (master branch)
    **Environment:** `${var.environment}`
    **Service:** `playwright-tests`

    🔍 **Investigate:**
    - [Failing CI Test Runs](https://sonymusic-pde.datadoghq.com/ci/test-runs?query=%40ci.pipeline.name%3A${each.value.pipelineName}%20%40test.status%3Afail%20env%3A${var.environment})
    - [Playwright Test Logs](https://sonymusic-pde.datadoghq.com/logs?query=service%3Aplaywright-tests%20env%3A${var.environment}%20status%3Aerror)
    - [Monitor](https://sonymusic-pde.datadoghq.com/monitors/{{monitor.id}})

    {{#is_warning}}
    ⚠️ **Warning** — 1 pipeline run has had test failures after retry in the last 35 minutes.
    {{/is_warning}}

    {{#is_alert}}
    🚨 **Alert** — tests have failed after retry in 2 or more pipeline runs in the last 35 minutes.
    {{/is_alert}}

    {{#is_recovery}}
    ✅ **Recovered** — no retry-failed runs in the last 35 minutes.
    {{/is_recovery}}
  EOT
}

locals {
  e2e_monitors = {
    for key, monitor in datadog_monitor.Prod_Test_Monitor : key => monitor.id
  }
}

resource "datadog_monitor" "prod_e2e_master_monitor" {
  name    = "Prod E2E Playwright Test Master Monitor"
  type    = "composite"
  message = <<-EOT
    🚨 One or more Prod E2E Playwright Test Monitors are alerting.

    {{#is_alert}}
    @workflow-Run-Agent-prod-E2E-Test-Monitor-Incident-Manager
    {{/is_alert}}

    {{#is_recovery}}
    ✅ All Prod E2E Playwright Test Monitors have recovered.
    {{/is_recovery}}
  EOT

  query = join(" || ", values(local.e2e_monitors))

  notify_no_data = false
  notify_audit   = false
  require_full_window = false

  monitor_thresholds {
  critical = 1
  }

  tags = [
    "application_family:automation-testing",
    "environment:prod",
    "monitor_type:composite"
  ]
}
