File size: 1,928 Bytes
b76f199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Public template catalog — section codes and labels per RICS product tier."""

from fastapi import APIRouter, Query, Request

from app.models.schemas import TemplateCatalogResponse, TemplateCatalogSection
from app.templates.registry import get_survey_pack, resolve_survey_level

router = APIRouter()


def _hint_for_template(expected_fields: list[str]) -> str:
    if not expected_fields:
        return ""
    return " · ".join(f.replace("_", " ") for f in expected_fields[:12])


@router.get("/templates/catalog", response_model=TemplateCatalogResponse)
async def templates_catalog(
    request: Request,
    survey_level: int | None = Query(
        default=None,
        ge=1,
        le=3,
        description="1 = Condition Report, 2 = HomeBuyer / Level 2, 3 = Building Survey. Omit → 3.",
    ),
) -> TemplateCatalogResponse:
    """Return ordered sections, group headings, and export title for the given tier.

    The UI should call this after the user chooses a survey level so section cards
    match the selected RICS product (codes differ between tiers, e.g. Level 1 uses
    a single ``E`` for all external elements; Level 3 uses ``E1``–``E9``).
    """
    _ = request.state.tenant_id  # enforce middleware
    lvl = resolve_survey_level(survey_level)
    pack = get_survey_pack(lvl)
    sections: list[TemplateCatalogSection] = []
    for code in pack.section_order:
        tmpl = pack.get(code)
        if tmpl is None:
            continue
        sections.append(
            TemplateCatalogSection(
                code=tmpl.code,
                title=tmpl.title,
                group=tmpl.group,
                hint=_hint_for_template(tmpl.expected_fields),
            )
        )
    return TemplateCatalogResponse(
        survey_level=lvl,
        product_name=pack.product_label,
        docx_title=pack.docx_title,
        sections=sections,
        group_labels=pack.group_labels,
    )