///|
/// Check presence without putting the variable's value in a report.
pub fn environment_rule(id : String, key : String, expected? : String) -> Rule {
  rule(id, "environment", ctx => {
    match ctx.facts.get(key) {
      None =>
        finding(
          Failed,
          "env_missing",
          "Required environment variable is absent",
          remediation="Set the required variable in the calling environment",
        )
      Some(value) =>
        if value.trim().is_empty() {
          finding(
            Failed,
            "env_empty",
            "Required environment variable is empty",
            remediation="Provide a nonempty value",
          )
        } else if expected is Some(wanted) && value != wanted {
          finding(
            Failed,
            "env_invalid",
            "Environment variable does not match the required value",
            remediation="Check the variable against the project configuration",
          )
        } else {
          finding(
            Passed,
            "env_present",
            "Required environment variable is valid",
          )
        }
    }
  })
}

///|
/// PATH entries are compared as complete entries; substring matches are rejected.
pub fn inspect_path(
  value : String,
  required : String,
  windows~ : Bool = false,
) -> Finding {
  let separator = if windows { ";" } else { ":" }
  let entries = value.split(separator).map(v => v.to_owned()).collect()
  if entries.iter().any(v => v.trim().is_empty()) {
    return finding(
      Warning,
      "path_empty_entry",
      "PATH has an empty entry that can search the current directory",
      remediation="Remove empty PATH entries",
    )
  }
  let normalized = entries.map(v => if windows { v.to_lower() } else { v })
  let wanted = if windows { required.to_lower() } else { required }
  if normalized.iter().any(v => v == wanted) {
    let seen : Map[String, Bool] = Map([])
    for entry in normalized {
      if seen.contains(entry) {
        return finding(
          Warning,
          "path_duplicate",
          "PATH contains duplicate entries",
          remediation="Remove duplicate PATH entries",
        )
      }
      seen[entry] = true
    }
    finding(
      Passed,
      "path_present",
      "Required directory is a complete PATH entry",
    )
  } else {
    finding(
      Failed,
      "path_missing",
      "Required directory is not a PATH entry",
      remediation="Add the required directory to PATH",
    )
  }
}

///|
pub fn path_rule(
  id : String,
  required : String,
  windows~ : Bool = false,
) -> Rule {
  rule(id, "environment", ctx => {
    match ctx.facts.get("PATH") {
      Some(value) => inspect_path(value, required, windows~)
      None => finding(Failed, "path_missing", "PATH is absent")
    }
  })
}

///|
pub fn platform_rule() -> Rule {
  rule("environment.platform", "environment", ctx => {
    let os = ctx.facts.get("doctor.os").unwrap_or("unknown")
    let arch = ctx.facts.get("doctor.arch").unwrap_or("unknown")
    finding(
      if os == "unknown" || arch == "unknown" {
        Unsupported
      } else {
        Passed
      },
      "platform_identity",
      "Host platform identity",
      evidence=[
        { key: "os", value: os, sensitive: false, },
        { key: "architecture", value: arch, sensitive: false, },
      ],
    )
  })
}