#!/usr/bin/env python3
"""
Compare Grok vs Our predictions
"""
import sqlite3
from datetime import datetime

DB_PATH = "/home/h3r7/turf_scraper/turf.db"

def add_grok_prediction(date, race_name, horse_name, odds, rank):
    conn = sqlite3.connect(DB_PATH)
    c = conn.cursor()
    c.execute('''
        INSERT INTO grok_predictions (date, race_name, horse_name, odds, rank)
        VALUES (?, ?, ?, ?, ?)
    ''', (date, race_name, horse_name, odds, rank))
    conn.commit()
    conn.close()
    print(f"Added: {horse_name}")

def compare_predictions(date):
    conn = sqlite3.connect(DB_PATH)
    c = conn.cursor()
    
    print(f"\n{'='*50}")
    print(f"COMPARAISON - {date}")
    print(f"{'='*50}")
    
    # Our predictions
    print("\n[NOS PREDICTIONS]")
    c.execute("SELECT horse_name, odds, prediction_rank FROM predictions WHERE date = ? ORDER BY prediction_rank", (date,))
    our_preds = c.fetchall()
    for name, odds, rank in our_preds:
        print(f"  {rank}. {name} (cote: {odds})")
    
    # Grok predictions
    print("\n[PREDICTIONS GROK]")
    c.execute("SELECT horse_name, odds, rank FROM grok_predictions WHERE date = ? ORDER BY rank", (date,))
    grok_preds = c.fetchall()
    for name, odds, rank in grok_preds:
        print(f"  {rank}. {name} (cote: {odds})")
    
    # Results (if available)
    print("\n[RÉSULTATS]")
    c.execute("SELECT horse_name, position FROM results WHERE date = ? AND position <= 5", (date,))
    results = c.fetchall()
    for name, pos in results:
        print(f"  {pos}. {name}")
    
    # Comparison
    if our_preds and grok_preds and results:
        result_names = [r[0] for r in results]
        
        our_hits = sum(1 for p in our_preds if p[0] in result_names[:3])
        grok_hits = sum(1 for p in grok_preds if p[0] in result_names[:3])
        
        print(f"\n[SCORE]")
        print(f"  Nos picks dans les 3 premiers: {our_hits}/3")
        print(f"  Grok picks dans les 3 premiers: {grok_hits}/3")
    
    conn.close()

if __name__ == "__main__":
    import sys
    if len(sys.argv) > 1:
        compare_predictions(sys.argv[1])
    else:
        compare_predictions(datetime.now().strftime('%Y-%m-%d'))
