"""iStudy backend: Flask + SQLite. Run: python app.py  (serves the API and the web app from ../www)."""
import os, re, io, json, secrets, sqlite3, random, datetime as dt, urllib.request
from collections import Counter
from functools import wraps
from flask import Flask, request, jsonify, g, send_from_directory
from werkzeug.security import generate_password_hash, check_password_hash

HERE = os.path.dirname(os.path.abspath(__file__))
DB = os.environ.get('ISTUDY_DB', os.path.join(HERE, 'istudy.db'))
WWW = os.environ.get('ISTUDY_WWW', os.path.join(HERE, '..', 'www'))
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024

SCHEMA = """
CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, email TEXT UNIQUE NOT NULL, pw TEXT NOT NULL, minor INTEGER DEFAULT 0, consent INTEGER DEFAULT 0, created TEXT);
CREATE TABLE IF NOT EXISTS tokens(token TEXT PRIMARY KEY, uid INTEGER REFERENCES users(id) ON DELETE CASCADE, created TEXT);
CREATE TABLE IF NOT EXISTS profiles(uid INTEGER PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE, name TEXT DEFAULT '', institution TEXT DEFAULT '', department TEXT DEFAULT '', level TEXT DEFAULT '', semester TEXT DEFAULT '', target_cgpa REAL, step INTEGER DEFAULT 2, settings TEXT DEFAULT '{"ai_personalisation":true,"share_reports":false,"notifications":true}');
CREATE TABLE IF NOT EXISTS courses(id INTEGER PRIMARY KEY, uid INTEGER REFERENCES users(id) ON DELETE CASCADE, code TEXT, name TEXT, units INTEGER DEFAULT 2, target TEXT DEFAULT 'B');
CREATE TABLE IF NOT EXISTS assessments(id INTEGER PRIMARY KEY, cid INTEGER REFERENCES courses(id) ON DELETE CASCADE, name TEXT, weight REAL, maxscore REAL DEFAULT 100, score REAL, created TEXT);
CREATE TABLE IF NOT EXISTS topics(id INTEGER PRIMARY KEY, cid INTEGER REFERENCES courses(id) ON DELETE CASCADE, name TEXT, status TEXT DEFAULT 'not_started', confidence INTEGER, notes TEXT DEFAULT '');
CREATE TABLE IF NOT EXISTS timetable(id INTEGER PRIMARY KEY, uid INTEGER REFERENCES users(id) ON DELETE CASCADE, cid INTEGER REFERENCES courses(id) ON DELETE SET NULL, title TEXT, day INTEGER, start TEXT, stop TEXT, venue TEXT DEFAULT '', lecturer TEXT DEFAULT '');
CREATE TABLE IF NOT EXISTS deadlines(id INTEGER PRIMARY KEY, uid INTEGER REFERENCES users(id) ON DELETE CASCADE, cid INTEGER REFERENCES courses(id) ON DELETE SET NULL, title TEXT, kind TEXT DEFAULT 'assignment', due TEXT);
CREATE TABLE IF NOT EXISTS sessions(id INTEGER PRIMARY KEY, uid INTEGER REFERENCES users(id) ON DELETE CASCADE, tid INTEGER REFERENCES topics(id) ON DELETE SET NULL, cid INTEGER REFERENCES courses(id) ON DELETE SET NULL, title TEXT, date TEXT, start TEXT, stop TEXT, status TEXT DEFAULT 'planned', auto INTEGER DEFAULT 0);
CREATE TABLE IF NOT EXISTS materials(id INTEGER PRIMARY KEY, uid INTEGER REFERENCES users(id) ON DELETE CASCADE, cid INTEGER REFERENCES courses(id) ON DELETE CASCADE, name TEXT, body TEXT, created TEXT);
CREATE TABLE IF NOT EXISTS chat(id INTEGER PRIMARY KEY, uid INTEGER REFERENCES users(id) ON DELETE CASCADE, role TEXT, content TEXT, basis TEXT DEFAULT '[]', meta TEXT DEFAULT '{}', created TEXT);
CREATE TABLE IF NOT EXISTS quizzes(id INTEGER PRIMARY KEY, uid INTEGER REFERENCES users(id) ON DELETE CASCADE, cid INTEGER, score INTEGER, total INTEGER, created TEXT);
CREATE TABLE IF NOT EXISTS snapshots(id INTEGER PRIMARY KEY, uid INTEGER REFERENCES users(id) ON DELETE CASCADE, cgpa REAL, status TEXT, created TEXT);
"""

# ---------- db helpers ----------
def db():
    if 'db' not in g:
        g.db = sqlite3.connect(DB); g.db.row_factory = sqlite3.Row
        g.db.execute('PRAGMA foreign_keys=ON')
    return g.db
@app.teardown_appcontext
def _close(e):
    d = g.pop('db', None)
    if d is not None: d.close()
def init_db():
    c = sqlite3.connect(DB); c.executescript(SCHEMA); c.commit(); c.close()
def rows(sql, a=()): return [dict(r) for r in db().execute(sql, a).fetchall()]
def row(sql, a=()):
    r = db().execute(sql, a).fetchone(); return dict(r) if r else None
def run(sql, a=()):
    c = db().execute(sql, a); db().commit(); return c.lastrowid
def now(): return dt.datetime.now().isoformat(timespec='seconds')
def today(): return dt.date.today()
def body(): return request.get_json(silent=True) or {}
def err(msg, code=400): return jsonify(error=msg), code

@app.after_request
def _cors(r):
    r.headers['Access-Control-Allow-Origin'] = os.environ.get('ISTUDY_CORS', '*')
    r.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization'
    r.headers['Access-Control-Allow-Methods'] = 'GET,POST,PUT,PATCH,DELETE,OPTIONS'
    return r

def auth(f):
    @wraps(f)
    def w(*a, **k):
        t = request.headers.get('Authorization', '').replace('Bearer ', '').strip()
        r = row('SELECT uid FROM tokens WHERE token=?', (t,)) if t else None
        if not r: return err('Please sign in.', 401)
        g.uid = r['uid']; return f(*a, **k)
    return w

