/**
 * Generate contextual follow-up questions based on AI response
 */
export function generateFollowUpQuestions(
  userMessage: string,
  aiResponse: string,
  limit: number = 3
): string[] {
  const questions: string[] = [];
  const lowerResponse = aiResponse.toLowerCase();
  const lowerMessage = userMessage.toLowerCase();

  // Topic-based follow-up questions
  const topicQuestions: Record<string, string[]> = {
    sleep: [
      "What if my baby still wakes up frequently?",
      "How can I create a better bedtime routine?",
      "When should I be concerned about sleep patterns?",
      "Tips for sleep training?",
      "How long should naps be?",
    ],
    feeding: [
      "How do I know if my baby is eating enough?",
      "What are signs of overfeeding?",
      "When should I introduce solids?",
      "How to handle feeding refusal?",
      "Tips for establishing a feeding schedule?",
    ],
    development: [
      "What milestones should I watch for next?",
      "How can I support my baby development?",
      "When should I talk to my pediatrician?",
      "Activities to encourage development?",
      "Is my baby developing normally?",
    ],
    health: [
      "When should I call the doctor?",
      "What are warning signs to watch for?",
      "How to prevent common illnesses?",
      "When to give medicine?",
      "Normal vs concerning symptoms?",
    ],
    crying: [
      "How to soothe a crying baby?",
      "What if nothing seems to work?",
      "Is this normal crying or colic?",
      "When does crying typically decrease?",
      "Tips for staying calm?",
    ],
    schedule: [
      "How to create a daily routine?",
      "When to adjust the schedule?",
      "Flexibility vs consistency?",
      "Managing multiple children schedules?",
      "Sample schedule for this age?",
    ],
    growth: [
      "How to track growth?",
      "What is a healthy growth rate?",
      "When to worry about weight?",
      "Growth spurts vs concerns?",
      "Nutrition for healthy growth?",
    ],
  };

  // Detect topics in the conversation
  const detectedTopics = new Set<string>();

  Object.keys(topicQuestions).forEach((topic) => {
    if (lowerResponse.includes(topic) || lowerMessage.includes(topic)) {
      detectedTopics.add(topic);
    }
  });

  // Add sleep-related keywords
  if (
    lowerResponse.match(/\b(sleep|nap|bedtime|wake|waking|night)\b/) ||
    lowerMessage.match(/\b(sleep|nap|bedtime|wake|waking|night)\b/)
  ) {
    detectedTopics.add("sleep");
  }

  // Add feeding-related keywords
  if (
    lowerResponse.match(/\b(feed|feeding|eat|eating|bottle|breast|formula|milk|nurse|nursing)\b/) ||
    lowerMessage.match(/\b(feed|feeding|eat|eating|bottle|breast|formula|milk|nurse|nursing)\b/)
  ) {
    detectedTopics.add("feeding");
  }

  // Add development keywords
  if (
    lowerResponse.match(/\b(milestone|develop|crawl|walk|talk|sit|roll)\b/) ||
    lowerMessage.match(/\b(milestone|develop|crawl|walk|talk|sit|roll)\b/)
  ) {
    detectedTopics.add("development");
  }

  // Add health keywords
  if (
    lowerResponse.match(/\b(sick|fever|doctor|medicine|vaccine|health|symptom)\b/) ||
    lowerMessage.match(/\b(sick|fever|doctor|medicine|vaccine|health|symptom)\b/)
  ) {
    detectedTopics.add("health");
  }

  // Add crying keywords
  if (
    lowerResponse.match(/\b(cry|crying|fuss|fussy|colic)\b/) ||
    lowerMessage.match(/\b(cry|crying|fuss|fussy|colic)\b/)
  ) {
    detectedTopics.add("crying");
  }

  // Add schedule keywords
  if (
    lowerResponse.match(/\b(schedule|routine|timing|consistent|pattern)\b/) ||
    lowerMessage.match(/\b(schedule|routine|timing|consistent|pattern)\b/)
  ) {
    detectedTopics.add("schedule");
  }

  // Add growth keywords
  if (
    lowerResponse.match(/\b(growth|spurt|weight|height|size|growing)\b/) ||
    lowerMessage.match(/\b(growth|spurt|weight|height|size|growing)\b/)
  ) {
    detectedTopics.add("growth");
  }

  // Collect questions from detected topics
  detectedTopics.forEach((topic) => {
    const topicQs = topicQuestions[topic] || [];
    // Add random questions from this topic
    const shuffled = topicQs.sort(() => Math.random() - 0.5);
    questions.push(...shuffled.slice(0, 2));
  });

  // Generic follow-ups if no specific topics detected or to fill gaps
  const genericQuestions = [
    "Can you explain that in more detail?",
    "What else should I know about this?",
    "Are there any common mistakes to avoid?",
    "How long does this usually last?",
    "What can I do to help?",
  ];

  if (questions.length < limit) {
    const needed = limit - questions.length;
    const shuffledGeneric = genericQuestions.sort(() => Math.random() - 0.5);
    questions.push(...shuffledGeneric.slice(0, needed));
  }

  // Response-specific follow-ups based on keywords
  if (lowerResponse.includes("consult") || lowerResponse.includes("pediatrician")) {
    questions.unshift("What symptoms should prompt an immediate call?");
  }

  if (lowerResponse.includes("normal") || lowerResponse.includes("typical")) {
    questions.unshift("What are signs this might NOT be normal?");
  }

  if (lowerResponse.includes("weeks") || lowerResponse.includes("months")) {
    questions.push("What comes after this stage?");
  }

  // Remove duplicates and limit
  const uniqueQuestions = Array.from(new Set(questions));

  return uniqueQuestions.slice(0, limit);
}
