"""Draw the category counts reported in Goel et al. (2024), Fig. 3.

Source: https://www.nature.com/articles/s41467-024-46670-5.pdf
Each category used 604 pairs of observations. The bars count categories,
not people, model accuracy or measurements from an Orbital study.
"""
from collections import Counter
import csv
from pathlib import Path

folder = Path(__file__).resolve().parent
stem = Path(__file__).stem
with (folder / f"{stem}.csv").open(newline="") as stream:
    rows = list(csv.DictReader(stream))
counts = Counter(row["outcome"] for row in rows)
labels = ("Combined fit higher", "No clear difference", "Combined fit lower")
assert len(rows) == len({row["category"] for row in rows}) == 13
assert {int(row["observation_pairs"]) for row in rows} == {604}
assert set(counts) == set(labels)

svg = [
    '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 760 250" role="img" aria-labelledby="cue-chart-title cue-chart-desc">',
    '<title id="cue-chart-title">How the combined model compared with the situation-only model</title>',
    '<desc id="cue-chart-desc">Across 13 emotion categories, the combined fit was higher in two, lower in six and did not differ significantly in five.</desc>',
    '<rect width="760" height="250" rx="14" fill="#fbf5f1"/>',
    '<g fill="#2d201b" font-family="Arial,sans-serif" font-size="20">',
    '<text x="24" y="39" font-weight="700">Face and situation versus situation alone</text>',
]
for index, label in enumerate(labels):
    top = 70 + index * 48
    width = 60 * counts[label]
    svg += [
        f'<text x="24" y="{top + 21}">{label}</text>',
        f'<rect x="252" y="{top}" width="{width}" height="28" rx="3" fill="#a44934"/>',
        f'<text x="{264 + width}" y="{top + 21}" font-weight="700">{counts[label]}</text>',
    ]
svg += [
    '<text x="24" y="226" font-size="17">Categories, Goel et al. (2024), Study 1, Figure 3</text>',
    '</g>',
    '</svg>',
]
(folder / f"{stem}.svg").write_text("\n".join(svg) + "\n", encoding="utf-8")
