"""Reference solution for Nonprofit evaluation project: reconcile the budget and the outcome claims.
Original synthetic teaching data only. Python 3.10+, standard library.
Run python solve.py [path-to-project-inputs]. No network or credentials.
"""
from pathlib import Path
from datetime import datetime, date
import csv, json, math, statistics, sys

def read(root, name):
    with (Path(root)/name).open(newline='',encoding='utf-8') as f:
        reader=csv.DictReader(f)
        if not reader.fieldnames or len(reader.fieldnames)!=len(set(reader.fieldnames)):
            raise ValueError(f'{name}: missing or duplicate headers')
        rows=list(reader)
        if any(None in row or any(v is None for v in row.values()) for row in rows):
            raise ValueError(f'{name}: inconsistent row length')
        return rows

def rules(root): return json.loads((Path(root)/'rules.json').read_text())

def number(value):
    x=float(value)
    if not math.isfinite(x): raise ValueError('Non-finite input')
    return x

def nonnegative(value):
    x=number(value)
    if x<0:raise ValueError('Negative quantity or amount')
    return x

def flag(value):
    if value not in ('true','false'):raise ValueError('Boolean must be true or false')
    return value=='true'

def timestamp(value):
    d=datetime.fromisoformat(value.replace('Z','+00:00'))
    if d.tzinfo is None:raise ValueError('Timestamp requires an explicit time zone')
    return d

def unique(rows, keys, ignore=()):
    by={};duplicates=0
    for row in rows:
        key=tuple(row[k] for k in keys)
        if any(not k for k in key):raise ValueError('Missing record identity')
        if key in by:
            prior={k:v for k,v in by[key].items() if k not in ignore}
            new={k:v for k,v in row.items() if k not in ignore}
            if prior!=new:raise ValueError(f'Conflicting records at {key}')
            duplicates+=1
        else:by[key]=row
    return list(by.values()),duplicates

def rounded(value):
    if isinstance(value,float): return round(value,8)
    if isinstance(value,dict):return {k:rounded(v) for k,v in value.items()}
    if isinstance(value,list):return [rounded(v) for v in value]
    return value

def month_index(s):
    d=datetime.strptime(s,'%Y-%m')
    if d.strftime('%Y-%m')!=s:raise ValueError('Use YYYY-MM')
    return d.year*12+d.month

def grant_evaluation(root):
    r=rules(root);budget,_=unique(read(root,'budget.csv'),['item']);funds,_=unique(read(root,'funding.csv'),['source_id']);people,_=unique(read(root,'participants.csv'),['participant_id'])
    if len(people)!=r['actual_enrollment']:raise ValueError('Enrollment count mismatch')
    planned=sum(nonnegative(x['planned_quantity'])*nonnegative(x['planned_rate']) for x in budget);actual=sum(nonnegative(x['actual_quantity'])*nonnegative(x['actual_rate']) for x in budget)
    if any(x['status'] not in ('committed','prospective') for x in funds):raise ValueError('Unknown funding status')
    committed=sum(nonnegative(x['amount']) for x in funds if x['status']=='committed')
    pairs=[]
    for x in people:
        for field in ['pre_score','post_score']:
            if x[field]!='' and not 0<=number(x[field])<=r['score_scale_max']:raise ValueError('Score outside defined scale')
        if x['pre_score']!='' and x['post_score']!='':pairs.append((number(x['pre_score']),number(x['post_score'])))
    completed=sum(flag(x['completed']) for x in people);improved=sum(b>a for a,b in pairs);n=len(people);p=len(pairs)
    return {'planned_cost':planned,'actual_cost':actual,'cost_variance':actual-planned,'committed_funding':committed,'unfunded_actual_cost':actual-committed,'enrolled':n,'completed':completed,'paired_measurements':p,'completion_rate':completed/n,'paired_coverage':p/n,'paired_mean_change':statistics.mean(b-a for a,b in pairs) if pairs else None,'observed_improved':improved,'whole_cohort_improved_lower_bound':improved/n,'whole_cohort_improved_upper_bound':(improved+n-p)/n,'cost_per_enrolled':actual/n,'cost_per_completion':actual/completed if completed else None}

def main():
    directory=Path(sys.argv[1]) if len(sys.argv)>1 else Path(__file__).resolve().parent
    try:
        result=grant_evaluation(directory)
        print(json.dumps(rounded(result),indent=2,allow_nan=False))
    except (OSError,ValueError,KeyError,TypeError,ZeroDivisionError) as exc:
        print(f"Input could not be analyzed: {exc}",file=sys.stderr);return 2
    return 0
if __name__=='__main__':raise SystemExit(main())
