///|
pub fn markdown_report(
  text : String,
  agent : String,
  paths : Array[String],
) -> String {
  let robots = parse(text)
  join_lines([
    "# robots.txt report",
    "",
    "## Summary",
    "",
    "- " + stats_line(stats_of(robots)),
    "- policy: " + policy_summary(policy_from(robots, agent)),
    "",
    "## Decisions",
    "",
    markdown_decision_table(robots, agent, paths),
    "",
    "## Audit",
    "",
    fenced(audit_report(text)),
  ])
}

///|
pub fn markdown_decision_table(
  robots : Robots,
  agent : String,
  paths : Array[String],
) -> String {
  let lines : Array[String] = [
    "| path | decision | rule |", "| --- | --- | --- |",
  ]
  for path in paths {
    let decision = decide(robots, agent, path)
    let verdict = if decision.allowed { "allow" } else { "block" }
    let rule = if decision.matched_pattern == "" {
      "-"
    } else {
      decision.matched_kind + ":" + decision.matched_pattern
    }
    lines.push("| `" + path + "` | " + verdict + " | `" + rule + "` |")
  }
  join_lines(lines)
}

///|
pub fn markdown_inventory(text : String) -> String {
  let robots = parse(text)
  let lines : Array[String] = [
    "| agents | kind | pattern |", "| --- | --- | --- |",
  ]
  for group in robots.groups {
    let agents = join_with(group.agents, ",")
    for rule in group.rules {
      lines.push(
        "| `" + agents + "` | " + rule.kind + " | `" + rule.pattern + "` |",
      )
    }
  }
  if lines.length() == 2 {
    lines.push("| - | - | - |")
  }
  join_lines(lines)
}

///|
pub fn fenced(text : String) -> String {
  "```text\n" + text + "\n```"
}

///|
pub fn markdown_sitemaps(robots : Robots) -> String {
  if robots.sitemaps.is_empty() {
    return "_No sitemaps declared._"
  }
  let lines : Array[String] = []
  for sitemap in robots.sitemaps {
    lines.push("- " + sitemap)
  }
  join_lines(lines)
}

///|
pub fn markdown_hosts(robots : Robots) -> String {
  if robots.hosts.is_empty() {
    return "_No host directive declared._"
  }
  let lines : Array[String] = []
  for host in robots.hosts {
    lines.push("- " + host)
  }
  join_lines(lines)
}

///|
pub fn markdown_full(
  text : String,
  agent : String,
  paths : Array[String],
) -> String {
  let robots = parse(text)
  markdown_report(text, agent, paths) +
  "\n\n## Sitemaps\n\n" +
  markdown_sitemaps(robots) +
  "\n\n## Hosts\n\n" +
  markdown_hosts(robots) +
  "\n\n## Rules\n\n" +
  markdown_inventory(text)
}