"""
Value Sniper Backtest (High Odds)
=================================
Sadece Oran > 1.50 ve Güven > %70 olan bahisleri oynar.
"""

import os
import sys
import json
import time
import psycopg2
from psycopg2.extras import RealDictCursor

AI_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(AI_DIR)
sys.path.insert(0, ROOT_DIR)
if "scripts" in os.path.basename(AI_DIR):
    ROOT_DIR = os.path.dirname(ROOT_DIR)

from services.single_match_orchestrator import get_single_match_orchestrator

def get_clean_dsn() -> str:
    return "postgresql://suggestbet:SuGGesT2026SecuRe@localhost:15432/boilerplate_db"

def run_value_sniper():
    print("💰 VALUE SNIPER BACKTEST (Odds > 1.50)")
    print("="*60)
    
    leagues_path = os.path.join(ROOT_DIR, "top_leagues.json")
    with open(leagues_path, 'r') as f:
        top_leagues = json.load(f)
    league_ids = tuple(str(lid) for lid in top_leagues)

    dsn = get_clean_dsn()
    conn = psycopg2.connect(dsn)
    cur = conn.cursor(cursor_factory=RealDictCursor)

    cur.execute("""
        SELECT m.id, m.match_name, m.home_team_id, m.away_team_id,
               m.score_home, m.score_away,
               t1.name as home_team, t2.name as away_team
        FROM matches m
        LEFT JOIN teams t1 ON m.home_team_id = t1.id
        LEFT JOIN teams t2 ON m.away_team_id = t2.id
        WHERE m.league_id IN %s
          AND m.status = 'FT'
          AND m.score_home IS NOT NULL
          AND EXISTS (SELECT 1 FROM odd_categories oc WHERE oc.match_id = m.id)
        ORDER BY m.mst_utc DESC
        LIMIT 500
    """, (league_ids,))
    
    rows = cur.fetchall()
    print(f"📊 {len(rows)} maç taranıyor...\n")

    try: orchestrator = get_single_match_orchestrator()
    except Exception as e:
        print(f"❌ AI Hatası: {e}")
        return

    total_bet = 0
    total_won = 0
    total_profit = 0.0

    for i, row in enumerate(rows):
        match_id = str(row['id'])
        home = row['home_team'] or "?"
        away = row['away_team'] or "?"
        h_score = row['score_home'] or 0
        a_score = row['score_away'] or 0

        try:
            pred = orchestrator.analyze_match(match_id)
            if not pred: continue

            candidates = []
            if pred.get("expert_recommendation"):
                rec = pred["expert_recommendation"]
                if rec.get("main_pick"): candidates.append(rec["main_pick"])
                if rec.get("value_picks"): candidates.extend(rec["value_picks"])
            elif pred.get("main_pick"):
                candidates.append(pred["main_pick"])

            best_bet = None
            for c in candidates:
                if not c: continue
                conf = c.get("confidence", 0) if isinstance(c, dict) else getattr(c, 'confidence', 0)
                odds = c.get("odds", 0) if isinstance(c, dict) else getattr(c, 'odds', 0)
                
                # VALUE CRITERIA: Odds > 1.50 AND Conf > 70%
                if conf >= 70.0 and odds >= 1.50:
                    # Check Edge
                    implied = 1.0 / odds
                    edge = ((conf/100) - implied) * 100
                    if edge > 0: # Must be positive value
                        if best_bet is None or (conf > (best_bet.get("confidence", 0) if isinstance(best_bet, dict) else getattr(best_bet, 'confidence', 0))):
                            best_bet = c

            if best_bet:
                pick = str(best_bet.get("pick") if isinstance(best_bet, dict) else getattr(best_bet, 'pick', "")).upper()
                conf = best_bet.get("confidence", 0) if isinstance(best_bet, dict) else getattr(best_bet, 'confidence', 0)
                odds = best_bet.get("odds", 0) if isinstance(best_bet, dict) else getattr(best_bet, 'odds', 0)
                
                won = False
                if pick in ["1", "MS 1"] and h_score > a_score: won = True
                elif pick in ["X", "MS X"] and h_score == a_score: won = True
                elif pick in ["2", "MS 2"] and a_score > h_score: won = True
                elif "ÜST" in pick or "OVER" in pick:
                    line = 2.5
                    if "1.5" in pick: line = 1.5
                    elif "3.5" in pick: line = 3.5
                    if (h_score + a_score) > line: won = True
                elif "ALT" in pick or "UNDER" in pick:
                    line = 2.5
                    if "1.5" in pick: line = 1.5
                    elif "3.5" in pick: line = 3.5
                    if (h_score + a_score) < line: won = True
                elif "VAR" in pick and h_score > 0 and a_score > 0: won = True
                elif "YOK" in pick and (h_score == 0 or a_score == 0): won = True

                total_bet += 1
                if won:
                    total_won += 1
                    profit = odds - 1.0
                    total_profit += profit
                    print(f"[{i+1}] ✅ {home} vs {away} | {pick} ({odds:.2f}) -> WON (+{profit:.2f})")
                else:
                    total_profit -= 1.0
                    print(f"[{i+1}] ❌ {home} vs {away} | {pick} ({odds:.2f}) -> LOST")

        except: pass

    print("\n" + "="*60)
    print("💰 VALUE SNIPER SONUÇLARI")
    print("="*60)
    print(f"Oynanan Bahis: {total_bet}")
    print(f"Kazanılan: {total_won}")
    
    if total_bet > 0:
        win_rate = (total_won / total_bet) * 100
        roi = (total_profit / total_bet) * 100
        print(f"Kazanma Oranı: %{win_rate:.2f}")
        print(f"Toplam Kâr: {total_profit:.2f} Units")
        if total_profit > 0: print("🟢 PARA KAZANDIK!")
        else: print("🔴 PARA KAYBETTİK!")
    else:
        print("⚠️ Yeterli VALUE bulunamadı.")

    cur.close()
    conn.close()

if __name__ == "__main__":
    run_value_sniper()
