import type { WorkshopSlug } from "./workshops";

export interface IntentClassification {
  slug: WorkshopSlug;
  prefilledPrompt: string;
}

/**
 * Phase 10 — simple keyword-based intent classifier.
 *
 * Given a freeform prompt from the Studio prompt bar, return the workshop
 * slug to route to and a copy of the prompt to pre-fill. Returns `null` if
 * no workshop matches — callers should fall back to sending the prompt
 * through to the Assistant.
 *
 * This is deliberately a single pure function with no state, no network,
 * no LLM. Phase 14 may replace it with a real classifier; for Phase 10 we
 * want deterministic routing and exhaustive unit-testable behavior.
 *
 * Ordering matters: more specific patterns (convert with explicit formats,
 * wallpaper, diagram, icon) are checked first, so that "convert this pdf
 * to docx" doesn't accidentally fall through the `documents` branch.
 */
export function classifyIntent(prompt: string): IntentClassification | null {
  const lower = prompt.toLowerCase().trim();
  if (!lower) return null;

  // Convert — most specific first: explicit "convert X to Y", "from X to Y",
  // or any of the common file-format keywords that strongly imply conversion.
  if (
    /\bconvert\b/.test(lower) ||
    /\bfrom\s+\w+\s+to\s+\w+\b/.test(lower) ||
    /\b(pdf|docx?|md|markdown|html|rtf|odt|epub|txt)\s+to\s+\w+/.test(lower) ||
    /\bto\s+(pdf|docx?|md|markdown|html|rtf|odt|epub|txt)\b/.test(lower)
  ) {
    return { slug: "convert", prefilledPrompt: prompt };
  }

  // Wallpaper
  if (/\bwallpaper(s)?\b|background\s+image|desktop\s+bg|lock\s+screen/.test(lower)) {
    return { slug: "wallpapers", prefilledPrompt: prompt };
  }

  // Diagram
  if (/\b(diagram|flowchart|sequence\s+diagram|mermaid|architecture\s+diagram|uml)\b/.test(lower)) {
    return { slug: "diagrams", prefilledPrompt: prompt };
  }

  // Icons (note: "icon" must match as a whole word, not "iconic")
  if (/\bicon(s|\s+set|\s+pack)?\b|svg\s+icon/.test(lower)) {
    return { slug: "icons", prefilledPrompt: prompt };
  }

  // Themes / palettes
  if (/\btheme(s)?\b|color\s+palette|colour\s+palette|\bpalette\b/.test(lower)) {
    return { slug: "themes", prefilledPrompt: prompt };
  }

  // Brand kits (before documents — "logo" and "brand identity" beat "document")
  if (/\b(brand|logo|identity|style\s+guide)\b/.test(lower)) {
    return { slug: "brand-kits", prefilledPrompt: prompt };
  }

  // Social
  if (/\b(social|tweet|post|instagram|linkedin|twitter|x\.com|carousel)\b/.test(lower)) {
    return { slug: "social", prefilledPrompt: prompt };
  }

  // Documents — PDF/report/invoice/one-pager (after convert so "pdf to X" routes to convert)
  if (/\b(pdf|document|invoice|report|one-?pager|whitepaper|resume|cv)\b/.test(lower)) {
    return { slug: "documents", prefilledPrompt: prompt };
  }

  return null;
}
