#!/usr/bin/env python3
"""
Test script for the new conversation history system.
"""

from utils.conversation_history import conversation_history

def test_conversation_history():
    print("🧪 Testing conversation history system...\n")
    
    # Test 1: Add messages to a server channel
    print("Test 1: Adding messages to server channel")
    server_id = "123456789"
    conversation_history.add_message(server_id, "Alice", "Hello Miku!", is_bot=False)
    conversation_history.add_message(server_id, "Miku", "Hi Alice! 💙", is_bot=True)
    conversation_history.add_message(server_id, "Bob", "What's up?", is_bot=False)
    conversation_history.add_message(server_id, "Miku", "Just chatting! ✨", is_bot=True)
    
    recent = conversation_history.get_recent_messages(server_id)
    print(f"  Recent messages: {len(recent)}")
    for author, content, is_bot in recent:
        role = "BOT" if is_bot else "USER"
        print(f"    [{role}] {author}: {content}")
    
    # Test 2: Format for LLM
    print("\nTest 2: Format for LLM (OpenAI messages)")
    messages = conversation_history.format_for_llm(server_id, max_messages=4)
    for msg in messages:
        print(f"    {msg['role']}: {msg['content']}")
    
    # Test 3: Add messages to a DM channel
    print("\nTest 3: Adding messages to DM channel")
    user_id = "987654321"
    conversation_history.add_message(user_id, "Charlie", "Can you help me?", is_bot=False)
    conversation_history.add_message(user_id, "Miku", "Of course! What do you need?", is_bot=True)
    conversation_history.add_message(user_id, "Charlie", "I need song recommendations", is_bot=False)
    
    dm_messages = conversation_history.format_for_llm(user_id)
    print(f"  DM messages: {len(dm_messages)}")
    for msg in dm_messages:
        print(f"    {msg['role']}: {msg['content']}")
    
    # Test 4: Empty message filtering
    print("\nTest 4: Empty message filtering")
    conversation_history.add_message(server_id, "Dave", "", is_bot=False)  # Should be ignored
    conversation_history.add_message(server_id, "Dave", "   ", is_bot=False)  # Should be ignored
    conversation_history.add_message(server_id, "Dave", "Real message", is_bot=False)
    
    filtered = conversation_history.get_recent_messages(server_id)
    print(f"  Messages after adding empty ones: {len(filtered)}")
    print(f"  Last message: {filtered[-1][1]}")
    
    # Test 5: Message truncation
    print("\nTest 5: Message truncation")
    long_message = "A" * 600  # 600 chars
    conversation_history.add_message(server_id, "Eve", long_message, is_bot=False)
    truncated = conversation_history.format_for_llm(server_id, max_chars_per_message=500)
    last_msg = truncated[-1]['content']
    print(f"  Original length: {len(long_message)}")
    print(f"  Truncated length: {len(last_msg)}")
    print(f"  Ends with '...': {last_msg.endswith('...')}")
    
    # Test 6: Channel stats
    print("\nTest 6: Channel statistics")
    stats = conversation_history.get_channel_stats(server_id)
    print(f"  Server stats: {stats}")
    
    dm_stats = conversation_history.get_channel_stats(user_id)
    print(f"  DM stats: {dm_stats}")
    
    print("\n✅ All tests completed!")

if __name__ == "__main__":
    test_conversation_history()
