"""Dated sample of StepStone.de search result pages (logged out).

Fetches the first result page (25 listings) for each occupation/city pair,
reads the embedded window.__PRELOADED_STATE__["app-unifiedResultlist"] JSON
and counts the per-listing labels (QUICK_APPLY = "Schnelle Bewerbung",
NO_COVER_LETTER = "Anschreiben nicht erforderlich").
Run: python3 label-sample.py > label-sample.json
"""
import json, re, sys, time, urllib.request, datetime

SEARCHES = [
    ("industriekaufmann", "koeln"), ("buchhalter", "hamburg"),
    ("pflegefachkraft", "berlin"), ("elektriker", "muenchen"),
    ("softwareentwickler", "berlin"), ("vertriebsmitarbeiter", "frankfurt-am-main"),
    ("sachbearbeiter", "stuttgart"), ("lagerlogistik", "dortmund"),
    ("projektmanager", "duesseldorf"), ("personalreferent", "leipzig"),
    ("kfz-mechatroniker", "hannover"), ("marketing-manager", "muenchen"),
]
UA = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130 Safari/537.36"

def fetch(url):
    req = urllib.request.Request(url, headers={"User-Agent": UA, "Accept-Language": "de-DE,de;q=0.9"})
    with urllib.request.urlopen(req, timeout=40) as r:
        return r.read().decode("utf-8")

def state(html):
    m = re.search(r'window\.__PRELOADED_STATE__\["app-unifiedResultlist"\]\s*=\s*', html)
    return json.JSONDecoder().raw_decode(html[m.end():])[0]

out = {"fetched": datetime.datetime.now(datetime.timezone.utc).isoformat(), "searches": []}
for job, city in SEARCHES:
    url = f"https://www.stepstone.de/jobs/{job}/in-{city}"
    try:
        o = state(fetch(url))
    except Exception as e:
        out["searches"].append({"url": url, "error": str(e)}); continue
    items = o["searchResults"]["items"]
    types = [[l.get("type") for l in it.get("labels", [])] for it in items]
    out["searches"].append({
        "url": url,
        "listings": len(items),
        "quickApply": sum("QUICK_APPLY" in t for t in types),
        "noCoverLetter": sum("NO_COVER_LETTER" in t for t in types),
        "both": sum("QUICK_APPLY" in t and "NO_COVER_LETTER" in t for t in types),
        "anonymous": sum(bool(it.get("isAnonymous")) for it in items),
        "cvToJobScoreNonNull": sum(it.get("cvToJobScore") is not None for it in items),
        "labelTypes": sorted({x for t in types for x in t}),
    })
    time.sleep(3)
tot = [s for s in out["searches"] if "listings" in s]
out["total"] = {k: sum(s[k] for s in tot) for k in ("listings", "quickApply", "noCoverLetter", "both", "anonymous")}
json.dump(out, sys.stdout, ensure_ascii=False, indent=1)
