"""Verify the frozen host selection and count the published audit. No network needed.

Usage: python3 reproduce.py
Place audit.csv, sampling-frame.csv and summary.json next to this file.
This reproduces recorded classifications, not the subjective content review.
"""
import csv
import hashlib
import json
from collections import Counter
from decimal import Decimal, ROUND_HALF_UP
from pathlib import Path

root = Path(__file__).resolve().parent
summary = json.loads((root / 'summary.json').read_text())
with (root / 'sampling-frame.csv').open() as f:
    hosts = [r['normalized_host'] for r in csv.DictReader(f)]
with (root / 'audit.csv').open() as f:
    audit = list(csv.DictReader(f))

assert len(hosts) == len(set(hosts)) == summary['population_n']
def digest(host):
    return hashlib.sha256((summary['seed'] + host).encode()).hexdigest()
selected = sorted(hosts, key=digest)[:400]
assert len(audit) == len({r['host'] for r in audit}) == 400
assert [r['host'] for r in audit] == selected
for i, row in enumerate(audit, 1):
    assert int(row['sample_index']) == i
    assert row['sample_hash'] == digest(row['host'])
    expected = (
        row['endpoint_class'] == 'h1_text_candidate'
        and row['file_purpose'] == 'project_information'
        and row['homepage_review'] == 'matching_project_content'
    )
    assert (row['primary_positive'] == 'True') == expected
    if expected:
        assert row['file_status'] == row['homepage_status'] == '200'

hits = sum(r['primary_positive'] == 'True' for r in audit)
pct = float((Decimal(hits) * 100 / len(audit)).quantize(Decimal('.1'), rounding=ROUND_HALF_UP))
counts = dict(Counter(r['endpoint_class'] for r in audit))
assert hits == summary['primary_n'] and pct == summary['primary_pct']
assert counts == summary['endpoint_counts']
assert dict(Counter(r['file_purpose'] for r in audit)) == summary['file_purpose_counts']
print(json.dumps({'sample_verified': True, 'hits': hits, 'n': len(audit), 'pct': pct, 'endpoint_counts': counts}, indent=2))