def own(table, id_, col='uid'):
    return row(f'SELECT * FROM {table} WHERE id=? AND {col}=?', (id_, g.uid))
def own_course(cid): return own('courses', cid)
def own_assessment(aid):
    return row('SELECT a.* FROM assessments a JOIN courses c ON c.id=a.cid WHERE a.id=? AND c.uid=?', (aid, g.uid))
def own_topic(tid):
    return row('SELECT t.* FROM topics t JOIN courses c ON c.id=t.cid WHERE t.id=? AND c.uid=?', (tid, g.uid))

# ---------- time helpers ----------
def tm(s):
    m = re.match(r'^(\d{1,2}):(\d{2})$', s or '')
    if not m or int(m[1]) > 23 or int(m[2]) > 59: return None
    return int(m[1]) * 60 + int(m[2])
def fm(m): return f'{m // 60:02d}:{m % 60:02d}'
def valid_date(s):
    try: return dt.date.fromisoformat(s)
    except Exception: return None
DAYS = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

# ---------- deterministic academic logic (no AI) ----------
SCALE = [(70, 'A', 5), (60, 'B', 4), (50, 'C', 3), (45, 'D', 2), (40, 'E', 1), (0, 'F', 0)]
TARGET = {'A': 70, 'B': 60, 'C': 50, 'D': 45, 'E': 40}
def letter(p):
    for lo, l, pt in SCALE:
        if p >= lo: return l, pt
    return 'F', 0
def course_grade(assess, target='B'):
    rec = [a for a in assess if a['score'] is not None and a['maxscore']]
    total_w = sum(a['weight'] for a in assess) or 100
    earned = sum(a['score'] / a['maxscore'] * a['weight'] for a in rec)
    done_w = sum(a['weight'] for a in rec)
    remaining = total_w - done_w
    final = bool(assess) and remaining <= 0.0001 and done_w > 0
    pct = None
    if done_w > 0: pct = round((earned / total_w * 100) if final else (earned / done_w * 100), 1)
    out = dict(earned=round(earned, 2), done_weight=done_w, remaining_weight=round(remaining, 2), total_weight=total_w,
               percent=pct, final=final, status='confirmed' if final else ('estimate' if pct is not None else 'none'))
    if pct is not None: out['letter'], out['points'] = letter(pct)
    tp = TARGET.get(target, 60) * total_w / 100
    out['target'] = target
    if remaining > 0.0001:
        need = (tp - earned) / remaining * 100
        out['needed_pct_on_remaining'] = round(need, 1)
        out['needed_message'] = ('Your target is already secured on the work recorded so far.' if need <= 0 else
            f'To reach {target}, you would need about {round(need)}% on the remaining assessments.' if need <= 100 else
            f'{target} is no longer reachable with the remaining assessments. A lower target may be realistic.')
    return out
def gpa_all(uid):
    tot = pts = 0; status = 'confirmed'; per = []
    for c in rows('SELECT * FROM courses WHERE uid=?', (uid,)):
        gr = course_grade(rows('SELECT * FROM assessments WHERE cid=?', (c['id'],)), c['target'])
        if gr['percent'] is None or not c['units']: continue
        tot += c['units']; pts += gr['points'] * c['units']
        if not gr['final']: status = 'estimate'
        per.append(dict(id=c['id'], code=c['code'], name=c['name'], percent=gr['percent'], letter=gr['letter'], final=gr['final']))
    return dict(cgpa=round(pts / tot, 2) if tot else None, status=status if tot else None, scale=5.0, courses=per)
def snap(uid):
    g_ = gpa_all(uid)
    if g_['cgpa'] is not None:
        run('INSERT INTO snapshots(uid,cgpa,status,created) VALUES(?,?,?,?)', (uid, g_['cgpa'], g_['status'], now()))
def clashes(entries):
    out = []
    for i, a in enumerate(entries):
        for b in entries[i + 1:]:
            if a['day'] == b['day'] and tm(a['start']) < tm(b['stop']) and tm(b['start']) < tm(a['stop']):
                out.append([a.get('id'), b.get('id')])
    return out

# ---------- auth ----------
def make_session(uid, code=200):
    t = secrets.token_urlsafe(32); run('INSERT INTO tokens(token,uid,created) VALUES(?,?,?)', (t, uid, now()))
    return jsonify(token=t, user=me_json(uid)), code
def me_json(uid):
    u = row('SELECT id,email,minor FROM users WHERE id=?', (uid,)); p = row('SELECT * FROM profiles WHERE uid=?', (uid,))
    p['settings'] = json.loads(p['settings']); return dict(u, profile=p)
@app.post('/api/auth/signup')
def signup():
    d = body(); email = (d.get('email') or '').strip().lower(); pw = d.get('password') or ''
    if not re.match(r'^[^@\s]+@[^@\s]+\.[^@\s]+$', email): return err('Enter a valid email address.')
    if len(pw) < 8: return err('Use a password of at least 8 characters.')
    if not d.get('agree'): return err('Please accept the Terms and Privacy Policy.')
    minor = bool(d.get('under18'))
    if minor and not d.get('guardian_consent'): return err('A parent or guardian must consent before you continue.')
    if row('SELECT 1 FROM users WHERE email=?', (email,)): return err('An account with this email already exists.', 409)
    uid = run('INSERT INTO users(email,pw,minor,consent,created) VALUES(?,?,?,?,?)', (email, generate_password_hash(pw), int(minor), int(bool(d.get('guardian_consent'))), now()))
    run('INSERT INTO profiles(uid) VALUES(?)', (uid,)); return make_session(uid, 201)
@app.post('/api/auth/login')
def login():
    d = body(); u = row('SELECT * FROM users WHERE email=?', ((d.get('email') or '').strip().lower(),))
    if not u or not check_password_hash(u['pw'], d.get('password') or ''): return err('Email or password is incorrect.', 401)
    return make_session(u['id'])
@app.post('/api/auth/logout')
@auth
def logout():
    run('DELETE FROM tokens WHERE token=?', (request.headers.get('Authorization', '').replace('Bearer ', ''),)); return jsonify(ok=True)
@app.get('/api/me')
@auth
def me(): return jsonify(me_json(g.uid))

