/** Jetable. Mesures fines : tuiles vs lignes, filtres, complétion, portée. */
import { bodyRows, get, signIn, taskIds, visible } from "./probe"

function lines(html: string): string[] {
  return visible(html)
    .split("\n")
    .map((l) => l.trim())
    .filter(Boolean)
}

/** La ligne d'en-tête : la seule qui porte « · N people · ». */
function header(html: string): string {
  return lines(html).find((l) => /· \d+ (people|คน) ·/.test(l)) ?? "(aucune)"
}

/** Les tuiles, dans l'ordre : Late, Due today, Waiting, Completion. */
function tiles(html: string): string[] {
  const all = lines(html)
  const at = all.findIndex((l) => /· \d+ (people|คน) ·/.test(l))
  return all.slice(at + 1, at + 14)
}

/** Le compteur « X of Y » de la barre de filtres. */
function shown(html: string): string {
  return (
    lines(html).find((l) => /^\d+ of \d+$|^\d+ duties, \d+ dates$/.test(l)) ??
    "(aucun)"
  )
}

const admin = await signIn("narin", "admin1")
const manager = await signIn("malai", "manager1")

console.log("=== 1. les tuiles valent-elles les lignes sous chaque onglet ===")
for (const dept of ["Top management", "Accounting", "Accessory Center", "CAD Design"]) {
  const q = `dept=${encodeURIComponent(dept)}`
  const home = await get(admin, `/team?${q}`)
  console.log("\n", dept)
  console.log("  en-tête :", header(home.body))
  console.log("  tuiles  :", tiles(home.body).join(" | "))
  for (const tab of ["late", "today", "waiting"] as const) {
    const grouped = await get(admin, `/team?${q}&tab=${tab}`)
    const flat = await get(admin, `/team?${q}&tab=${tab}&group=off`)
    console.log(
      `  ${tab.padEnd(8)} tbody=${bodyRows(grouped.body)} tâches=${taskIds(grouped.body).length}` +
        (tab === "late"
          ? ` | dégroupé tbody=${bodyRows(flat.body)} tâches=${taskIds(flat.body).length}`
          : "") +
        ` | compteur="${shown(grouped.body)}"`,
    )
  }
}

console.log("\n=== 2. un filtre rétrécit-il tableau ET compteur ===")
const dept = "dept=Accounting"
const base = await get(admin, `/team?${dept}&tab=late&group=off`)
console.log("sans filtre :", bodyRows(base.body), "lignes,", shown(base.body))
// On prend un mot d'une des descriptions présentes.
const duty = (base.body.match(/\/tasks\/[0-9a-f-]{36}"[^>]*>([^<]{4,40})</) ?? [])[1]
console.log("duty choisi :", duty)
if (duty) {
  const word = duty.split(" ")[0]!
  const narrowed = await get(
    admin,
    `/team?${dept}&tab=late&group=off&duty=${encodeURIComponent(word)}`,
  )
  console.log(
    `filtre duty="${word}" :`,
    bodyRows(narrowed.body),
    "lignes,",
    shown(narrowed.body),
    "| bouton Clear présent :",
    narrowed.body.includes("Clear filters"),
  )
  const none = await get(admin, `/team?${dept}&tab=late&group=off&duty=zzzzz`)
  console.log("filtre qui ne matche rien :", bodyRows(none.body), "lignes,", shown(none.body))
}
console.log("Clear absent sans filtre :", !base.body.includes("Clear filters"))

console.log("\n=== 3. la portée : un manager peut-il élargir ===")
const wide = await get(manager, "/team?dept=Accounting&dept=Top+management")
console.log("manager avec ?dept=Accounting&Top management →", header(wide.body))
const own = await get(manager, "/team")
console.log("manager sans ?dept= →", header(own.body))

console.log("\n=== 4. url illisible ===")
for (const q of ["tab=banana", "sort=Oldest", "group=maybe", "tab=late&sort=zzz"]) {
  const r = await get(manager, `/team?${q}`)
  console.log(q, "→", r.status, "|", shown(r.body))
}
