#!/usr/bin/env python3
"""Number, citation, disagreement, length and form checks on a raw file: python check_essay.py raw/<tool>.md"""
import re, sys
text = open(sys.argv[1], encoding="utf-8").read()
ans = text.split("\n## Answer", 1)[1] if "\n## Answer" in text else text
a = ans.replace(" ", " ").replace("–", "-").replace("—", "-").strip()
low = a.lower()
allowed = {"6,400","6400","14","71","9","08:00","10:00","8","38","11","2025","1.2","210","96","58","31","7","15","6","2023","22","41","07:00","11:00","44","19","34","61","5,100","5100","2026","1","2","3","4,500","4500","20","57","500","600","4","5","10","1,300","1300","25","1.2m"}
nums = re.findall(r"\b\d[\d,]*(?:\.\d+)?\b", re.sub(r"\b\d{1,2}:\d{2}\b", " ", a))  # clock times checked as whole tokens, not halves
imported_nums = sorted(set(n for n in nums if n not in allowed))
places = ["pontevedra","ghent","copenhagen","amsterdam","oslo","madrid","barcelona","london","paris","new york","bogot","curitiba","freiburg","strasbourg","lisbon","porto","world health","european","eu ","study by","studies show","research shows","according to research"]
imported_names = [p.strip() for p in places if p in low] + (["WHO"] if re.search(r"\bWHO\b", a) else [])
cites = {k: len(re.findall(r"\[" + k + r"\]", a)) for k in ("1","2","3")}
disagree = bool(re.search(r"(6,?400).{0,200}(5,?100)|(5,?100).{0,200}(6,?400)", a, re.S)) and bool(re.search(r"(differ|disagree|not match|conflict|discrepan|lower|higher|than the council|than the university|rather than|instead of|puts it at|counts?)", low))
lines = [l for l in a.split("\n") if l.strip()]
title = lines[0].strip("# *").strip() if lines else ""
body = "\n".join(lines[1:]) if len(lines) > 1 else a
words = len(re.findall(r"\S+", body))
bullets = bool(re.search(r"^\s*([-*•]|\d+[.)])\s", body, re.M))
headings = bool(re.search(r"^\s*#{1,6}\s|^\s*\*\*[^*]{3,60}\*\*\s*$", body, re.M))
announce = bool(re.search(r"(in this essay|this essay (will|argues|examines|explores)|i will (argue|discuss|examine|show)|the following essay)", low))
counter = bool(re.search(r"(strongest (case|argument|objection)|opponents|those who oppose|critics|it is true that|admittedly|granted|the case against|traders argue|one might object|the objection)", low))
print(f"title: {title[:70]!r}")
print(f"words (below title): {words}  ->  {'in window' if 500 <= words <= 600 else ('SHORT' if words < 500 else 'LONG')}")
print(f"citations [1]/[2]/[3]: {cites['1']}/{cites['2']}/{cites['3']}  ->  {'all three' if all(cites.values()) else 'MISSING ' + ','.join(k for k,v in cites.items() if not v)}")
print(f"disagreement 6,400 vs 5,100 named: {disagree}")
print(f"counterargument signalled: {counter}")
print(f"imported numbers: {imported_nums}")
print(f"imported names/phrases: {imported_names}")
print(f"bullets: {bullets} | headings: {headings} | announces itself: {announce}")