# ---------- profile & settings ----------
@app.put('/api/profile')
@auth
def put_profile():
    d = body(); cols = ['name', 'institution', 'department', 'level', 'semester']; sets = []; vals = []
    for c in cols:
        if c in d: sets.append(f'{c}=?'); vals.append(str(d[c]).strip()[:120])
    if 'target_cgpa' in d:
        try: v = float(d['target_cgpa']) if d['target_cgpa'] not in ('', None) else None
        except ValueError: return err('Target CGPA must be a number.')
        if v is not None and not 0 <= v <= 5: return err('Target CGPA must be between 0 and 5.')
        sets.append('target_cgpa=?'); vals.append(v)
    if 'step' in d: sets.append('step=?'); vals.append(max(2, min(7, int(d['step']))))
    if sets: run(f"UPDATE profiles SET {','.join(sets)} WHERE uid=?", (*vals, g.uid))
    return jsonify(me_json(g.uid))
@app.put('/api/settings')
@auth
def put_settings():
    cur = json.loads(row('SELECT settings FROM profiles WHERE uid=?', (g.uid,))['settings']); d = body()
    for k in ('ai_personalisation', 'share_reports', 'notifications'):
        if k in d: cur[k] = bool(d[k])
    run('UPDATE profiles SET settings=? WHERE uid=?', (json.dumps(cur), g.uid)); return jsonify(cur)
@app.get('/api/export')
@auth
def export():
    out = {'exported': now(), 'user': me_json(g.uid)}
    for t, q in [('courses', 'SELECT * FROM courses WHERE uid=?'), ('timetable', 'SELECT * FROM timetable WHERE uid=?'), ('deadlines', 'SELECT * FROM deadlines WHERE uid=?'),
                 ('sessions', 'SELECT * FROM sessions WHERE uid=?'), ('materials', 'SELECT id,cid,name,created FROM materials WHERE uid=?'), ('quizzes', 'SELECT * FROM quizzes WHERE uid=?'),
                 ('assessments', 'SELECT a.* FROM assessments a JOIN courses c ON c.id=a.cid WHERE c.uid=?'), ('topics', 'SELECT t.* FROM topics t JOIN courses c ON c.id=t.cid WHERE c.uid=?')]:
        out[t] = rows(q, (g.uid,))
    return jsonify(out)
@app.delete('/api/account')
@auth
def delete_account():
    run('DELETE FROM users WHERE id=?', (g.uid,)); return jsonify(ok=True)

# ---------- courses, assessments, topics ----------
@app.get('/api/courses')
@auth
def courses():
    out = []
    for c in rows('SELECT * FROM courses WHERE uid=? ORDER BY code', (g.uid,)):
        c['grade'] = course_grade(rows('SELECT * FROM assessments WHERE cid=?', (c['id'],)), c['target'])
        c['topics'] = {r['status']: r['n'] for r in rows('SELECT status, COUNT(*) n FROM topics WHERE cid=? GROUP BY status', (c['id'],))}
        out.append(c)
    return jsonify(out)
@app.post('/api/courses')
@auth
def add_course():
    d = body(); code = (d.get('code') or '').strip().upper()[:12]; name = (d.get('name') or '').strip()[:80] or code
    if not code: return err('Enter the course code.')
    try: units = int(d.get('units', 2))
    except ValueError: return err('Credit units must be a whole number.')
    if not 0 <= units <= 12: return err('Credit units must be between 0 and 12.')
    target = d.get('target', 'B') if d.get('target', 'B') in TARGET else 'B'
    cid = run('INSERT INTO courses(uid,code,name,units,target) VALUES(?,?,?,?,?)', (g.uid, code, name, units, target))
    for n, w in (('Continuous assessment', 30), ('Test', 20), ('Examination', 50)):
        run('INSERT INTO assessments(cid,name,weight,maxscore,created) VALUES(?,?,?,?,?)', (cid, n, w, w, now()))
    return jsonify(own_course(cid)), 201
@app.put('/api/courses/<int:cid>')
@auth
def put_course(cid):
    c = own_course(cid)
    if not c: return err('Course not found.', 404)
    d = body(); name = d.get('name', c['name']); target = d.get('target', c['target']); units = d.get('units', c['units'])
    if target not in TARGET: return err('Choose a target grade from A to E.')
    run('UPDATE courses SET name=?,target=?,units=? WHERE id=?', (name, target, int(units), cid)); snap(g.uid); return jsonify(own_course(cid))
@app.delete('/api/courses/<int:cid>')
@auth
def del_course(cid):
    if not own_course(cid): return err('Course not found.', 404)
    run('DELETE FROM courses WHERE id=?', (cid,)); snap(g.uid); return jsonify(ok=True)
@app.get('/api/courses/<int:cid>/grades')
@auth
def grades(cid):
    c = own_course(cid)
    if not c: return err('Course not found.', 404)
    a = rows('SELECT * FROM assessments WHERE cid=? ORDER BY id', (cid,)); return jsonify(course=c, assessments=a, summary=course_grade(a, c['target']))
def check_assess(d, cur=None):
    cur = cur or {}; w = d.get('weight', cur.get('weight')); m = d.get('maxscore', cur.get('maxscore')); s = d.get('score', cur.get('score'))
    try:
        w = float(w); m = float(m); s = None if s in (None, '') else float(s)
    except (TypeError, ValueError): return None, 'Enter numbers for weight, maximum score and score.'
    if w <= 0 or w > 100: return None, 'Weight must be between 0 and 100.'
    if m <= 0: return None, 'Maximum score must be above zero.'
    if s is not None and not 0 <= s <= m: return None, f'Score must be between 0 and {m:g}.'
    return (w, m, s), None
@app.post('/api/courses/<int:cid>/assessments')
@auth
def add_assess(cid):
    if not own_course(cid): return err('Course not found.', 404)
    d = body(); v, e = check_assess(d)
    if e: return err(e)
    if sum(a['weight'] for a in rows('SELECT weight FROM assessments WHERE cid=?', (cid,))) + v[0] > 100.0001: return err('Assessment weights cannot add up to more than 100%.')
    aid = run('INSERT INTO assessments(cid,name,weight,maxscore,score,created) VALUES(?,?,?,?,?,?)', (cid, (d.get('name') or 'Assessment')[:60], *v, now())); snap(g.uid)
    return jsonify(id=aid), 201
