/** Jetable. Les chiffres et la structure rendus par /team, après la refonte visuelle. */
const BASE = "http://127.0.0.1:4214"

async function signIn(username: string, password: string): Promise<string> {
  const res = await fetch(`${BASE}/api/auth/sign-in/username`, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ username, password }),
    redirect: "manual",
  })
  const raw = res.headers.getSetCookie()
  if (!raw.length) throw new Error(`no cookie for ${username}`)
  return raw.map((c) => c.split(";")[0]).join("; ")
}

function text(html: string): string {
  return html
    .replace(/<script[\s\S]*?<\/script>/g, " ")
    .replace(/<style[\s\S]*?<\/style>/g, " ")
    .replace(/<[^>]+>/g, "\n")
    .replace(/&#x27;/g, "'")
    .replace(/&amp;/g, "&")
    .split("\n")
    .map((s) => s.trim())
    .filter(Boolean)
    .join("\n")
}

const cookie = await signIn("narin", "admin1")

async function look(label: string, path: string) {
  const res = await fetch(`${BASE}${path}`, { headers: { cookie }, redirect: "manual" })
  const html = res.status === 200 ? await res.text() : ""
  const body = html.slice(html.indexOf("<main") >= 0 ? html.indexOf("<main") : 0)
  const t = text(html)
  const after = (needle: string, n: number) => {
    const lines = t.split("\n")
    const i = lines.indexOf(needle)
    return i < 0 ? ["MISSING: " + needle] : lines.slice(i, i + n)
  }
  console.log(`\n===== ${label} — ${res.status} =====`)
  console.log("h1              :", (html.match(/<h1[^>]*>([^<]*)</g) ?? []).join(" | "))
  console.log("tiles           :",
    [...after("Late", 3), "//", ...after("Due today", 3), "//",
     ...after("Waiting for a check", 3), "//", ...after("Completion, 30 days", 4)].join(" "))
  console.log("selected tiles  :", (html.match(/aria-selected="true"/g) ?? []).length,
              "| unselected:", (html.match(/aria-selected="false"/g) ?? []).length)
  console.log("aria-pressed    :", html.match(/aria-pressed="(true|false)"/)?.[0] ?? "none")
  console.log("search field    :", /Search a person or a duty|ค้นหาผู้รับผิดชอบ/.test(html))
  console.log("old text inputs :", (html.match(/list="team-(people|duties)"/g) ?? []).length)
  console.log("headers         :", (body.match(/<th[^>]*>(?:(?!<\/th>)[\s\S])*<\/th>/g) ?? [])
      .map((h) => text(h).replace(/\n/g, " ")).join(" · "))
  console.log("pill            :", (t.match(/\d+ missed|one missed|\d+ dates|one date/g) ?? []).slice(0, 4).join(", "))
  console.log("count line      :", t.match(/\d+ of \d+/g)?.slice(-1)[0])
  console.log("token classes   :", ["text-destructive", "text-primary-tint-foreground", "text-warning",
      "aria-selected:border-primary"].filter((c) => html.includes(c)).join(" "))
  console.log("raw hex in page :", (body.match(/#[0-9a-f]{6}/gi) ?? []).slice(0, 5))
  console.log("dept control    :", html.match(/placeholder="[^"]*"/g)?.join(" | "))
}

await look("Top management, late", "/team?dept=Top+management")
await look("Top management, today", "/team?dept=Top+management&tab=today")
await look("Top management, waiting", "/team?dept=Top+management&tab=waiting")
await look("Top management, flat late", "/team?dept=Top+management&group=off")
await look("Accounting, late", "/team?dept=Accounting")
await look("no dept param (falls back)", "/team")
await look("search narrows", "/team?dept=Top+management&q=weekly")

for (const [u, p] of [["malai", "manager1"], ["nattikan", "staff1"]] as const) {
  const c = await signIn(u, p)
  const r = await fetch(`${BASE}/team`, { headers: { cookie: c }, redirect: "manual" })
  console.log(`\naccess ${u}: ${r.status} ${r.headers.get("location") ?? ""}`)
}

export {}
