#!/usr/bin/env python3
"""
Test script to verify "Tell me" format in speaking question generation.
This tests that all questions start with "Tell me" and are context-appropriate.
"""

import sys
import os

# Add the project root to Python path
project_root = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, project_root)

class MockTopic:
    def __init__(self, title, reading_content):
        self.title = title
        self.reading_content = reading_content

def test_tell_me_format():
    """Test that all generated questions start with 'Tell me'."""
    
    print("Testing 'Tell me' format in speaking questions...")
    
    # Test case 1: University/education content
    education_topic = MockTopic(
        title="About Myself - University Life",
        reading_content="""
        Student: Hi, I'm John. I study at Punjab University in the Faculty of Engineering. 
        My department is Computer Science, and I'm in my third year. I really enjoy 
        programming and mathematics. My favorite subjects are algorithms and database systems.
        Teacher: That's interesting! Tell me about your daily schedule at university.
        Student: I have classes from 9 AM to 3 PM on most days. I spend time in the computer lab 
        practicing coding. In the evening, I study and work on assignments.
        """
    )
    
    # Test case 2: Family content
    family_topic = MockTopic(
        title="About Myself - Family Background", 
        reading_content="""
        I come from a middle-class family in Lahore. My father is a teacher and my mother 
        is a nurse. I have two siblings - an older brother who works as an engineer and 
        a younger sister who is still in high school. We are very close and spend a lot 
        of time together on weekends. My family values education and hard work.
        """
    )
    
    # Test case 3: Empty content (should still generate "Tell me" questions)
    empty_topic = MockTopic(
        title="Myself",
        reading_content=""
    )
    
    test_cases = [
        ("Education Topic", education_topic),
        ("Family Topic", family_topic), 
        ("Empty Content Topic", empty_topic)
    ]
    
    try:
        import flask
        app = flask.Flask(__name__)
        app.config.update({
            'MISTRAL_API_KEY': 'test-key',  # Won't actually call AI in this test
            'USE_AI_TRANSCRIPTION': False,  # Disable AI to test local fallback
        })
        
        with app.app_context():
            from app.services.ai_assessment import AIAssessment
            
            ai = AIAssessment()
            
            for test_name, topic in test_cases:
                print(f"\n--- {test_name} ---")
                print(f"Topic: {topic.title}")
                print(f"Content length: {len(topic.reading_content)} characters")
                
                if topic.reading_content:
                    preview = topic.reading_content[:100] + "..." if len(topic.reading_content) > 100 else topic.reading_content
                    print(f"Content preview: {preview}")
                
                questions = ai.generate_speaking_questions(topic)
                
                print(f"\nGenerated {len(questions)} questions:")
                all_correct = True
                
                for i, q in enumerate(questions, 1):
                    starts_with_tell_me = q.text.strip().lower().startswith('tell me')
                    status = "✓" if starts_with_tell_me else "✗"
                    
                    print(f"{i}. {status} {q.text}")
                    
                    if not starts_with_tell_me:
                        all_correct = False
                        print(f"   ⚠️  ERROR: Question {i} does not start with 'Tell me'")
                    
                    # Check for forbidden phrases
                    forbidden = ['read and speak', 'read aloud', 'repeat after', 'speak these sentences']
                    if any(phrase in q.text.lower() for phrase in forbidden):
                        all_correct = False
                        print(f"   ⚠️  ERROR: Question {i} contains forbidden phrase")
                
                if all_correct:
                    print(f"   ✅ All questions correctly start with 'Tell me'")
                else:
                    print(f"   ❌ Some questions do not follow the 'Tell me' format")
                    
    except Exception as e:
        print(f"Error during testing: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    test_tell_me_format()