@app.put('/api/assessments/<int:aid>')
@auth
def put_assess(aid):
    a = own_assessment(aid)
    if not a: return err('Assessment not found.', 404)
    d = body(); v, e = check_assess(d, a)
    if e: return err(e)
    others = sum(x['weight'] for x in rows('SELECT weight FROM assessments WHERE cid=? AND id<>?', (a['cid'], aid)))
    if others + v[0] > 100.0001: return err('Assessment weights cannot add up to more than 100%.')
    run('UPDATE assessments SET name=?,weight=?,maxscore=?,score=? WHERE id=?', (d.get('name', a['name']), *v, aid)); snap(g.uid); return jsonify(ok=True)
@app.delete('/api/assessments/<int:aid>')
@auth
def del_assess(aid):
    if not own_assessment(aid): return err('Assessment not found.', 404)
    run('DELETE FROM assessments WHERE id=?', (aid,)); snap(g.uid); return jsonify(ok=True)
STATUSES = ('not_started', 'difficult', 'improving', 'understood')
@app.get('/api/courses/<int:cid>/topics')
@auth
def topics(cid):
    if not own_course(cid): return err('Course not found.', 404)
    return jsonify(rows('SELECT * FROM topics WHERE cid=? ORDER BY id', (cid,)))
@app.post('/api/courses/<int:cid>/topics')
@auth
def add_topic(cid):
    if not own_course(cid): return err('Course not found.', 404)
    d = body(); n = (d.get('name') or '').strip()[:100]
    if not n: return err('Enter the topic name.')
    st = d.get('status', 'not_started')
    if st not in STATUSES: return err('Unknown topic status.')
    return jsonify(id=run('INSERT INTO topics(cid,name,status,confidence) VALUES(?,?,?,?)', (cid, n, st, d.get('confidence')))), 201
@app.put('/api/topics/<int:tid>')
@auth
def put_topic(tid):
    t = own_topic(tid)
    if not t: return err('Topic not found.', 404)
    d = body(); st = d.get('status', t['status']); cf = d.get('confidence', t['confidence'])
    if st not in STATUSES: return err('Unknown topic status.')
    if cf not in (None, '') and not 1 <= int(cf) <= 5: return err('Confidence must be from 1 to 5.')
    run('UPDATE topics SET name=?,status=?,confidence=?,notes=? WHERE id=?', (d.get('name', t['name']), st, int(cf) if cf not in (None, '') else None, d.get('notes', t['notes']), tid)); return jsonify(ok=True)
@app.delete('/api/topics/<int:tid>')
@auth
def del_topic(tid):
    if not own_topic(tid): return err('Topic not found.', 404)
    run('DELETE FROM topics WHERE id=?', (tid,)); return jsonify(ok=True)

# ---------- timetable ----------
def tt_rows(): return rows('SELECT t.*, c.code FROM timetable t LEFT JOIN courses c ON c.id=t.cid WHERE t.uid=? ORDER BY day,start', (g.uid,))
@app.get('/api/timetable')
@auth
def get_tt():
    e = tt_rows(); return jsonify(entries=e, clashes=clashes(e))
def valid_entry(d):
    try: day = int(d.get('day'))
    except (TypeError, ValueError): return None, 'Choose the day of the week.'
    s, e = tm(d.get('start')), tm(d.get('stop'))
    if not 0 <= day <= 6: return None, 'Choose the day of the week.'
    if s is None or e is None: return None, 'Enter times like 08:00.'
    if e <= s: return None, 'The end time must be after the start time.'
    return (day, d['start'], d['stop']), None
@app.post('/api/timetable')
@auth
def add_tt():
    d = body(); v, e = valid_entry(d)
    if e: return err(e)
    cid = d.get('cid'); c = own_course(cid) if cid else None
    title = (d.get('title') or (c['name'] if c else '')).strip()[:80]
    if not title: return err('Choose a course or enter a title.')
    rid = run('INSERT INTO timetable(uid,cid,title,day,start,stop,venue,lecturer) VALUES(?,?,?,?,?,?,?,?)', (g.uid, c['id'] if c else None, title, *v, (d.get('venue') or '')[:60], (d.get('lecturer') or '')[:60]))
    return jsonify(id=rid, clashes=clashes(tt_rows())), 201
@app.delete('/api/timetable/<int:rid>')
@auth
def del_tt(rid):
    if not own('timetable', rid): return err('Entry not found.', 404)
    run('DELETE FROM timetable WHERE id=?', (rid,)); return jsonify(ok=True)
LINE = re.compile(r'(?P<day>mon|tue|wed|thu|fri|sat|sun)[a-z]*\.?[\s,:\-]*(?P<s>\d{1,2})[:.](?P<sm>\d{2})\s*(?:-|to|–)\s*(?P<e>\d{1,2})[:.](?P<em>\d{2})(?P<rest>.*)', re.I)
CODE = re.compile(r'\b([A-Za-z]{2,4})\s?-?(\d{3})\b')
def parse_timetable(text):
    mine = {c['code']: c for c in rows('SELECT * FROM courses WHERE uid=?', (g.uid,))}; items = []
    for ln in text.splitlines():
        m = LINE.search(ln)
        if not m: continue
        rest = m['rest'].strip(' ,-:|'); cm = CODE.search(rest); code = (cm[1] + ' ' + cm[2]).upper() if cm else ''
        venue = (rest.replace(cm[0], '') if cm else rest).strip(' ,-:|')[:60]
        day = [d.lower() for d in DAYS].index(m['day'].lower()[:3])
        it = dict(day=day, start=f"{int(m['s']):02d}:{m['sm']}", stop=f"{int(m['e']):02d}:{m['em']}", code=code, venue=venue, cid=mine[code]['id'] if code in mine else None, title=mine[code]['name'] if code in mine else code)
        it['flags'] = [f for f, bad in (('Check course', not code), ('Check venue', not venue), ('New course', bool(code) and code not in mine)) if bad]
        if tm(it['start']) is None or tm(it['stop']) is None or tm(it['stop']) <= tm(it['start']): it['flags'].append('Check times')
        items.append(it)
    return items
