#!/usr/bin/env python3
"""Publish repaired automated outputs without cumulative body excerpts."""
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"


def sanitize_csv(source: Path, target: Path, excerpt_fields: set[str], status_field: str) -> None:
    with source.open(newline="", encoding="utf-8") as stream:
        reader = csv.DictReader(stream)
        rows = list(reader)
        fields = reader.fieldnames
    assert fields
    for row in rows:
        for field in excerpt_fields:
            if field in row:
                row[field] = ""
        row[status_field] = "automated_screening_only_failed_manual_semantic_gate"
    with target.open("w", newline="", encoding="utf-8") as stream:
        writer = csv.DictWriter(stream, fieldnames=fields)
        writer.writeheader()
        writer.writerows(rows)


def main() -> None:
    sanitize_csv(
        WORK / "university-automated-article-coding.csv",
        OUT / "article-coding.csv",
        {"marked_evidence_excerpt_max_25_words"},
        "coding_status",
    )
    sanitize_csv(
        WORK / "university-automated-actor-coding.csv",
        OUT / "actor-level-analysis.csv",
        {"marked_evidence_excerpt"},
        "validation_status",
    )
    results = json.loads((WORK / "university-automated-results.json").read_text(encoding="utf-8"))
    results["study"]["interpretation_status"] = "SCREENING_ONLY_ALL_SEMANTIC_POPULATION_ESTIMATES_SUPPRESSED"
    results["study"]["manual_validation_outcome"] = {
        "audit_n": 230,
        "threshold": 0.60,
        "semantic_measures_tested": 19,
        "measures_passing": 0,
        "consequence": "Do not promote any automated semantic value to a population finding.",
        "validation_file": "university-validation-results.json",
    }
    (OUT / "automated-screening-results.json").write_text(json.dumps(results, indent=2) + "\n", encoding="utf-8")
    with (OUT / "article-coding.csv").open(newline="", encoding="utf-8") as stream:
        article_n = sum(1 for _ in csv.DictReader(stream))
    with (OUT / "actor-level-analysis.csv").open(newline="", encoding="utf-8") as stream:
        actor_n = sum(1 for _ in csv.DictReader(stream))
    print(json.dumps({"article_rows": article_n, "actor_rows": actor_n, "automated_semantic_status": "suppressed"}, indent=2))


if __name__ == "__main__":
    main()
