import { describe, it, expect } from 'vitest';
import { classifyIntent } from './intent-detector';

describe('Intent Detector', () => {
  describe('classifyIntent - Venting Patterns', () => {
    it('should classify "venting" when user expresses frustration', () => {
      const result = classifyIntent("I'm so frustrated with this bug");
      expect(result).toBe('venting');
    });

    it('should classify "venting" when user is stuck', () => {
      const result = classifyIntent("I've been stuck on this for hours");
      expect(result).toBe('venting');
    });

    it('should classify "venting" when something is broken', () => {
      const result = classifyIntent("My code broke again and I don't know why");
      expect(result).toBe('venting');
    });

    it('should classify "venting" with negative emotion words', () => {
      const result = classifyIntent("I hate debugging");
      expect(result).toBe('venting');
    });

    it('should classify "venting" when expressing confusion', () => {
      const result = classifyIntent("I don't understand why this isn't working");
      expect(result).toBe('venting');
    });

    it('should classify "venting" with problem-focused language', () => {
      const result = classifyIntent("This keeps failing and I can't figure it out");
      expect(result).toBe('venting');
    });

    it('should classify "venting" when mentioning time spent struggling', () => {
      const result = classifyIntent("I've been working on this all day");
      expect(result).toBe('venting');
    });

    it('should classify "venting" with "error" keyword', () => {
      const result = classifyIntent("I keep getting this error and don't know what to do");
      expect(result).toBe('venting');
    });
  });

  describe('classifyIntent - Insight Patterns', () => {
    it('should classify "insight" when user has realization', () => {
      const result = classifyIntent("I finally get how closures work");
      expect(result).toBe('insight');
    });

    it('should classify "insight" when sharing understanding', () => {
      const result = classifyIntent("Now I understand the difference between let and const");
      expect(result).toBe('insight');
    });

    it('should classify "insight" when something clicked', () => {
      const result = classifyIntent("It just clicked - React hooks are all about side effects");
      expect(result).toBe('insight');
    });

    it('should classify "insight" when problem is solved', () => {
      const result = classifyIntent("I figured out the bug - it was a missing dependency");
      expect(result).toBe('insight');
    });

    it('should classify "insight" when explaining solution', () => {
      const result = classifyIntent("So the trick is to use useMemo for expensive calculations");
      expect(result).toBe('insight');
    });

    it('should classify "insight" with completion language', () => {
      const result = classifyIntent("Finally got it working after trying everything");
      expect(result).toBe('insight');
    });

    it('should classify "insight" when teaching concept', () => {
      const result = classifyIntent("Here's what I learned - async/await is just syntactic sugar for promises");
      expect(result).toBe('insight');
    });

    it('should classify "insight" with positive realization', () => {
      const result = classifyIntent("Realized that I was overthinking this whole time");
      expect(result).toBe('insight');
    });
  });

  describe('classifyIntent - Edge Cases', () => {
    it('should default to "venting" for ambiguous input', () => {
      const result = classifyIntent("I'm working on a project");
      expect(result).toBe('venting'); // Default to venting for safety
    });

    it('should classify "venting" for mixed signals', () => {
      const result = classifyIntent("I figured it out but it took forever");
      expect(result).toBe('insight'); // Positive indicators take precedence
    });

    it('should handle empty string gracefully', () => {
      const result = classifyIntent("");
      expect(result).toBe('venting');
    });

    it('should be case-insensitive', () => {
      const result = classifyIntent("I HATE WHEN THIS HAPPENS");
      expect(result).toBe('venting');
    });

    it('should classify "venting" for questions', () => {
      const result = classifyIntent("Why does this keep happening?");
      expect(result).toBe('venting');
    });

    it('should classify "insight" when solution is primary focus', () => {
      const result = classifyIntent("The solution was to use useRef instead of useState");
      expect(result).toBe('insight');
    });
  });

  describe('classifyIntent - Accuracy Requirements', () => {
    it('should achieve >85% accuracy on common venting patterns', () => {
      const ventingInputs = [
        "I'm so frustrated",
        "This is broken",
        "I don't understand",
        "Stuck on this bug",
        "Keeps failing",
        "Why won't this work",
        "I hate this",
        "Can't figure it out",
        "This is so confusing",
        "Getting an error",
      ];

      const correct = ventingInputs.filter(input => classifyIntent(input) === 'venting').length;
      const accuracy = (correct / ventingInputs.length) * 100;
      expect(accuracy).toBeGreaterThan(85);
    });

    it('should achieve >85% accuracy on common insight patterns', () => {
      const insightInputs = [
        "I finally get it",
        "Figured it out",
        "Now I understand",
        "It just clicked",
        "Solved the problem",
        "Here's what I learned",
        "Realized something",
        "Got it working",
        "The solution is",
        "So the key is",
      ];

      const correct = insightInputs.filter(input => classifyIntent(input) === 'insight').length;
      const accuracy = (correct / insightInputs.length) * 100;
      expect(accuracy).toBeGreaterThan(85);
    });
  });
});