def mark_clashes(items):
    ex = [dict(day=r['day'], start=r['start'], stop=r['stop']) for r in tt_rows()]
    ok = [it for it in items if tm(it['start']) is not None and tm(it['stop']) is not None and tm(it['stop']) > tm(it['start'])]
    for a in ok:
        for b in ok + ex:
            if b is a: continue
            if a['day'] == b['day'] and tm(a['start']) < tm(b['stop']) and tm(b['start']) < tm(a['stop']):
                if 'Clash' not in a['flags']: a['flags'].append('Clash')
    return items
def extract_text(f):
    name = (f.filename or '').lower(); data = f.read()
    if name.endswith(('.txt', '.csv', '.md')): return data.decode('utf-8', 'ignore'), None
    if name.endswith('.pdf'):
        try:
            from pypdf import PdfReader
            t = '\n'.join((p.extract_text() or '') for p in PdfReader(io.BytesIO(data)).pages)
            return (t, None) if t.strip() else ('', 'This PDF has no readable text (it may be a scan). Paste the text instead.')
        except Exception: return '', 'Could not read this PDF. Paste the text instead.'
    try:
        import pytesseract; from PIL import Image
        return pytesseract.image_to_string(Image.open(io.BytesIO(data))), None
    except Exception:
        return '', 'Reading photos (OCR) is not enabled on this server yet. Paste the timetable text or upload a text PDF.'
@app.post('/api/timetable/import')
@auth
def import_tt():
    if request.files.get('file'):
        text, e = extract_text(request.files['file'])
        if e: return err(e, 422)
    else: text = body().get('text') or request.form.get('text') or ''
    items = mark_clashes(parse_timetable(text))
    if not items: return err('No classes were found. Use lines like "Mon 08:00-10:00 ECO 201 Hall A".', 422)
    return jsonify(items=items)
@app.post('/api/timetable/confirm')
@auth
def confirm_tt():
    items = body().get('items') or []; saved = 0
    mine = {c['code']: c for c in rows('SELECT * FROM courses WHERE uid=?', (g.uid,))}
    for it in items:
        v, e = valid_entry(it)
        if e: return err(f'{it.get("code") or "A class"}: {e}')
    for it in items:
        v, _ = valid_entry(it); code = (it.get('code') or '').strip().upper(); cid = it.get('cid')
        if not cid and code:
            if code in mine: cid = mine[code]['id']
            else:
                cid = run('INSERT INTO courses(uid,code,name,units,target) VALUES(?,?,?,?,?)', (g.uid, code, code, 2, 'B')); mine[code] = {'id': cid, 'code': code}
                for n, w in (('Continuous assessment', 30), ('Test', 20), ('Examination', 50)):
                    run('INSERT INTO assessments(cid,name,weight,maxscore,created) VALUES(?,?,?,?,?)', (cid, n, w, w, now()))
        run('INSERT INTO timetable(uid,cid,title,day,start,stop,venue,lecturer) VALUES(?,?,?,?,?,?,?,?)', (g.uid, cid, it.get('title') or code or 'Class', *v, (it.get('venue') or '')[:60], ''))
        saved += 1
    return jsonify(saved=saved, clashes=clashes(tt_rows()))

# ---------- deadlines, planner, study plan ----------
@app.get('/api/deadlines')
@auth
def get_dl(): return jsonify(rows('SELECT d.*, c.code FROM deadlines d LEFT JOIN courses c ON c.id=d.cid WHERE d.uid=? ORDER BY due', (g.uid,)))
@app.post('/api/deadlines')
@auth
def add_dl():
    d = body(); t = (d.get('title') or '').strip()[:80]
    if not t: return err('Enter a title.')
    if not valid_date(d.get('due')): return err('Choose a due date.')
    kind = d.get('kind') if d.get('kind') in ('assignment', 'test', 'exam') else 'assignment'
    cid = d.get('cid') if d.get('cid') and own_course(d.get('cid')) else None
    return jsonify(id=run('INSERT INTO deadlines(uid,cid,title,kind,due) VALUES(?,?,?,?,?)', (g.uid, cid, t, kind, d['due']))), 201
@app.delete('/api/deadlines/<int:rid>')
@auth
def del_dl(rid):
    if not own('deadlines', rid): return err('Not found.', 404)
    run('DELETE FROM deadlines WHERE id=?', (rid,)); return jsonify(ok=True)
@app.get('/api/planner')
@auth
def planner():
    a = valid_date(request.args.get('from', '')) or today(); b = valid_date(request.args.get('to', '')) or a
    if (b - a).days > 62: return err('Choose a range of up to two months.')
    tt = tt_rows(); out = {}
    for i in range((b - a).days + 1):
        d = a + dt.timedelta(days=i); ds = d.isoformat()
        out[ds] = dict(lectures=[t for t in tt if t['day'] == d.weekday()],
                       deadlines=rows('SELECT d.*, c.code FROM deadlines d LEFT JOIN courses c ON c.id=d.cid WHERE d.uid=? AND due=?', (g.uid, ds)),
                       sessions=rows('SELECT * FROM sessions WHERE uid=? AND date=? ORDER BY start', (g.uid, ds)))
    return jsonify(days=out, clashes=clashes(tt))
def free_slots(uid, d, tt, length=45, after=None):
    busy = [(tm(t['start']), tm(t['stop'])) for t in tt if t['day'] == d.weekday()]
    busy += [(tm(s['start']), tm(s['stop'])) for s in rows("SELECT start,stop FROM sessions WHERE uid=? AND date=? AND status<>'skipped'", (uid, d.isoformat()))]
    busy.sort(); cur = max(8 * 60, after or 0); out = []
    for s, e in busy + [(20 * 60, 20 * 60)]:
        while s - cur >= length + 15 and cur + length <= 20 * 60:
            out.append((cur, cur + length)); cur += length + 15
        cur = max(cur, e + 15)
    return out
