"""
OddsJam Scraper with Auto-2FA
Scrapes arbitrage and positive EV data from OddsJam.
Uses local Maildir to automatically retrieve 2FA codes via subprocess.
"""

import os
import re
import time
import glob
import json
import subprocess
import psycopg2
from datetime import datetime, timezone
from playwright.sync_api import sync_playwright

# Configuration
ODDSJAM_EMAIL = 'admin@cashhive.ai'
MAILDIR_PATH = '/home/admin/Maildir'
DB_URL_FILE = '/var/www/html/eventheodds/.env'

def load_db_url():
    """Load database URL from .env file"""
    try:
        with open(DB_URL_FILE) as f:
            for line in f:
                if line.startswith('SPORTS_DATABASE_URL='):
                    url = line.strip().split('=', 1)[1]
                    return url.strip('"').split('?')[0]
    except Exception as e:
        print(f'Error loading DB URL: {e}')
    return None

def get_oddsjam_code_from_content(content):
    """Extract 6-digit code from email content"""
    if 'oddsjam.com' not in content.lower():
        return None
    if 'verification code' not in content.lower():
        return None
    
    lines = content.split('\n')
    for line in lines:
        line = line.strip()
        if re.match(r'^\d{6}$', line):
            return line
    return None

def wait_for_new_code(min_timestamp, timeout=180):
    """
    Wait for a new 2FA code to arrive AFTER min_timestamp.
    Uses sudo to read emails.
    """
    print(f'  Waiting for 2FA code (email must be newer than {min_timestamp})...')
    start_time = time.time()
    
    while time.time() - start_time < timeout:
        time.sleep(5)
        
        # Find recent OddsJam emails using sudo
        try:
            # Get list of recent files
            cmd = f"sudo find {MAILDIR_PATH}/new {MAILDIR_PATH}/cur -type f -mmin -5 2>/dev/null"
            result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
            files = [f.strip() for f in result.stdout.strip().split('\n') if f.strip()]
            
            for filepath in files:
                # Get mtime
                stat_result = subprocess.run(f'stat -c %Y "{filepath}"', shell=True, capture_output=True, text=True)
                try:
                    mtime = float(stat_result.stdout.strip())
                except:
                    continue
                    
                if mtime < min_timestamp:
                    continue
                
                # Read file content with sudo
                cat_result = subprocess.run(f'sudo cat "{filepath}"', shell=True, capture_output=True, text=True)
                content = cat_result.stdout
                
                code = get_oddsjam_code_from_content(content)
                if code:
                    print(f'  Found NEW 2FA code: {code}')
                    return code
                    
        except Exception as e:
            print(f'  Error checking emails: {e}')
        
        elapsed = int(time.time() - start_time)
        print(f'  Still waiting... ({elapsed}s)')
        
    return None

def scrape_oddsjam(conn):
    """Main scraping function"""
    total_records = 0
    
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        context = browser.new_context(
            viewport={'width': 1920, 'height': 1080},
            user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
        )
        page = context.new_page()
        
        # Step 1: Navigate to login
        print('Step 1: Navigating to OddsJam login...')
        page.goto('https://oddsjam.com/login', wait_until='networkidle')
        time.sleep(2)
        
        # Step 2: Enter email
        print('Step 2: Entering email...')
        page.fill('#email-address', ODDSJAM_EMAIL)
        time.sleep(1)
        
        # Record the timestamp BEFORE clicking sign in
        login_trigger_time = time.time()
        
        # Step 3: Click Sign In
        print('Step 3: Clicking Sign In...')
        page.click('button.bg-brand-blue')
        time.sleep(3)
        
        # Step 4: Wait for 2FA screen
        print('Step 4: Waiting for 2FA screen...')
        try:
            page.wait_for_selector('input[data-input-otp="true"]', timeout=10000)
        except:
            print('  2FA input not found')
        
        # Get the 2FA code from local mail
        print('Step 5: Retrieving 2FA code from local mail...')
        code = wait_for_new_code(min_timestamp=login_trigger_time, timeout=180)
        
        if not code:
            print('  ERROR: Could not retrieve 2FA code')
            browser.close()
            return 0
        
        print(f'Step 6: Entering 2FA code: {code}')
        otp_input = page.locator('input[data-input-otp="true"]')
        otp_input.fill(code)
        time.sleep(5)
        
        # Wait for redirect
        print('Step 7: Waiting for login to complete...')
        page.wait_for_load_state('networkidle')
        time.sleep(3)
        
        current_url = page.url
        print(f'  Current URL: {current_url}')
        
        # Step 8: Navigate to Arbitrage page
        print('Step 8: Navigating to Arbitrage page...')
        page.goto('https://oddsjam.com/betting-tools/arbitrage', wait_until='networkidle')
        time.sleep(5)
        
        # Check if logged in
        user_data = page.evaluate('window.__NEXT_DATA__?.props?.pageProps?.fallback?.["auth/user"]?.user')
        if user_data:
            print(f'  Logged in as: {user_data.get("email", "unknown")}')
        else:
            print('  WARNING: User appears not logged in')
        
        # Save debug files
        html = page.content()
        text = page.inner_text('body')
        with open('/tmp/oddsjam_debug_arb.html', 'w') as f:
            f.write(html)
        with open('/tmp/oddsjam_debug_arb.txt', 'w') as f:
            f.write(text)
        print(f'  Saved debug files (HTML: {len(html)}, Text: {len(text)})')
        
        # Step 9: Navigate to Positive EV page
        print('Step 9: Navigating to Positive EV page...')
        try:
            page.goto('https://oddsjam.com/betting-tools/positive-ev', wait_until='networkidle', timeout=30000)
            time.sleep(5)
            
            html = page.content()
            text = page.inner_text('body')
            with open('/tmp/oddsjam_debug_ev.html', 'w') as f:
                f.write(html)
            with open('/tmp/oddsjam_debug_ev.txt', 'w') as f:
                f.write(text)
            print(f'  Saved debug files (HTML: {len(html)}, Text: {len(text)})')
        except Exception as e:
            print(f'  Error navigating to Positive EV: {e}')
        
        browser.close()
    
    return total_records

def main():
    print('=' * 60)
    print('ODDSJAM SCRAPER WITH AUTO-2FA')
    print(f'Time: {datetime.now(timezone.utc).isoformat()}')
    print('=' * 60)
    
    db_url = load_db_url()
    if not db_url:
        print('ERROR: No database URL found')
        return
    
    try:
        conn = psycopg2.connect(db_url)
        total = scrape_oddsjam(conn)
        conn.close()
        print(f'\nTotal records scraped: {total}')
    except Exception as e:
        print(f'ERROR: {e}')
        import traceback
        traceback.print_exc()

if __name__ == '__main__':
    main()
