"""
Navigate directly to a sport-iframe event detail page to capture ALL markets.

Known event IDs from a previous successful run:
  Liverpool vs PSG: 16100045
  Bayern vs Real Madrid: 16301733
  Arsenal vs Sporting: 16219628

The event detail page should call GetAllMarketsByEventId or similar,
which includes BTTS and other markets not in the listing profile.

Usage:
    python3 tools/discover_surebet247_event_detail.py
"""
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

import time, collections
from scrapers.surebet247 import _Surebet247Worker, _decode_frame

IFRAME_BASE = ('https://sport-iframe.serhjs.xyz'
               '?language=en&currency=ngn&seq=last'
               '&brand=CL38B1&X-Api-Key=b8b92942-ce6a-44e8-a5d5-6bf407e316a2')

# Event detail URL candidates to try
EVENT_IDS = ['16100045', '16301733', '16219628']

KNOWN_TYPES = {1: 'Home/Away', 2: '1X2', 4: 'Asian Handicap', 5: 'Over/Under'}


def probe_event_page(page) -> dict:
    frames_recv = []
    frames_sent = []
    last_recv   = [time.time()]

    def handle_ws(ws):
        if 'direct-feed' not in ws.url:
            return
        print(f'  WS: {ws.url[:80]}')
        ws.on('framereceived', lambda p: (frames_recv.append(p), last_recv.__setitem__(0, time.time())) if isinstance(p, bytes) else None)
        ws.on('framesent',     lambda p: frames_sent.append(p) if isinstance(p, bytes) else None)

    page.on('websocket', handle_ws)

    # Try event URL formats
    event_url_formats = [
        f'https://sport-iframe.serhjs.xyz/football/event/{EVENT_IDS[0]}?language=en&currency=ngn&seq=last&brand=CL38B1&X-Api-Key=b8b92942-ce6a-44e8-a5d5-6bf407e316a2',
        f'https://sport-iframe.serhjs.xyz/event/{EVENT_IDS[0]}?language=en&currency=ngn&seq=last&brand=CL38B1&X-Api-Key=b8b92942-ce6a-44e8-a5d5-6bf407e316a2',
        f'https://sport-iframe.serhjs.xyz/sport/football/event/{EVENT_IDS[0]}?language=en&currency=ngn&seq=last&brand=CL38B1&X-Api-Key=b8b92942-ce6a-44e8-a5d5-6bf407e316a2',
    ]

    for url in event_url_formats:
        print(f'\n  Trying: {url[:80]}...')
        page.goto(url, wait_until='domcontentloaded', timeout=30_000)
        time.sleep(6)
        deadline = time.time() + 10
        while time.time() < deadline:
            time.sleep(1)
            if time.time() - last_recv[0] > 2.0 and frames_recv:
                break

        print(f'  Current URL: {page.url}')
        print(f'  Frames so far: {len(frames_recv)} recv / {len(frames_sent)} sent')

        # Check what methods were sent
        method_by_id = {}
        for frame in frames_sent:
            if isinstance(frame, bytes):
                d = _decode_frame(frame)
                if d and isinstance(d, (list, tuple)) and len(d) >= 4 and d[0] == 4:
                    method_by_id[str(d[2])] = str(d[3])
        unique_methods = list(set(method_by_id.values()))
        print(f'  Methods sent: {unique_methods}')

        # If we got useful methods (not just init), break
        has_market_data = any(m for m in unique_methods
                              if 'Market' in m or 'Event' in m or 'Rich' in m)
        if has_market_data and len(frames_recv) > 5:
            print(f'  Got market-related methods! Waiting for more data...')
            time.sleep(8)
            break

    # Final wait
    deadline = time.time() + 15
    while time.time() < deadline:
        time.sleep(1)
        if time.time() - last_recv[0] > 4.0 and frames_recv:
            break

    print(f'\n  Final: {len(frames_recv)} recv / {len(frames_sent)} sent')

    # Decode all methods sent
    method_by_id = {}
    for frame in frames_sent:
        if isinstance(frame, bytes):
            d = _decode_frame(frame)
            if d and isinstance(d, (list, tuple)) and len(d) >= 4 and d[0] == 4:
                method_by_id[str(d[2])] = str(d[3])

    print('\n  All methods sent:')
    seen = {}
    for rid, m in method_by_id.items():
        if m not in seen:
            # find the args for this method
            for frame in frames_sent:
                if isinstance(frame, bytes):
                    d = _decode_frame(frame)
                    if d and isinstance(d, (list, tuple)) and str(d[2]) == rid and len(d) >= 5:
                        seen[m] = str(d[4])[:200]
                        break
            if m not in seen:
                seen[m] = ''
    for m, args in sorted(seen.items()):
        print(f'    {m}')
        if args:
            print(f'      args: {args}')

    # Parse all market types from replies
    market_types_seen = collections.defaultdict(list)
    all_rids = set(method_by_id.keys())
    for frame in frames_recv:
        if not isinstance(frame, bytes):
            continue
        d = _decode_frame(frame)
        if not d or not isinstance(d, (list, tuple)) or d[0] != 2 or len(d) < 4:
            continue
        if str(d[2]) not in all_rids:
            continue
        reply = d[3]
        if not isinstance(reply, (list, tuple)) or len(reply) < 2 or not reply[0]:
            continue
        for item in (reply[1] or []):
            if not isinstance(item, (list, tuple)) or len(item) < 3:
                continue
            item_key = item[1]
            if not isinstance(item_key, (list, tuple)) or len(item_key) < 3:
                continue
            mtype  = item_key[2]
            period = item_key[3] if len(item_key) > 3 else None
            mval   = item[2]
            if not isinstance(mval, (list, tuple)) or not mval:
                continue
            for group in (mval[0] or []):
                if not isinstance(group, (list, tuple)) or len(group) < 2:
                    continue
                spec = group[0]
                ocs  = []
                for oc in (group[1] or []):
                    if isinstance(oc, (list, tuple)) and len(oc) >= 2:
                        oc_type = oc[0][0] if isinstance(oc[0], (list, tuple)) and oc[0] else '?'
                        price   = round(oc[1] / 100, 2) if isinstance(oc[1], (int, float)) else oc[1]
                        ocs.append((oc_type, price))
                if ocs:
                    market_types_seen[(mtype, period, str(spec))].append(ocs)

    return dict(market_types_seen)


if __name__ == '__main__':
    print('Navigating to sport-iframe event detail page...\n')
    worker = _Surebet247Worker()
    mts = worker.call(probe_event_page, timeout=180)

    if not mts:
        print('\nNo market data returned.')
    else:
        print(f'\n\nAll market types from event page ({len(mts)} unique):\n')
        known_ids = set(KNOWN_TYPES.keys())
        for (mtype, period, spec), samples in sorted(mts.items()):
            s = samples[0]
            oc_ids = [o[0] for o in s]
            prices = [o[1] for o in s]
            known  = KNOWN_TYPES.get(mtype, f'*** NEW ({mtype})')
            flag   = '  ← NEW!' if mtype not in known_ids else ''
            print(f'  type={mtype:<5} period={period}  spec={spec:<15} {known:<22} oc_ids={oc_ids}  prices={prices}{flag}')
