#!/usr/bin/env python3
"""Merge the two independent assignments with the 50-article adjudication."""
from __future__ import annotations

import csv
import json
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]
WORK = ROOT / "work"
OUT = ROOT / "research-output"
SAMPLE = WORK / "university-manual-sample.jsonl"
A = WORK / "university-manual-coder-a.csv"
B = WORK / "university-manual-coder-b.csv"
ADJ = WORK / "university-manual-adjudicated.csv"
REPAIR = WORK / "university-manual-parser-repair-review.csv"
FINAL = OUT / "university-manual-audit.csv"

FIELDS = [
    "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",
]
SCREEN = ["subdesk", "quarter", "length_band", "sample_stratum", "screen_relevant", "screen_actor_class", "screen_release_origin", "screen_marked_language"]


def read_csv(path: Path) -> dict[str, dict]:
    with path.open(newline="", encoding="utf-8") as stream:
        reader = csv.DictReader(stream)
        assert reader.fieldnames == FIELDS, f"{path.name}: wrong columns"
        rows = list(reader)
    assert len(rows) == len({row["article_id"] for row in rows})
    return {row["article_id"]: row for row in rows}


def jsonl(path: Path) -> list[dict]:
    return [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines() if line.strip()]


def main() -> None:
    sample_rows = jsonl(SAMPLE)
    sample = {row["article_id"]: row for row in sample_rows}
    a, b, adj = read_csv(A), read_csv(B), read_csv(ADJ)
    repair = read_csv(REPAIR) if REPAIR.exists() else {}
    overlap = set(a) & set(b)
    assert len(sample) == 230 and len(a) == len(b) == 140 and len(overlap) == 50
    assert set(adj) == overlap and len(adj) == 50
    final_rows = []
    for aid in sorted(sample, key=lambda value: (sample[value]["archive_date"], value)):
        if aid in overlap:
            row = dict(adj[aid])
            row["manual_reviewer"] = "adjudicated"
            provenance = "coder_a+coder_b+adjudicator"
        elif aid in a:
            row = dict(a[aid])
            provenance = "coder_a"
        else:
            row = dict(b[aid])
            provenance = "coder_b"
        if aid in repair:
            row = dict(repair[aid])
            row["manual_reviewer"] = "parser_repair_review"
            provenance = provenance + "+parser_repair_review"
        row["coding_provenance"] = provenance
        for field in SCREEN:
            row[field] = sample[aid].get(field, "")
        row["date"] = sample[aid]["archive_date"]
        row["word_count"] = sample[aid].get("word_count", "")
        final_rows.append(row)
    extra = ["coding_provenance", "date", "word_count"] + SCREEN
    with FINAL.open("w", newline="", encoding="utf-8") as stream:
        writer = csv.DictWriter(stream, fieldnames=FIELDS + extra)
        writer.writeheader()
        writer.writerows(final_rows)
    assert len(final_rows) == 230
    assert all(len(row["evidence_excerpt"].split()) <= 25 for row in final_rows)
    print(json.dumps({"final_n": len(final_rows), "adjudicated_n": len(overlap), "output": str(FINAL)}, indent=2))


if __name__ == "__main__":
    main()
