/**
 * Renders StaticPage.body using a tiny convention, not full markdown:
 * a line starting "## " is a subheading, consecutive "- " lines are a bullet list,
 * everything else groups into paragraphs (blank lines separate them). Kept deliberately
 * simple — no HTML, no new dependency. Works line-by-line so a heading followed
 * immediately by a bullet list (no blank line between them) still splits correctly.
 */
type Block = { type: "h2"; text: string } | { type: "ul"; items: string[] } | { type: "p"; text: string };

function parseBody(body: string): Block[] {
  const lines = body.split("\n").map((l) => l.trim());
  const blocks: Block[] = [];
  let i = 0;

  while (i < lines.length) {
    const line = lines[i];

    if (line === "") {
      i++;
      continue;
    }

    if (line.startsWith("## ")) {
      blocks.push({ type: "h2", text: line.slice(3) });
      i++;
      continue;
    }

    if (line.startsWith("- ")) {
      const items: string[] = [];
      while (i < lines.length && lines[i].startsWith("- ")) {
        items.push(lines[i].slice(2));
        i++;
      }
      blocks.push({ type: "ul", items });
      continue;
    }

    const paragraphLines: string[] = [];
    while (i < lines.length && lines[i] !== "" && !lines[i].startsWith("## ") && !lines[i].startsWith("- ")) {
      paragraphLines.push(lines[i]);
      i++;
    }
    blocks.push({ type: "p", text: paragraphLines.join(" ") });
  }

  return blocks;
}

export default function PageBody({ body }: { body: string }) {
  const blocks = parseBody(body);

  return (
    <>
      {blocks.map((block, i) => {
        if (block.type === "h2") {
          return (
            <h2 key={i} className="mt-6 text-base font-semibold text-stone-800">
              {block.text}
            </h2>
          );
        }
        if (block.type === "ul") {
          return (
            <ul key={i} className="mt-3 list-disc space-y-1 pl-5">
              {block.items.map((item, j) => (
                <li key={j}>{item}</li>
              ))}
            </ul>
          );
        }
        return (
          <p key={i} className="mt-3 leading-relaxed">
            {block.text}
          </p>
        );
      })}
    </>
  );
}
