"""Parse Hudl team <li> elements (shared by exchange + search)."""
from playwright.async_api import Locator


async def parse_team_li(item: Locator, index: int) -> dict | None:
    block = item.locator(".ex-team-block").first
    if await block.count() == 0:
        return None

    school = item.locator(".team-info h5.uni-headline--3").first
    team = item.locator(".team-info p.uni-text").first
    photo = item.locator("figure.ex-team-photo").first

    school_name = (await school.inner_text()).strip() if await school.count() else ""
    team_name = (await team.inner_text()).strip() if await team.count() else ""

    if not school_name and not team_name:
        return None

    data_id = await block.get_attribute("data-id")
    title = await block.get_attribute("title")

    photo_url = None
    if await photo.count():
        style = await photo.get_attribute("style") or ""
        if "url(" in style:
            photo_url = style.split("url(")[1].split(")")[0].strip("'\"")

    return {
        "id": data_id or str(index),
        "school_name": school_name,
        "team_name": team_name,
        "title": (title or "").strip() or None,
        "photo_url": photo_url,
    }
