import json, urllib.request, urllib.parse, http.cookiejar, sys
B='http://127.0.0.1:8080'
def mk(jar):
    cj=http.cookiejar.MozillaCookieJar(); cj.load(jar, ignore_discard=True, ignore_expires=True)
    o=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)); o.addheaders=[('Host','demo.pencil.lk')]; return o
def run(app, jar, calls):
    op=mk(jar); rec={}
    def get(action, **p):
        p={'action':action, **{k:str(v) for k,v in p.items()}}
        key='&'.join(f'{k}={v}' for k,v in sorted(p.items()))
        url=f'{B}/{app}/api.php?'+urllib.parse.urlencode(p)
        try: r=op.open(url); j=json.loads(r.read())
        except urllib.error.HTTPError as e: j=json.loads(e.read())
        rec[key]=j; return j
    calls(get)
    return rec
def parent(get):
    me=get('me'); get('poll',view='home'); get('home'); get('contacts'); get('leaves'); get('notifications'); get('vapid'); get('push_inbox')
    for c in [1,2]:
        get('attendance',child=c); get('timetable',child=c); get('fees',child=c)
        for m in ['2026-09','2026-10','2026-11','2026-12']: get('attendance',child=c,month=m)
        r=get('results',child=c)
        for s in (r.get('subjects') or []):
            sid=s.get('id') if isinstance(s,dict) else s
            if sid: get('results',child=c,subject=sid)
    get('calendar')
    for m in ['2026-09','2026-10','2026-11','2026-12','2027-01','2027-02','2027-03','2027-04']: get('calendar',month=m)
    get('updates')
    for cat in ['homework','notice','event']: get('updates',category=cat)
    cs=get('contacts')
    for s in [0,1,2,3,4,5,6,7,8,11,12]: get('messages',staff=s)
def staff(get, admin=False):
    get('me'); get('poll',view='home'); get('vapid'); get('push_inbox'); get('notifications'); get('threads'); get('parents'); get('post_targets'); get('sleaves')
    get('calendar')
    for m in ['2026-09','2026-10','2026-11','2026-12','2027-01','2027-02','2027-03','2027-04']: get('calendar',month=m)
    if admin:
        get('admin_home'); get('approvals'); get('accounts'); get('students'); get('school_today')
        for i in range(1,92): get('student',id=i)
        for t in ['all']+[f's{i}' for i in range(1,13)]: get('messages',thread=t)
    else:
        get('home'); get('my_leaves'); get('my_times'); get('school_today')
        for t in ['all','s1']: get('messages',thread=t)
    for d in ['2026-11-23','2026-11-24','2026-11-25','2026-11-26','2026-11-27','2026-11-30','2026-12-01','2026-12-02','2026-11-20','2026-11-19','2026-11-18','2026-11-17','2026-11-16']: get('day_logs',date=d)
    pr=get('parents')
    for i in range(1,3): get('pmessages',parent=i)
    tt=get('timetable')
    for c in range(1,13): get('timetable',**{'class':c})
    pt=get('post_targets')
    for g,dv in [(-2,'A'),(-1,'A'),(0,'A'),(1,'A'),(2,'A'),(3,'A'),(4,'A'),(4,'B'),(5,'A'),(6,'A'),(7,'A'),(8,'A')]: get('class_students',target=f'class:{g}:{dv}')
    mc=get('marks_classes')
    classes=mc.get('classes',[])
    for c in classes:
        for s in c.get('subjects',[]):
            ml=get('marks_list',**{'class':c['class_id'],'subject':s['id']})
            for a in ml.get('tests',ml.get('assessments',[])) or []:
                get('marks_get',id=a['id'])
            for k in ['unit','term']: get('marks_get',**{'class':c['class_id'],'subject':s['id'],'kind':k})
recs={'parent':run('parent','p.jar',parent),'staff':run('app','s.jar',lambda g:staff(g,False)),'admin':run('admin','a.jar',lambda g:staff(g,True))}
for k,v in recs.items():
    json.dump(v,open(f'enum_{k}.json','w'))
    bad=[x for x,j in v.items() if not j.get('ok')]
    print(k,len(v),'bad:',bad[:30])
