
import os
import psycopg2
from psycopg2.extras import RealDictCursor

def inspect_odds():
    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🔍 ODDS COVERAGE INSPECTION\n")

        # 1. Total Scored Football Matches
        cursor.execute("SELECT COUNT(*) as count FROM matches WHERE sport='football' AND score_home IS NOT NULL")
        total_matches = cursor.fetchone()['count']
        print(f"Total Scored Matches: {total_matches:,}")

        # 2. Matches with Link to Odds Category
        cursor.execute("""
            SELECT COUNT(DISTINCT m.id) as count 
            FROM matches m
            JOIN odd_categories oc ON m.id = oc.match_id
            WHERE m.sport='football' AND m.score_home IS NOT NULL
        """)
        odds_linked_count = cursor.fetchone()['count']
        print(f"Matches with ANY Odds Linked: {odds_linked_count:,} ({odds_linked_count/total_matches*100:.1f}%)")

        # 3. Matches with Actual Odds Values
        # Check if selections exist and have values
        cursor.execute("""
            SELECT COUNT(DISTINCT m.id) as count 
            FROM matches m
            JOIN odd_categories oc ON m.id = oc.match_id
            JOIN odd_selections os ON oc.db_id = os.odd_category_db_id
            WHERE m.sport='football' 
              AND m.score_home IS NOT NULL
              AND os.odd_value IS NOT NULL
        """)
        odds_values_count = cursor.fetchone()['count']
        print(f"Matches with VALID Odds Values: {odds_values_count:,} ({odds_values_count/total_matches*100:.1f}%)")
        
        # 4. Investigate Discrepancy (Golden vs Odds Only)
        print(f"\n💡 Insight: The Golden Dataset was ~28k. This Odds-Only check will show if the bottleneck is Odds (approx {odds_values_count}) or Stats.")

        conn.close()
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    inspect_odds()
