"""Focused parent preference checks against an isolated fictional school."""
import http.cookiejar,re,sys,urllib.parse,urllib.request
base=sys.argv[1] if len(sys.argv)>1 else 'http://127.0.0.1:8089'
client=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()))
html=client.open(base+'/?page=login').read().decode()
def post(action,values,ret='?page=account'):
    global html
    token=re.search('name="csrf" value="([^"]+)"',html).group(1)
    data={'csrf':token,'action':action,'return_to':ret,**values}
    html=client.open(urllib.request.Request(base+'/',urllib.parse.urlencode(data).encode())).read().decode()
    return html
post('login',{'email':'parent@demo.school','password':'SchoolDemo!2026'})
html=client.open(base+'/?page=account').read().decode()
assert 'Parent email preferences' in html
print('PASS parent sees per-child email preferences')
assert 'Email preferences updated' in post('erp.parent.preferences',{'student_id':'1'})
print('PASS parent can opt out for own child')
assert 'Student record unavailable' in post('erp.parent.preferences',{'student_id':'2','receive_emails':'1'})
print('PASS parent cannot change another child’s preferences')
assert 'Email preferences updated' in post('erp.parent.preferences',{'student_id':'1','receive_emails':'1'})
print('PASS parent can opt back in for own child')
print('4 focused preference checks passed.')
