import re

def parse_signal(text):
    text = text.upper()

    if "GOLD" not in text:
        return None

    # Detect BUY or SELL
    if "BUY" in text:
        trade_type = "BUY"
    elif "SELL" in text:
        trade_type = "SELL"
    else:
        return None

    # Extract entry range
    entry_match = re.search(r'(\d+\.?\d*)\s*[-@]\s*(\d+\.?\d*)', text)
    if not entry_match:
        return None

    entry1 = float(entry_match.group(1))
    entry2 = float(entry_match.group(2))

    entry_price = (entry1 + entry2) / 2  # average

    # Extract SL
    sl_match = re.search(r'SL\s*[: ]\s*(\d+\.?\d*)', text)
    if not sl_match:
        return None
    sl = float(sl_match.group(1))

    # Extract TP (first TP only)
    tp_match = re.search(r'TP\s*[: ]\s*(\d+\.?\d*)', text)
    if not tp_match:
        return None
    tp = float(tp_match.group(1))

    return {
        "symbol": "XAUUSD.m",
        "type": trade_type,
        "entry": entry_price,
        "sl": sl,
        "tp": tp
    }