"""q01_venue.py: register the numbers of the IJAMT author instructions in the Phase 3 values file.

Every number is parsed from a verbatim quote stored in inputs/venue_checklist_quotes.json (verified on the
publisher's page, see quote_check there); nothing is typed by hand. The checklist rules themselves live in
inputs/venue_rules.json and cite these value names.
"""
import json, re, sys

sys.path.insert(0, "/home/claude/mill/code")
from vals import Values

P3 = "/home/claude/mill/phase3"
V = Values(path=f"{P3}/outputs/values3.json", script="q01_venue.py")
C = json.load(open(f"{P3}/inputs/venue_checklist_quotes.json"))
Q = C["quotes"]
E = lambda qid: {"population": "external source", "filter": f"IJAMT submission guidelines, quote {qid}, retrieved {C['source']['retrieved_utc'][:10]}", "statistic": "value as printed"}
WORDS = {"two": 2, "three": 3, "four": 4, "five": 5, "six": 6}


def num(qid, pattern, group=1, cast=float):
    m = re.search(pattern, Q[qid])
    if not m:
        raise ValueError(f"{qid}: pattern not found")
    g = m.group(group)
    return cast(WORDS[g]) if g in WORDS else cast(g)


V.set("v_abstract_min_words", num("q_abstract", r"abstract of (\d+) to (\d+) words", 1, int), "words", E("q_abstract"), kind="external")
V.set("v_abstract_max_words", num("q_abstract", r"abstract of (\d+) to (\d+) words", 2, int), "words", E("q_abstract"), kind="external")
V.set("v_keywords_min", num("q_keywords", r"provide (\d+) to (\d+) keywords", 1, int), "keywords", E("q_keywords"), kind="external")
V.set("v_keywords_max", num("q_keywords", r"provide (\d+) to (\d+) keywords", 2, int), "keywords", E("q_keywords"), kind="external")
V.set("v_heading_levels_max", num("q_headings", r"no more than (\w+) levels", 1, int), "levels", E("q_headings"), kind="external")
V.set("v_fig_width_col_mm", num("q_width", r"should be (\d+) mm \(for double-column", 1, float), "mm", E("q_width"), kind="external")
V.set("v_fig_width_page_mm", num("q_width", r"or (\d+) mm \(for single-column", 1, float), "mm", E("q_width"), kind="external")
V.set("v_fig_height_max_mm", num("q_width", r"not higher than (\d+) mm", 1, float), "mm", E("q_width"), kind="external")
V.set("v_line_min_mm", num("q_line", r"at least ([\d.]+) mm", 1, float), "mm", E("q_line"), kind="external")
V.set("v_font_min_pt", num("q_fontsize", r"\((\d+)-(\d+) pt\)", 1, int), "pt", E("q_fontsize"), kind="external")
V.set("v_font_max_pt", num("q_fontsize", r"\((\d+)-(\d+) pt\)", 2, int), "pt", E("q_fontsize"), kind="external")
V.set("v_dpi_combination", num("q_combo", r"resolution of (\d+) dpi", 1, int), "dpi", E("q_combo"), kind="external")
V.set("v_dpi_halftone", num("q_halftone", r"resolution of (\d+) dpi", 1, int), "dpi", E("q_halftone"), kind="external")
V.set("v_dpi_lineart", num("q_lineart", r"resolution of (\d+) dpi", 1, int), "dpi", E("q_lineart"), kind="external")
V.set("v_contrast_min", num("q_contrast", r"at least ([\d.]+):1", 1, float), "ratio", E("q_contrast"), kind="external")
V.set("v_ijamt_ref_share_max", num("q_ijamt15", r"more than (\d+) %", 1, float) / 100, "fraction", E("q_ijamt15"), kind="external")
V.set("v_rgb_bits", num("q_rgb", r"RGB \((\d+) bits", 1, int), "bits", E("q_rgb"), kind="external")
V.set("v_ci_window_years", num("q_ci_window", r"last (\d+) years", 1, int), "years", E("q_ci_window"), kind="external")
V.set("v_referees", num("q_reviewers", r"reviewed by (\w+) referees", 1, int), "referees", E("q_reviewers"), kind="external")
V.set("v_submission_questions", num("q_questions", r"answer the below (\w+) questions", 1, int), "questions", E("q_questions"), kind="external")
V.set("v_orcid_digits", num("q_orcid", r"the (\d+)-digit ORCID", 1, int), "digits", E("q_orcid"), kind="external")
t = C["other_sources"]["S_sn_latex"]["quotes"]
m = re.search(r"\((\w+ \d{4}) version\)", t["q_pkg"])
V.set("v_template_version", m.group(1), "text", {"population": "external source", "filter": "Springer Nature LaTeX author support page, retrieved 2026-09-22", "statistic": "template package version as printed"}, kind="external")
m = re.search(r"ZIP, ([\d.]+) KB", t["q_zip"])
V.set("v_template_zip_kb", float(m.group(1)), "KB", {"population": "external source", "filter": "Springer Nature LaTeX author support page, retrieved 2026-09-22", "statistic": "package size as printed"}, kind="external")
V.set("v_small_fig_width_mm", num("q_small_width", r"should be (\d+) mm wide", 1, float), "mm", E("q_small_width"), kind="external")
V.set("v_small_fig_height_mm", num("q_small_width", r"not higher than (\d+) mm", 1, float), "mm", E("q_small_width"), kind="external")
QC1, QC2 = C["quote_check"], C["quote_check_round2"]
V.set("v_quotes_checked", QC1["n_checked"] + QC2["n_checked"], "quotes", {"population": "inputs/venue_checklist_quotes.json", "filter": "quote_check and quote_check_round2", "statistic": "quotes found verbatim on the page"})
V.set("v_quotes_checked_round2", QC2["n_checked"], "quotes", {"population": "inputs/venue_checklist_quotes.json", "filter": "quote_check_round2", "statistic": "quotes added after the review round and found verbatim on the page"})
V.set("v_quotes_failed", len(QC1["failed"]) + len(QC2["failed"]), "quotes", {"population": "inputs/venue_checklist_quotes.json", "filter": "quote_check and quote_check_round2", "statistic": "quotes not found"})
V.set("v_round2_date", QC2["checked_utc"][:10], "date", {"population": "inputs/venue_checklist_quotes.json", "filter": "quote_check_round2", "statistic": "date of the second reading (UTC)"}, kind="text")
V.set("v_page_headings", sum(1 for h in C["page_headings"]["headings"] if h["chars"] > 0 and h["heading"] != "Contents"), "headings", {"population": "IJAMT submission guidelines", "filter": "page_headings", "statistic": "headings with text under them"})
O = C["other_sources"]
V.set("v_u1_url", O["S_ijamt_u1"]["url"], "text", {"population": "IJAMT article page of U1", "filter": "retrieval record", "statistic": "URL read"}, kind="text")
V.set("v_sn_latex_url", O["S_sn_latex"]["url"], "text", {"population": "Springer Nature LaTeX author support page", "filter": "retrieval record", "statistic": "URL read"}, kind="text")
V.set("v_other_retrieved", O["S_sn_latex"]["retrieved_utc"], "date", {"population": "U1 article page and LaTeX author support page", "filter": "retrieval record", "statistic": "date read"}, kind="text")
V.set("v_retrieved_date", C["source"]["retrieved_utc"][:10], "date", {"population": "IJAMT submission guidelines", "filter": "retrieval record", "statistic": "date the page was read (UTC)"}, kind="text")
V.set("v_source_url", C["source"]["url"], "text", {"population": "IJAMT submission guidelines", "filter": "retrieval record", "statistic": "URL read"}, kind="text")
V.set("v_page_chars", C["source"]["page_text_chars"], "characters", {"population": "IJAMT submission guidelines", "filter": "document.body.innerText", "statistic": "characters"})
V.save()
print("q01_venue: registered", len([k for k in V.v if V.v[k]["source"] == "q01_venue.py"]), "values")
