// Terminal — content rendered from facts.json.
// Edit variations/term/facts.json to change what the terminal says.
// We hydrate TERM_CONTENT from facts loaded before the shell mounts.

window.TERM_CONTENT_LOADER = async function () {
  const C = window.TERM_PALETTE;
  const span = (color, text) => ({ type: 'span', color, text });
  const plain = (t) => ({ type: 'span', text: t });
  const line = (...parts) => ({ type: 'line', parts });
  const blank = () => ({ type: 'line', parts: [] });
  const h = (t) => line(span(C.magenta, '## '), span(C.fg, t));
  const k = (label, value, col = C.blue) => line(span(col, label.padEnd(11)), plain(' = '), span(C.yellow, `"${value}"`));

  let F;
  try {
    const r = await fetch('facts.json', { cache: 'no-store' });
    F = await r.json();
  } catch (e) {
    F = { about: { name: 'Shawn', intro: '(facts.json failed to load)', likes: [], footer: '' }, projects: [], now: [], uses: [], contact: [], fortunes: ['(no fortunes)'], motd: '', secret: '' };
  }

  const intro = F.about.intro.split(/"([^"]+)"/).map((seg, i) =>
    i % 2 === 1 ? span(C.yellow, `"${seg}"`) : plain(seg)
  );

  return {
    banner: [
      line(span(C.orange, ' __  __ _____ _    ___ _____   _   _ _____ _____ ')),
      line(span(C.orange, '|  \\/  |  ___/ \\  / __| ____| | \\ | | ____|_   _|')),
      line(span(C.orange, '| |\\/| | |_ / _ \\| |  |  _|   |  \\| |  _|   | |  ')),
      line(span(C.orange, '| |  | |  _/ ___ \\ |__| |___  | |\\  | |___  | |  ')),
      line(span(C.orange, '|_|  |_|_|/_/   \\_\\___|_____| |_| \\_|_____| |_|  ')),
    ],

    help: [
      line(span(C.dim, '# available commands — tab-complete works')),
      blank(),
      ...[
        ['help',          'this list'],
        ['about',         'who I am'],
        ['projects',      'things I work on'],
        ['now',           'what I\'m up to'],
        ['uses',          'my setup'],
        ['contact',       'where to find me'],
        ['ls [path]',     'list files'],
        ['cat <file>',    'print a file'],
        ['clear',         'clear screen'],
        ['banner',        'print the mface banner'],
        ['motd',          'message of the day'],
        ['fortune',       'a short, possibly true thing'],
        ['cowsay <text>', 'a cow says <text>'],
        ['sl',            'steam locomotive'],
        ['snake',         'play snake'],
        ['hangman',       'play hangman'],
        ['sudo <cmd>',    'become root (good luck)'],
        ['exit',          'log out (kind of)'],
      ].map(([cmd, desc]) => line(span(C.green, '  ' + cmd.padEnd(15)), span(C.fg, desc))),
      blank(),
      line(span(C.dim, '# try also: '), span(C.yellow, 'ls -a'), span(C.dim, ' · '), span(C.yellow, 'rm -rf /'), span(C.dim, ' · hint: konami code')),
    ],

    about: [
      h(F.about.name),
      blank(),
      line(...intro),
      blank(),
      line(span(C.fg, 'I like:')),
      ...F.about.likes.map((s) => line(span(C.dim, '  - '), plain(s))),
      blank(),
      line(span(C.dim, F.about.footer)),
    ],

    projects: [
      line(span(C.dim, `total ${F.projects.length}`)),
      ...F.projects.map((p) => {
        const col = { dir: C.blue, yaml: C.yellow, md: C.cyan, html: C.magenta }[p.kind] || C.fg;
        const perm = p.kind === 'dir' ? 'drwxr-xr-x' : '-rw-r--r--';
        return line(
          span(C.dim, perm + ' '),
          span(C.dim, 'shawn '),
          p.url
            ? { type: 'link', href: p.url, color: col, text: p.name.padEnd(16), bold: true }
            : span(col, p.name.padEnd(16)),
          span(C.dim, (p.size || '—').padStart(6) + '  '),
          span(C.fg, p.desc),
        );
      }),
    ],

    now: [
      line(span(C.dim, '# tailing now.log — ^C to stop')),
      ...F.now.map(([t, lvl, m]) => {
        const lc = { INFO: C.green, NOTE: C.cyan, DEBUG: C.dim, WARN: C.yellow, ERR: C.red }[lvl] || C.fg;
        return line(span(C.dim, `[${t}] `), span(lc, lvl.padEnd(5)), span(C.fg, ' ' + m));
      }),
      line(span(C.red, '^C')),
    ],

    uses: [
      line(span(C.dim, '# /etc/uses.conf — human-maintained')),
      blank(),
      ...F.uses.map(([kk, vv]) => k(kk, vv)),
    ],

    contact: [
      line(span(C.dim, '[finger: shawn@mface.net]')),
      ...F.contact.map(([kk, vv, url]) => line(
        span(C.blue, kk.padEnd(10) + ' '),
        url ? { type: 'link', href: url, color: C.cyan, text: vv } : span(C.fg, vv),
      )),
    ],

    motd: [
      line(span(C.magenta, '── message of the day ──')),
      line(span(C.fg, `  "${F.motd}"`)),
      line(span(C.dim, '  — shawn, probably')),
    ],

    secret: [
      line(span(C.magenta, '~ .secret ~')),
      line(span(C.fg, F.secret)),
    ],

    fs: {
      '/': ['home/', 'etc/', 'var/', 'README'],
      '/home/shawn': ['about.md', 'projects/', 'now.log', 'contact.vcf', '.secret'],
      '/home/shawn/projects': ['skaphos/', 'rillanai/', 'idp/', 'pipeline.yml', 'mface.net'],
      '/etc': ['uses.conf', 'motd'],
    },
    fileMap: {
      'about.md': 'about', 'now.log': 'now', 'uses.conf': 'uses',
      'motd': 'motd', 'contact.vcf': 'contact', '.secret': 'secret', 'README': 'about',
    },

    fortunes: F.fortunes,
    site: F.site,
  };
};
