"""Original explicit-calendar comparison, not a delivery-date calculator or full schema validator."""
import json,pathlib,sys,datetime
DAYS={'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'}
def days(x):
    if not isinstance(x,list) or not x:return None
    v=[s.removeprefix('https://schema.org/') if isinstance(s,str) else None for s in x]
    return set(v) if all(s in DAYS for s in v) and len(v)==len(set(v)) else None
def duration(x):
    if not isinstance(x,dict) or x.get('unitCode') not in {'DAY','d'}:return False
    def integer(v):return type(v) is int and v>=0
    if 'value' in x:return integer(x['value']) and 'minValue' not in x and 'maxValue' not in x
    return integer(x.get('maxValue')) and ('minValue' not in x or integer(x['minValue']) and x['minValue']<=x['maxValue'])
def clock(x):
    if not isinstance(x,str):return False
    try:return datetime.time.fromisoformat(x).utcoffset() is not None
    except ValueError:return False
def review(p):
    out={'deliveryDateCalculated':False,'fullSchemaValidated':False,'carrierServiceVerified':False}
    def done(status,**more):return dict(out,status=status,**more)
    if not isinstance(p,dict) or p.get('policyApproved') is not True:return done('POLICY_UNVERIFIED')
    a=p.get('approved');h=p.get('handlingTime');t=p.get('transitTime')
    if not all(isinstance(x,dict) for x in [a,h,t]):return done('ENVELOPE_UNRESOLVED')
    hd,td,ah,at=days(h.get('businessDays')),days(t.get('businessDays')),days(a.get('handlingDays')),days(a.get('transitDays'))
    if any(x is None for x in [hd,td,ah,at]):return done('EXPLICIT_CALENDAR_UNRESOLVED')
    conflicts=[]
    if hd!=ah:conflicts.append('HANDLING_CALENDAR_MISMATCH')
    if td!=at:conflicts.append('TRANSIT_CALENDAR_MISMATCH')
    if not duration(h.get('duration')) or not duration(t.get('duration')):conflicts.append('DURATION_SHAPE_UNRESOLVED')
    cutoff=a.get('handlingCutoff');supplied=h.get('cutoffTime')
    if not clock(cutoff) or not clock(supplied) or cutoff!=supplied:conflicts.append('HANDLING_CUTOFF_UNRESOLVED')
    if 'cutoffTime' in t:conflicts.append('TRANSIT_CUTOFF_NOT_USED')
    return done('SELECTED_CONTRACT_CONFLICT' if conflicts else 'SELECTED_CALENDARS_MATCH_ONLY',conflicts=conflicts)
if __name__=='__main__':
    f=pathlib.Path(sys.argv[1])
    if f.stat().st_size>1048576:raise SystemExit('Input exceeds1MiB')
    print(json.dumps([{'case':x['case'],**review(x['packet'])} for x in json.loads(f.read_text())['cases']],ensure_ascii=False,indent=2))
