#!/usr/bin/env python3
"""
Test script to verify speaking question generation without running the full Flask app.
This helps us debug the AI prompt and ensure it generates proper topic-based questions.
"""

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)

# Mock Flask current_app for the AI assessment module
class MockApp:
    def __init__(self):
        self.config = {
            'MISTRAL_API_KEY': 'your-mistral-api-key-here',  # You'll need to set this
            'USE_AI_TRANSCRIPTION': True,
            'FFMPEG_PATH': None,
            'FFPROBE_PATH': None
        }

class MockTopic:
    def __init__(self, title, reading_content):
        self.title = title
        self.reading_content = reading_content

def test_speaking_question_generation():
    """Test speaking question generation with different content types."""
    
    print("Testing speaking question generation...")
    
    # Test case 1: Topic with good reading content
    topic_with_content = MockTopic(
        title="About Myself",
        reading_content="""
        Introducing yourself is an important skill in English. When you meet new people, 
        you should share information about your name, where you're from, your hobbies, 
        and what you do for work or study. This helps others understand who you are and 
        creates connections. Some people are shy about talking about themselves, but 
        practice makes it easier. You can start with simple information and add more 
        details as you become more comfortable.
        """
    )
    
    # Test case 2: Topic with no/empty reading content  
    topic_without_content = MockTopic(
        title="Myself",
        reading_content=""
    )
    
    try:
        # Mock Flask app context
        import flask
        app = flask.Flask(__name__)
        app.config.update({
            'MISTRAL_API_KEY': 'your-mistral-api-key-here',
            'USE_AI_TRANSCRIPTION': True,
            'FFMPEG_PATH': None,
            'FFPROBE_PATH': None
        })
        
        with app.app_context():
            from app.services.ai_assessment import AIAssessment
            
            # Create AI assessor
            ai = AIAssessment()
            ai.enable_ai_transcription(True)
            
            print("\n--- Test 1: Topic with reading content ---")
            print(f"Topic: {topic_with_content.title}")
            print(f"Content length: {len(topic_with_content.reading_content)} characters")
            
            questions1 = ai.generate_speaking_questions(topic_with_content)
            
            print(f"Generated {len(questions1)} questions:")
            for i, q in enumerate(questions1, 1):
                print(f"{i}. {q.text}")
                # Check if any contain "read and speak" or similar
                if any(phrase in q.text.lower() for phrase in ['read and speak', 'read aloud', 'repeat after']):
                    print(f"   ⚠️  WARNING: Question {i} contains generic 'read aloud' instruction!")
                else:
                    print(f"   ✓ Question {i} looks good")
            
            print("\n--- Test 2: Topic without reading content ---")
            print(f"Topic: {topic_without_content.title}")
            print(f"Content length: {len(topic_without_content.reading_content)} characters")
            
            questions2 = ai.generate_speaking_questions(topic_without_content)
            
            print(f"Generated {len(questions2)} questions:")
            for i, q in enumerate(questions2, 1):
                print(f"{i}. {q.text}")
                if any(phrase in q.text.lower() for phrase in ['read and speak', 'read aloud', 'repeat after']):
                    print(f"   ⚠️  WARNING: Question {i} contains generic 'read aloud' instruction!")
                else:
                    print(f"   ✓ Question {i} looks good")
                    
    except Exception as e:
        print(f"Error during testing: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    test_speaking_question_generation()