#!/usr/bin/env python3
"""Create the nine-row manual review after full-text parser repair."""

import csv
import json
from pathlib import Path


ROOT = Path(__file__).resolve().parent
ASSIGNMENT = ROOT / "university-manual-parser-repair-assignment.jsonl"
OUTPUT = ROOT / "university-manual-parser-repair-review.csv"

COLUMNS = [
    "article_id", "url", "manual_reviewer", "eligible_news", "article_type",
    "admin_present", "challenger_present", "admin_tone", "challenger_tone",
    "admin_headline_frame", "challenger_headline_frame", "admin_lede_frame",
    "challenger_lede_frame", "admin_skepticism", "challenger_skepticism",
    "official_assertion_tested", "challenger_assertion_tested", "release_reliance",
    "documents_records_data", "affected_people_consulted",
    "power_history_consequences_context", "follow_up_orientation", "first_actor",
    "first_quoted_actor", "vulnerable_identification", "evidence_excerpt",
    "manual_notes",
]

# Only judgments materially affected or exposed by the repaired text are changed.
UPDATES = {
    "spec-09d62df0f94b6af5": {
        "first_quoted_actor": "government",
        "manual_notes": "Longitudinal enterprise report remains strongly sourced; the repaired paragraph order shows the first direct quotation is the federal rationale for the cuts.",
    },
    "spec-0fd272e6c455f4a0": {
        "first_actor": "challenger",
        "manual_notes": "Four wheelchair users, observation, maps, history, counterexamples, and University responses remain balanced; repaired text begins with an affected wheelchair user.",
    },
    "spec-d31ecb82fa9a3301": {
        "admin_tone": "-1",
        "manual_notes": "Records and comparisons strongly scrutinize Barnard, but the repaired reporter voice is critical rather than intensely condemnatory; faculty condemnation remains attributed.",
    },
    "spec-2b93cac947648207": {
        "challenger_present": "1",
        "challenger_tone": "1",
        "challenger_skepticism": "0",
        "challenger_assertion_tested": "0",
        "follow_up_orientation": "0",
        "first_quoted_actor": "challenger",
        "manual_notes": "Repaired text reveals several faculty and project participants advancing substantive, favorably framed positions; their aspirational claims are relayed without independent testing.",
    },
    "spec-bec73024a1e9c88b": {
        "challenger_present": "1",
        "challenger_tone": "0",
        "challenger_skepticism": "2",
        "challenger_assertion_tested": "1",
        "manual_notes": "Repaired text shows multiple faculty program leaders making substantive claims, contextualized with bulletin data, credit comparisons, and peer policies; students remain absent.",
    },
}


def main() -> None:
    records = [json.loads(line) for line in ASSIGNMENT.read_text(encoding="utf-8").splitlines() if line.strip()]
    output_rows = []
    for record in records:
        prior = record["previous_final_manual_code"]
        row = {column: str(prior.get(column, "")) for column in COLUMNS}
        row.update(UPDATES.get(record["article_id"], {}))
        row["article_id"] = record["article_id"]
        row["url"] = record["url"]
        row["manual_reviewer"] = "parser_repair_reviewer"
        output_rows.append(row)

    with OUTPUT.open("w", encoding="utf-8", newline="") as handle:
        writer = csv.DictWriter(handle, fieldnames=COLUMNS)
        writer.writeheader()
        writer.writerows(output_rows)


if __name__ == "__main__":
    main()