def priorities(uid):
    out = []
    dl = rows("SELECT * FROM deadlines WHERE uid=? AND due>=? AND due<=?", (uid, today().isoformat(), (today() + dt.timedelta(days=7)).isoformat()))
    for t in rows("SELECT t.*, c.code, c.name cname FROM topics t JOIN courses c ON c.id=t.cid WHERE c.uid=? AND t.status<>'understood'", (uid,)):
        sc = {'difficult': 3, 'improving': 2, 'not_started': 1}[t['status']] + ((5 - t['confidence']) / 4 if t['confidence'] else 0.5)
        basis = [f"Marked {t['status'].replace('_', ' ')}"] + ([f"Confidence {t['confidence']} of 5"] if t['confidence'] else [])
        for d in dl:
            if d['cid'] == t['cid']:
                sc += 2 + (1 if d['kind'] in ('test', 'exam') else 0); basis.append(f"{d['kind'].capitalize()} due {d['due']}"); break
        t['score'] = sc; t['basis'] = basis; out.append(t)
    return sorted(out, key=lambda x: -x['score'])
def now_min(): n = dt.datetime.now(); return n.hour * 60 + n.minute + 30
@app.post('/api/plan/generate')
@auth
def gen_plan():
    d = body(); days = max(1, min(14, int(d.get('days', 7)))); per = max(1, min(4, int(d.get('per_day', 2))))
    pr = priorities(g.uid)
    if not pr: return err('Add topics to your courses first, so the plan knows what to focus on.', 422)
    run("DELETE FROM sessions WHERE uid=? AND auto=1 AND status='planned' AND date>=?", (g.uid, today().isoformat()))
    tt = tt_rows(); made = 0; k = 0
    for i in range(days):
        day = today() + dt.timedelta(days=i)
        chosen = []
        for s, e in free_slots(g.uid, day, tt, after=now_min() if i == 0 else None):
            if not chosen or s >= chosen[-1][1] + 120: chosen.append((s, e))
        for s, e in chosen[:per]:
            t = pr[k % len(pr)]; k += 1
            run('INSERT INTO sessions(uid,tid,cid,title,date,start,stop,auto) VALUES(?,?,?,?,?,?,?,1)', (g.uid, t['id'], t['cid'], f"Study: {t['name']} ({t['code']})", day.isoformat(), fm(s), fm(e))); made += 1
    return jsonify(created=made)
@app.post('/api/plan/add')
@auth
def plan_add():
    t = own_topic(body().get('tid'))
    if not t: return err('Topic not found.', 404)
    c = own_course(t['cid']); tt = tt_rows()
    for i in range(7):
        day = today() + dt.timedelta(days=i); sl = free_slots(g.uid, day, tt, after=now_min() if i == 0 else None)
        if sl:
            run('INSERT INTO sessions(uid,tid,cid,title,date,start,stop) VALUES(?,?,?,?,?,?,?)', (g.uid, t['id'], t['cid'], f"Study: {t['name']} ({c['code']})", day.isoformat(), fm(sl[0][0]), fm(sl[0][1])))
            return jsonify(date=day.isoformat(), start=fm(sl[0][0]))
    return err('No free study time was found in the next 7 days.', 422)
@app.post('/api/sessions')
@auth
def add_session():
    d = body(); s, e = tm(d.get('start')), tm(d.get('stop'))
    if not valid_date(d.get('date')) or s is None or e is None or e <= s or not (d.get('title') or '').strip(): return err('Enter a title, a date and valid times.')
    return jsonify(id=run('INSERT INTO sessions(uid,title,date,start,stop) VALUES(?,?,?,?,?)', (g.uid, d['title'][:80], d['date'], d['start'], d['stop']))), 201
@app.patch('/api/sessions/<int:sid>')
@auth
def patch_session(sid):
    s = own('sessions', sid)
    if not s: return err('Session not found.', 404)
    d = body(); st = d.get('status', s['status']); date = d.get('date', s['date']); a = d.get('start', s['start']); b = d.get('stop', s['stop'])
    if st not in ('planned', 'done', 'skipped'): return err('Unknown session status.')
    if not valid_date(date) or tm(a) is None or tm(b) is None or tm(b) <= tm(a): return err('Enter a valid date and times.')
    run('UPDATE sessions SET status=?,date=?,start=?,stop=?,auto=0 WHERE id=?', (st, date, a, b, sid)); return jsonify(ok=True)
@app.delete('/api/sessions/<int:sid>')
@auth
def del_session(sid):
    if not own('sessions', sid): return err('Session not found.', 404)
    run('DELETE FROM sessions WHERE id=?', (sid,)); return jsonify(ok=True)

# ---------- home ----------
@app.get('/api/home')
@auth
def home():
    p = row('SELECT name FROM profiles WHERE uid=?', (g.uid,)); tt = tt_rows(); t0 = today()
    pr = priorities(g.uid); focus = None
    if pr:
        t = pr[0]; verb = {'difficult': f"Revise {t['name']} ({t['code']}) first, then try a short set of practice questions.",
                           'improving': f"Keep {t['name']} ({t['code']}) moving: review your notes, then test yourself.",
                           'not_started': f"Start {t['name']} ({t['code']}): read the key idea, then rate how confident you feel."}[t['status']]
        focus = dict(text=verb, basis=t['basis'], tid=t['id'], cid=t['cid'])
    wk0 = t0 - dt.timedelta(days=t0.weekday()); wk1 = wk0 + dt.timedelta(days=6)
    plan = {r['status']: r['n'] for r in rows('SELECT status, COUNT(*) n FROM sessions WHERE uid=? AND date>=? AND date<=? GROUP BY status', (g.uid, wk0.isoformat(), wk1.isoformat()))}
    return jsonify(name=p['name'], date=t0.isoformat(), lectures=[t for t in tt if t['day'] == t0.weekday()],
                   sessions=rows('SELECT * FROM sessions WHERE uid=? AND date=? ORDER BY start', (g.uid, t0.isoformat())),
                   upcoming=rows('SELECT d.*, c.code FROM deadlines d LEFT JOIN courses c ON c.id=d.cid WHERE d.uid=? AND due>=? AND due<=? ORDER BY due', (g.uid, t0.isoformat(), (t0 + dt.timedelta(days=14)).isoformat())),
                   focus=focus, plan=plan, flagged=[dict(id=t['id'], cid=t['cid'], name=t['name'], code=t['code']) for t in pr if t['status'] == 'difficult'][:4],
                   counts=dict(courses=row('SELECT COUNT(*) n FROM courses WHERE uid=?', (g.uid,))['n'], topics=len(pr)))

