"""an astronomy log, two presentation audiences

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

import json

import pandas as pd
import fieldwork as fw

# Each row is one observation entry, including repeat observations of a target.
log = pd.DataFrame(
    {
        "entry": range(1, 13),
        "target": ["Moon"] * 5
        + ["Jupiter"] * 3
        + ["Saturn"] * 2
        + [None] * 2,
    }
)
result = fw.levels(log, ["target"], max_levels=3, dropna=False)
archive_json = json.dumps(result.to_dict(), allow_nan=False)
saved = json.loads(archive_json)
record = saved["per_feature"][0]
print("Archive: total levels, reported levels, unreported rows")
print(
    record["levels_total"],
    record["levels_reported"],
    record["unreported_rows"],
)

full = fw.visualization_data(saved, detail="full")
feature = full["features"][0]
print(feature["scope"])
print("Target | Entries | Share of all entries")
for row in feature["rows"]:
    print(f"{row['label']} | {row['count']} | {row['share']:.1%}")

topology = fw.visualization_data(saved, detail="topology")
qualitative = topology["features"][0]
print("Targets for the planning board:")
for row in qualitative["rows"]:
    print(row["label"])

from pathlib import Path

presentation_json = json.dumps(topology, indent=2, allow_nan=False)
Path("observing-plan.json").write_text(
    presentation_json, encoding="utf-8"
)
print(fw.render_plaintext(saved))
