File size: 2,415 Bytes
5961e41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Insureon business-class taxonomy.

Loaded verbatim from the first-party endpoint
`https://content.apps.insureon.com/webcategoriesbybrandcode/ION/2`
(HTTP 200, 76,672 bytes, captured 2026-08-23) and stored beside this module as
`professions.json`. The clone reads it from disk; it makes no network request.
"""

from __future__ import annotations

import json
from functools import lru_cache
from pathlib import Path

_PATH = Path(__file__).resolve().parent / "professions.json"


@lru_cache(maxsize=1)
def _doc() -> dict:
    return json.loads(_PATH.read_text(encoding="utf-8"))


@lru_cache(maxsize=1)
def industries() -> list[dict]:
    """[{id, name, services:[{id, name}]}] sorted by display name."""
    out = []
    for ind in _doc().get("IndustriesServices", []):
        out.append({
            "id": int(ind["Id"]),
            "name": ind["Description"],
            "services": sorted(
                ({"id": int(s["Id"]), "name": s["Description"]}
                 for s in ind.get("Services", [])),
                key=lambda s: s["name"],
            ),
        })
    out.sort(key=lambda i: i["name"])
    return out


@lru_cache(maxsize=1)
def all_services() -> list[dict]:
    """Flat [{id, name, industry_id, industry_name}] sorted by service name."""
    rows = [
        {"id": s["id"], "name": s["name"],
         "industry_id": ind["id"], "industry_name": ind["name"]}
        for ind in industries() for s in ind["services"]
    ]
    rows.sort(key=lambda r: r["name"])
    return rows


@lru_cache(maxsize=1)
def _by_id() -> dict[int, dict]:
    return {r["id"]: r for r in all_services()}


def service(service_id: int | str | None) -> dict | None:
    try:
        return _by_id().get(int(service_id))  # type: ignore[arg-type]
    except (TypeError, ValueError):
        return None


def search(term: str, limit: int = 60) -> list[dict]:
    """Case-insensitive contains match, prefix matches ranked first."""
    q = (term or "").strip().lower()
    rows = all_services()
    if not q:
        return rows[:limit]
    starts = [r for r in rows if r["name"].lower().startswith(q)]
    contains = [r for r in rows
                if q in r["name"].lower() and not r["name"].lower().startswith(q)]
    return (starts + contains)[:limit]


def brand_code() -> str:
    return _doc().get("BrandCode", "ION")


__all__ = ["industries", "all_services", "service", "search", "brand_code"]