# ---------- materials, revision tools, quiz (offline text analysis) ----------
STOP = set('the a an and or but if then of to in on at by for with from as is are was were be been being this that these those it its their there here which who whom what when where why how can could may might must shall should will would not no nor than too very also such into over under about between during each other some any all most more less many much one two three first second use used using called known refers means include includes including because while whereas however therefore thus'.split())
def sentences(t):
    return [s.strip() for s in re.split(r'(?<=[.!?])\s+|\n+', t) if 30 <= len(s.strip()) <= 300]
def words(s): return [w for w in re.findall(r"[A-Za-z][A-Za-z\-']{2,}", s.lower()) if w not in STOP]
def keywords(t, n=10):
    c = Counter(words(t)); top = [w for w, k in c.most_common(n * 2) if k >= 2] or [w for w, _ in c.most_common(n)]
    return top[:n]
def summarise(t, n=3):
    ss = sentences(t); f = Counter(words(t))
    if not ss: return []
    sc = sorted(range(len(ss)), key=lambda i: -sum(f[w] for w in words(ss[i])) / (len(words(ss[i])) ** 0.6 + 1))[:n]
    return [ss[i] for i in sorted(sc)]
def flashcards(t, n=8):
    out = []
    for s in sentences(t):
        m = re.match(r'^(?:An?\s+|The\s+)?(.{3,60}?)\s+(is|are|means|refers to)\s+(.{10,})$', s)
        if m: out.append(dict(front=f"What {'are' if m[2] == 'are' else 'is'} {m[1].strip()}?", back=s))
        if len(out) >= n: break
    return out
def cloze(t, n=5, seed=0):
    kw = keywords(t, 14); rnd = random.Random(seed or len(t)); out = []
    if len(kw) < 4: return out
    for s in sentences(t):
        if not 40 <= len(s) <= 220: continue
        hit = [k for k in kw if re.search(rf'\b{re.escape(k)}\b', s, re.I)]
        if not hit: continue
        k = hit[0]; q = re.sub(rf'\b{re.escape(k)}\b', '_____', s, count=1, flags=re.I)
        opts = [k] + rnd.sample([x for x in kw if x != k], 3); rnd.shuffle(opts)
        out.append(dict(q='Fill in the blank: ' + q, options=opts, answer=opts.index(k), explanation=s))
        if len(out) >= n: break
    return out
def my_text(cid=None, mid=None):
    q = 'SELECT * FROM materials WHERE uid=?'; a = [g.uid]
    if cid: q += ' AND cid=?'; a.append(cid)
    if mid: q += ' AND id=?'; a.append(mid)
    return rows(q, a)
@app.get('/api/courses/<int:cid>/materials')
@auth
def materials(cid):
    if not own_course(cid): return err('Course not found.', 404)
    return jsonify(rows('SELECT id,name,created,LENGTH(body) size FROM materials WHERE uid=? AND cid=? ORDER BY id DESC', (g.uid, cid)))
@app.post('/api/courses/<int:cid>/materials')
@auth
def add_material(cid):
    if not own_course(cid): return err('Course not found.', 404)
    if request.files.get('file'):
        f = request.files['file']; text, e = extract_text(f); name = request.form.get('name') or f.filename or 'Notes'
        if e: return err(e, 422)
    else:
        d = body(); text = d.get('text') or ''; name = d.get('name') or 'Notes'
    if len(text.strip()) < 80: return err('Add at least a few sentences of notes (80 characters or more).', 422)
    return jsonify(id=run('INSERT INTO materials(uid,cid,name,body,created) VALUES(?,?,?,?,?)', (g.uid, cid, name[:80], text[:200000], now()))), 201
@app.get('/api/materials/<int:mid>')
@auth
def material(mid):
    m = own('materials', mid)
    if not m: return err('Material not found.', 404)
    t = m['body']; return jsonify(id=mid, name=m['name'], cid=m['cid'], summary=summarise(t), concepts=keywords(t, 8), flashcards=flashcards(t))
@app.delete('/api/materials/<int:mid>')
@auth
def del_material(mid):
    if not own('materials', mid): return err('Material not found.', 404)
    run('DELETE FROM materials WHERE id=?', (mid,)); return jsonify(ok=True)
@app.post('/api/quiz/generate')
@auth
def quiz_gen():
    d = body(); ms = my_text(d.get('cid'), d.get('mid'))
    if not ms: return err('Upload some notes for this course first. Quizzes are built from your own material.', 422)
    qs = cloze('\n'.join(m['body'] for m in ms), int(d.get('n', 5)), seed=int(d.get('seed', 0)))
    if not qs: return err('Your notes are too short to build questions. Add more detail and try again.', 422)
    return jsonify(questions=qs)
@app.post('/api/quiz/result')
@auth
def quiz_result():
    d = body()
    try: s, t = int(d['score']), int(d['total'])
    except (KeyError, ValueError): return err('Missing score.')
    run('INSERT INTO quizzes(uid,cid,score,total,created) VALUES(?,?,?,?,?)', (g.uid, d.get('cid'), s, t, now())); return jsonify(ok=True)

# ---------- AI tutor ----------
def llm(system, user):
    k = os.environ.get('ANTHROPIC_API_KEY')
    if not k: return None
    try:
        req = urllib.request.Request('https://api.anthropic.com/v1/messages', headers={'x-api-key': k, 'anthropic-version': '2023-06-01', 'content-type': 'application/json'},
            data=json.dumps({'model': os.environ.get('ISTUDY_MODEL', 'claude-sonnet-4-6'), 'max_tokens': 700, 'system': system, 'messages': [{'role': 'user', 'content': user}]}).encode())
        with urllib.request.urlopen(req, timeout=40) as r: return ''.join(b.get('text', '') for b in json.load(r)['content']).strip() or None
    except Exception: return None
