"""Local fictional learning reference. No website/database/cloud connection."""
import csv,json,sqlite3,hashlib,math,re,statistics,collections
from pathlib import Path

def py_functions(source,out):
 def line_value(quantity,price):
  if isinstance(quantity,bool) or not isinstance(quantity,int) or quantity<=0:raise ValueError('Quantity must be a positive integer')
  if isinstance(price,bool) or not isinstance(price,int) or price<0:raise ValueError('Price must be nonnegative integer cents')
  return quantity*price
 checks=[]
 for q,p in [(2,1000),(1,0),(0,1000),(-1,500),(True,100)]:
  try:checks.append({'quantity':q,'price':p,'result':line_value(q,p)})
  except ValueError as e:checks.append({'quantity':q,'price':p,'rejected':str(e)})
 return {'checks':checks,'source_sha256':hashlib.sha256((Path(source)/'order_lines.csv').read_bytes()).hexdigest()}

if __name__=='__main__':
    import argparse,platform
    parser=argparse.ArgumentParser();parser.add_argument('--source',default='.');parser.add_argument('--output',required=True);args=parser.parse_args()
    source=Path(args.source).resolve();out=Path(args.output).resolve()
    if source==out:raise ValueError('Source and output must differ')
    out.mkdir(parents=False,exist_ok=False)
    # Partial folders after an error are NOT successful; result.json is written last.
    result=py_functions(source,out)
    with (out/'result.json').open('x',encoding='utf-8') as handle:json.dump(result,handle,indent=2,allow_nan=False);handle.write(chr(10))
    print(json.dumps(result,indent=2,allow_nan=False))
