import requests
from bs4 import BeautifulSoup

from services.standings_service import sync_standings

URL_STANDINGS = "https://wfl.rfs.ru/tournament/1061879/tables"
SEASON = "2025/2026"


def fetch_html(url: str) -> str:
    headers = {"User-Agent": "Mozilla/5.0", "Accept": "*/*"}
    r = requests.get(url, headers=headers, timeout=20)
    r.raise_for_status()
    return r.text


def parse_standings(html: str) -> list[dict]:
    soup = BeautifulSoup(html, "html.parser")
    standings: list[dict] = []

    table_div = soup.find("ul", class_="custom-table__body")
    if not table_div:
        return standings

    rows = table_div.find_all("li", class_="custom-table__line")

    for row in rows:
        rank = row.find("div", class_="custom-table__number-wrapper").get_text(strip=True)

        team_name = row.find("div", class_="custom-table__team-name").get_text(strip=True)

        team_external_id = (
            row.find("a", class_="custom-table__team custom-table__cell")
            .get("href")
            .split("?team_id=")[-1]
        )

        vars_td = row.find_all("div", class_="custom-table__content")[1:]

        games = int(vars_td[0].get_text(strip=True))
        wins = int(vars_td[1].get_text(strip=True))
        draws = int(vars_td[2].get_text(strip=True))
        losses = int(vars_td[3].get_text(strip=True))

        plus_minus = vars_td[4].get_text(strip=True)
        gf = int(plus_minus.split("-")[0].strip())
        ga = int(plus_minus.split("-")[1].strip())

        points = int(vars_td[5].get_text(strip=True))

        standings.append(
            {
                "team_external_id": str(team_external_id),
                "played": games,
                "wins": wins,
                "losses": losses,
                "draws": draws,
                "points_for": gf,
                "points_against": ga,
                "points": points,
                "position": int(rank),
            }
        )

    return standings


def run_parser_standings() -> None:
    html = fetch_html(URL_STANDINGS)
    standings_rows = parse_standings(html)

    sync_standings(
        season=SEASON,
        standings_rows=standings_rows,
    )

    print(f"[parser_standings] Synced rows: {len(standings_rows)}")


if __name__ == "__main__":
    run_parser_standings()