def retrieve(q, n=3):
    qw = set(words(q)); hits = []
    for m in my_text():
        for s in sentences(m['body']):
            sc = len(qw & set(words(s)))
            if sc: hits.append((sc, s, m['name']))
    return sorted(hits, key=lambda h: -h[0])[:n]
@app.get('/api/tutor/history')
@auth
def tutor_hist():
    out = rows('SELECT * FROM chat WHERE uid=? ORDER BY id DESC LIMIT 40', (g.uid,))[::-1]
    for r in out: r['basis'] = json.loads(r['basis']); r['meta'] = json.loads(r['meta'])
    return jsonify(out)
@app.delete('/api/tutor/history')
@auth
def tutor_clear():
    run('DELETE FROM chat WHERE uid=?', (g.uid,)); return jsonify(ok=True)
@app.post('/api/tutor/chat')
@auth
def tutor():
    msg = (body().get('message') or '').strip()[:1500]
    if not msg: return err('Type a question first.')
    prof = json.loads(row('SELECT settings FROM profiles WHERE uid=?', (g.uid,))['settings']); personal = prof.get('ai_personalisation', True)
    pr = priorities(g.uid) if personal else []; basis = []; meta = {}
    named = next((t for t in pr if t['name'].lower() in msg.lower()), None) or (pr[0] if pr and re.search(r'today|focus|should i|next|priorit|plan', msg, re.I) else None)
    hits = retrieve(msg)
    if named: basis += [f"{named['name']} ({named['code']}): " + ', '.join(named['basis'])]; meta['tid'] = named['id']; meta['cid'] = named['cid']
    if hits: basis += ['Your notes: ' + ', '.join(sorted({x[2] for x in hits}))]
    ctx = json.dumps(dict(topics_needing_attention=[dict(topic=t['name'], course=t['code'], status=t['status'], confidence=t['confidence']) for t in pr[:6]], notes_excerpts=[h[1] for h in hits]))
    sysmsg = ("You are iStudy's study assistant for university students. Use the student's recorded context when it helps and say what you used. "
              "Never calculate official grades or GPA; the app does that. Say when you are unsure. Give practical next steps. Be concise (under 180 words).")
    reply = llm(sysmsg, f'Student context: {ctx}\n\nQuestion: {msg}'); meta['mode'] = 'model' if reply else 'offline'
    if not reply:
        parts = []
        if hits: parts.append('Here is what your own notes say that is relevant:\n' + '\n'.join('- ' + h[1] for h in hits))
        if named:
            tip = {'difficult': 'Because you marked this difficult, split it into three parts: the core definition, one worked example, then five practice questions. Stop and note what you cannot explain.',
                   'improving': 'You are improving here. Test yourself without notes, then check only the points you missed.',
                   'not_started': 'Start with a 25-minute pass: read the key idea, write it in your own words, then rate your confidence.'}[named['status']]
            parts.append(f"Suggested approach for {named['name']}: {tip}")
        if not parts:
            parts.append("I can answer from the notes you upload for each course and suggest priorities from your topic ratings. Upload notes under a course, or mark some topics as difficult, then ask again.")
        parts.append('(Offline mode: full explanations need an AI model key on the server.)'); reply = '\n\n'.join(parts)
    run('INSERT INTO chat(uid,role,content,created) VALUES(?,?,?,?)', (g.uid, 'user', msg, now()))
    run('INSERT INTO chat(uid,role,content,basis,meta,created) VALUES(?,?,?,?,?,?)', (g.uid, 'assistant', reply, json.dumps(basis), json.dumps(meta), now()))
    return jsonify(reply=reply, basis=basis, meta=meta)

# ---------- progress & report ----------
@app.get('/api/progress')
@auth
def progress():
    gp = gpa_all(g.uid); snaps = rows('SELECT cgpa,status,created FROM snapshots WHERE uid=? ORDER BY id DESC LIMIT 12', (g.uid,))[::-1]
    mastery = {s: 0 for s in STATUSES}
    for r in rows('SELECT t.status s, COUNT(*) n FROM topics t JOIN courses c ON c.id=t.cid WHERE c.uid=? GROUP BY t.status', (g.uid,)): mastery[r['s']] = r['n']
    weeks = []; t0 = today() - dt.timedelta(days=today().weekday())
    for i in range(3, -1, -1):
        a = t0 - dt.timedelta(weeks=i); b = a + dt.timedelta(days=6)
        r = row("SELECT COUNT(*) planned, SUM(status='done') done FROM sessions WHERE uid=? AND date>=? AND date<=? AND status<>'skipped'", (g.uid, a.isoformat(), b.isoformat()))
        weeks.append(dict(week=a.isoformat(), planned=r['planned'], done=r['done'] or 0))
    q = row('SELECT SUM(score) s, SUM(total) t, COUNT(*) n FROM quizzes WHERE uid=?', (g.uid,))
    return jsonify(gpa=gp, snapshots=snaps, mastery=mastery, weeks=weeks, quiz=dict(avg=round(q['s'] / q['t'] * 100) if q['t'] else None, count=q['n']))
@app.get('/api/progress/report')
@auth
def report():
    p = row('SELECT * FROM profiles WHERE uid=?', (g.uid,)); gp = gpa_all(g.uid)
    lines = [f"Progress report ({today().isoformat()})", f"{p['name'] or 'Student'} · {p['institution']} {p['department']} {p['level']}".strip()]
    lines.append(f"CGPA {gp['cgpa']} ({gp['status']}), 5.0 scale" if gp['cgpa'] is not None else 'No grades recorded yet.')
    lines += [f"- {c['code']}: {c['percent']}% ({c['letter']}), {'final' if c['final'] else 'estimate'}" for c in gp['courses']]
    lines.append('Estimates are based on recorded assessments only and are not official results.')
    return jsonify(text='\n'.join(lines))

# ---------- static web app ----------
@app.get('/')
def index(): return send_from_directory(WWW, 'index.html')
@app.get('/<path:p>')
def static_files(p):
    if p.startswith('api/'): return err('Not found.', 404)
    return send_from_directory(WWW, p)

init_db()
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)), debug=False)
