#!/usr/bin/env python3
"""
Simple script to check the content of the jokes database
"""

import sqlite3
import os

def check_database():
    db_path = 'jokes.db'
    
    if not os.path.exists(db_path):
        print(f"❌ Database {db_path} does not exist!")
        return
        
    print(f"🔍 Checking database: {db_path}")
    
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()

    # Count the number of jokes in the database
    cursor.execute('SELECT COUNT(*) FROM jokes')
    count = cursor.fetchone()[0]
    print(f"📊 Total jokes in database: {count}")

    # Count the number of user sentiments
    cursor.execute('SELECT COUNT(*) FROM user_sentiments')
    sentiment_count = cursor.fetchone()[0]
    print(f"📊 Total user sentiments recorded: {sentiment_count}")

    # If there are jokes, show a few of them
    if count > 0:
        cursor.execute('''
            SELECT j.id, j.joke, j.contributor, j.sentiment_label,
                   (SELECT COUNT(*) FROM user_sentiments us WHERE us.joke_id = j.id) as sentiment_count
            FROM jokes j
            LIMIT 5
        ''')
        jokes = cursor.fetchall()
        print('\n📋 Sample of jokes in the database:')
        for i, (joke_id, joke, contributor, sentiment, sentiment_count) in enumerate(jokes, 1):
            print(f'{i:2d}. [{sentiment}] {joke[:60]}...')
            print(f'     👤 {contributor} | {sentiment_count} user ratings')
        
        # Show AI sentiment distribution
        cursor.execute('SELECT sentiment_label, COUNT(*) FROM jokes GROUP BY sentiment_label')
        distribution = cursor.fetchall()
        print(f'\n📈 AI Sentiment distribution:')
        for label, cnt in distribution:
            print(f'   {label}: {cnt} jokes')
        
        # Show user sentiment distribution if any exist
        if sentiment_count > 0:
            cursor.execute('SELECT user_sentiment, COUNT(*) FROM user_sentiments GROUP BY user_sentiment')
            user_distribution = cursor.fetchall()
            print(f'\n👥 User Sentiment distribution:')
            for sentiment, cnt in user_distribution:
                emoji = {'up': '👍', 'down': '👎', 'neutral': '😐'}[sentiment]
                print(f'   {emoji} {sentiment.capitalize()}: {cnt} ratings')
    else:
        print("\n📭 No jokes found in the database!")
        print("💡 Run populate_db.py to add sample jokes to the database.")

    conn.close()

if __name__ == '__main__':
    check_database()