"""Original fixed-schema example. CSV byte checks, not spreadsheet certification.
python make-review-csv.py formula-sample.json new-output-directory
"""
import csv,json,sys
from pathlib import Path
STARTERS='=+-@\t\r\n\uff1d\uff0b\uff0d\uff20'
def flagged(text):
    return bool(text) and (text[0] in STARTERS or text.lstrip()[:1] in STARTERS and bool(text.lstrip()))
rows=json.loads(Path(sys.argv[1]).read_text(encoding='utf-8'))
if not isinstance(rows,list) or not rows: raise ValueError('expected nonempty array')
for r in rows:
    if not isinstance(r,dict) or set(r)!={'id','comment'} or not all(isinstance(v,str) for v in r.values()):
        raise ValueError('this fixture requires exactly id and comment, both strings')
out=Path(sys.argv[2]);out.mkdir(exist_ok=False)
columns=['id','comment']; review=[];changes=[]
for n,row in enumerate(rows,1):
    copy={}
    for key in columns:
        value=row[key]; changed=flagged(value)
        copy[key]='TEXT: '+value if changed else value
        if changed: changes.append({'record':n,'column':key,'original':value,'review':copy[key]})
    review.append(copy)
for name,data in [('raw.csv',rows),('review.csv',review)]:
    with (out/name).open('w',encoding='utf-8',newline='') as f:
        w=csv.DictWriter(f,fieldnames=columns,quoting=csv.QUOTE_ALL)
        w.writeheader();w.writerows(data)
(out/'changes.json').write_text(json.dumps(changes,indent=2)+'\n',encoding='utf-8')
print(json.dumps({'records':len(rows),'columns':len(columns),'changed_cells':len(changes),
    'scope':'CSV values only; spreadsheet behavior not tested; review data intentionally changed'}))
