import test from "node:test";
import assert from "node:assert/strict";
import {
  buildStudySheet,
  renderStudySheetMarkdown,
  studySheetFileName
} from "./study-sheet.js";

const runQuestions = [
  {
    id: "boot",
    type: "terminal",
    number: "01",
    topic: "Systemstart",
    title: "Bootmeldungen lesen",
    chapterId: "kap-01",
    acceptedAnswers: ["journalctl -b -p err"],
    signal: "Dienst startet nicht",
    tool: "journalctl",
    decision: "Fehler im Bootlog eingrenzen",
    risk: "Nicht blind neu starten",
    xp: 90
  },
  {
    id: "perm",
    type: "choice",
    number: "02",
    topic: "Rechte",
    title: "SUID prüfen",
    chapterId: "kap-01",
    choices: [
      { label: "find / -perm -4000", correct: true },
      { label: "chmod 777 /", correct: false }
    ],
    signal: "Unerwartete Rechte",
    tool: "find",
    decision: "SUID-Dateien auflisten",
    risk: "Keine Rechte pauschal ändern",
    xp: 80
  }
];

const state = {
  xp: 150,
  integrity: 96,
  attempts: { boot: 2 },
  hints: ["perm"],
  completed: ["boot", "perm"]
};

const results = [
  {
    id: "kap-01",
    number: "01",
    topic: "Systemstart",
    total: 2,
    completed: 2,
    mistakes: 2,
    hints: 1,
    clean: 0
  }
];

const sheet = buildStudySheet({
  modeLabel: "Lernmodus",
  runQuestions,
  state,
  results,
  score: 812,
  createdAt: "2026-07-30"
});

test("stellt Fragen mit Fehlern und Hinweisen nach vorn", () => {
  assert.deepEqual(
    sheet.review.map((entry) => entry.id),
    ["boot", "perm"]
  );
  assert.equal(sheet.review[0].wrongAttempts, 2);
  assert.equal(sheet.review[0].hintUsed, false);
  assert.equal(sheet.review[0].solution, "journalctl -b -p err");
  assert.equal(sheet.review[1].hintUsed, true);
  assert.equal(sheet.review[1].solution, "find / -perm -4000");
  assert.equal(sheet.totals.mistakes, 2);
  assert.equal(sheet.totals.hints, 1);
});

test("lässt fehlerfreie Fragen aus der Nacharbeit heraus", () => {
  const clean = buildStudySheet({
    modeLabel: "Kurslauf",
    runQuestions,
    state: { ...state, attempts: {}, hints: [] },
    results,
    score: 1000,
    createdAt: "2026-07-30"
  });

  assert.deepEqual(clean.review, []);
});

test("schreibt ein Markdown ohne personenbezogene Daten", () => {
  const markdown = renderStudySheetMarkdown(sheet);

  assert.match(markdown, /^# Kernel Panic — mein Lern-Spickzettel/);
  assert.match(markdown, /Lernmodus · 2 Fragen · 150 XP · Integrität 96 %/);
  assert.match(markdown, /## Diese Fragen saßen nicht sofort \(2\)/);
  assert.match(
    markdown,
    /### Kapitel 01 · Systemstart · „Bootmeldungen lesen"/
  );
  assert.match(markdown, /- \*\*Lösung:\*\* `journalctl -b -p err`/);
  assert.match(markdown, /- \*\*Signal:\*\* Dienst startet nicht/);
  assert.match(markdown, /## Kapitelbilanz/);
  assert.doesNotMatch(markdown, /Alias|André|Stiebitz/);
});

test("benennt die Datei nach Modus und Datum", () => {
  assert.equal(
    studySheetFileName(sheet),
    "kernel-panic-spickzettel-lernmodus-2026-07-30.md"
  );
});
