// Display-only prettifier for raw llama-swap model ids shown in the model
// pickers (BooChat ModelPicker + BooCode AgentComposerBar). The actual model id
// sent to the backend is never changed — this only affects what's rendered.
//
//   qwen3.6-35b-a3b-mxfp4         -> Qwen3.6 35B
//   qwopus3.5-9b-coder-mtp        -> Qwopus3.5 9B Coder
//   qwen3.5-9b-deepseek-v4-mtp    -> Qwen3.5 9B Deepseek
//   OpenCode Zen/Big Pickle       -> Big Pickle
//   llama-swap/Qwen 3.6 27B MTP   -> Qwen 3.6 27B MTP
//
// OpenCode surfaces models as "Provider Group/Model Name"; we drop the group
// prefix and show just the model name. Conservative otherwise: ids that don't
// look like the `<family><ver>-<size>-…` shape (e.g. "Opus (latest)",
// "nemotron-nano-4b") are returned unchanged, so friendly labels aren't mangled.

// Quant / format / speculative-decoding tags that carry no meaning for a human
// scanning the picker. Dropped from the label.
const DROP_TOKENS = new Set([
  'mtp', 'mxfp4', 'fp4', 'fp8', 'fp16', 'bf16',
  'q4', 'q5', 'q6', 'q8', 'int4', 'int8',
  'awq', 'gptq', 'gguf',
]);

export function formatModelLabel(raw: string): string {
  if (!raw) return raw;
  // OpenCode-style "Provider Group/Model Name" → keep just the model name.
  const slash = raw.lastIndexOf('/');
  if (slash >= 0) raw = raw.slice(slash + 1).trim();

  if (/\s/.test(raw)) return raw; // already a friendly (spaced) label
  const tokens = raw.split('-');
  const head = tokens[0] ?? '';
  // First token must look like a family+version (letters then a digit), e.g.
  // qwen3.6 / qwopus3.5. Otherwise leave the id alone.
  if (!/^[a-z]+\d/.test(head)) return raw;

  const kept: string[] = [];
  tokens.forEach((t, i) => {
    if (i === 0) {
      kept.push(t.charAt(0).toUpperCase() + t.slice(1)); // qwen3.6 -> Qwen3.6
      return;
    }
    if (/^\d+(\.\d+)?b$/.test(t)) { kept.push(t.toUpperCase()); return; } // size: 9B, 27B, 35B
    if (/^v\d+$/.test(t)) return;   // variant tag: v1, v2, v4
    if (/^a\d+b$/.test(t)) return;  // MoE active-params tag: a3b
    if (DROP_TOKENS.has(t)) return; // quant / format / decoding tags
    kept.push(t.charAt(0).toUpperCase() + t.slice(1)); // descriptive: coder, deepseek
  });
  return kept.join(' ');
}
