"""Offline fictional request-count comparisons; no network or authenticity check."""
import json,sys
FIELDS=("scope","filter","metric")
def compare(p):
 a,b=p.get("before",{}),p.get("after",{});result={"id":p.get("id"),"uniquePagesLost":None,"indexingRecovery":None,"cause":None}
 def done(s,**kw):return {**result,"status":s,**kw}
 if not all(isinstance(x.get("reference"),str) and x["reference"].strip() for x in (a,b)):return done("MISSING_REFERENCE")
 if any(not a.get(k) or a.get(k)!=b.get(k) for k in FIELDS):return done("INCOMPARABLE_SCOPE")
 if any(type(x.get("days")) is not int or x["days"]<1 for x in (a,b)) or a["days"]!=b["days"]:return done("INCOMPARABLE_DAYS")
 if any(x.get(k) is None for x in (a,b) for k in ("total","html")):return done("MISSING_COUNTS")
 if any(type(x[k]) is not int or x[k]<0 for x in (a,b) for k in ("total","html")) or any(x["html"]>x["total"] for x in (a,b)):return done("INVALID_COUNTS")
 return done("COMPARABLE_REQUEST_COUNTS",totalDelta=b["total"]-a["total"],totalPercentChange=None if a["total"]==0 else round((b["total"]-a["total"])*100/a["total"],4),htmlDelta=b["html"]-a["html"])
if __name__=="__main__":
 print(json.dumps([compare(p) for p in json.load(open(sys.argv[1],encoding="utf-8"))],indent=2))
