"""why are service-request photos missing?

Generated from src/content/docs/reference/explore.md.
Run with the documented Fieldwork release in a writable directory.
Exports are saved there using the filenames shown in the walkthrough.
"""

import pandas as pd
import fieldwork as fw

requests = pd.DataFrame(
    [
        ("app", "pothole", "north", "closed", "photo", None),
        ("app", "pothole", "north", "closed", "photo", None),
        ("app", "pothole", "south", "open", "photo", None),
        ("app", "lighting", "south", "open", None, None),
        ("phone", "pothole", "north", "closed", None, "callback"),
        ("phone", "pothole", "south", "open", None, "callback"),
        ("phone", "lighting", "north", "closed", None, "callback"),
        ("phone", "lighting", "south", "open", None, "callback"),
        ("web", "pothole", "north", "closed", "photo", None),
        ("web", "lighting", "north", "closed", None, None),
        ("web", "lighting", "south", "open", None, None),
        ("web", "lighting", "south", "open", None, None),
    ],
    columns=[
        "channel",
        "service",
        "district",
        "status",
        "attachment",
        "callback",
    ],
)
requests.insert(0, "request_id", range(1001, 1013))

features = [
    "channel",
    "service",
    "district",
    "status",
    "attachment",
    "callback",
]
overview = fw.explore(
    requests,
    features=features,
    options={
        "paths": {"objective": "availability", "max_dimensions": 2}
    },
)
availability = overview.section("missingness").to_frame("availability")
print(
    availability[["feature", "populated", "denominator"]].to_string(
        index=False
    )
)

for finding in overview.findings[:4]:
    print(
        finding["id"],
        finding["statement"],
        "|",
        finding["lead"]["reason"],
    )

print(overview.best.dimensions)
print(fw.render_plaintext(overview.best.census(requests)))

figure = fw.render_svg(overview.best.census(requests))
report = fw.render_html(overview, max_findings=12)

from pathlib import Path

Path("explore.svg").write_text(figure, encoding="utf-8")
Path("explore.html").write_text(report, encoding="utf-8")
