
from __future__ import annotations
from typing import Dict, Union
from bs4 import BeautifulSoup
from ..registry import extractor
from ..helpers.price import parse_price

@extractor
def ex_oe_list_price(soup: BeautifulSoup, url: str) -> Dict:
    """
    Extract Magento "oe-list-price" block:
      <div class="oe-list-price">
        <div class="rrp-price"><label>Regular Price: </label><span class="price">£30.50</span></div>
        <div class="oe-final-price"><label>Special Price: </label><span>£23.63</span></div>
      </div>
    Produces:
      oe_list_price: { rrp_raw, rrp, rrp_currency, special_raw, special, special_currency }
    Also promotes special_* to top-level (special_price_*) if available.
    """
    box = soup.select_one(".oe-list-price")
    if not box:
        return {}
    out: Dict[str, Union[str, float, dict]] = {}
    oe: Dict[str, Union[str, float]] = {}

    # RRP inside oe-list-price (if present)
    rrp = box.select_one(".rrp-price")
    if rrp:
        txt = (rrp.select_one("span.price") or rrp.select_one("span") or rrp).get_text(strip=True)
        val, cur, raw = parse_price(txt)
        if raw:
            oe["rrp_raw"] = raw
        if val is not None:
            oe["rrp"] = val
        if cur:
            oe["rrp_currency"] = cur

    # Special Price inside oe-list-price
    sp = box.select_one(".oe-final-price, .special-price, .final-price")
    if sp:
        txt = (sp.select_one("span.price") or sp.select_one("span") or sp).get_text(strip=True)
        val, cur, raw = parse_price(txt)
        if raw:
            oe["special_raw"] = raw
        if val is not None:
            oe["special"] = val
            out["special_price"] = val
        if cur:
            oe["special_currency"] = cur
            out["special_price_currency"] = cur
        if raw:
            out["special_price_raw"] = raw

    if oe:
        out["oe_list_price"] = oe
    return out
