
import os
import psycopg2
from psycopg2.extras import RealDictCursor

def inspect_counts():
    try:
        db_url = os.environ.get('DATABASE_URL', 'postgresql://suggestbet:SuGGesT2026SecuRe@localhost:15432/boilerplate_db')
        conn = psycopg2.connect(db_url)
        cursor = conn.cursor(cursor_factory=RealDictCursor)
        
        print("\n🔍 Detailed Match Counts Investigation\n")

        # 1. Total by Sport
        print("--- Total by Sport ---")
        cursor.execute("SELECT sport, COUNT(*) as count FROM matches GROUP BY sport")
        for row in cursor.fetchall():
            print(f"{row['sport']}: {row['count']:,}")

        # 2. Football Matches by State
        print("\n--- Football Matches by State ---")
        cursor.execute("SELECT state, COUNT(*) as count FROM matches WHERE sport='football' GROUP BY state ORDER BY count DESC")
        for row in cursor.fetchall():
            state_display = row['state'] if row['state'] else "NULL"
            print(f"{state_display}: {row['count']:,}")
            
        # 3. Total Football Matches (All States)
        cursor.execute("SELECT COUNT(*) as count FROM matches WHERE sport='football'")
        total_football = cursor.fetchone()['count']
        print(f"\nTOTAL FOOTBALL MATCHES (All States): {total_football:,}")
        
        # 4. Check for 'postGame' equivalent states
        # Sometimes 'FT', 'Ended', 'finished' might be used
        
        conn.close()
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    inspect_counts()
