File size: 14,264 Bytes
7644eac
 
9b44947
7644eac
9b44947
7644eac
9b44947
7644eac
 
9b44947
 
7644eac
 
 
 
9b44947
 
 
 
 
 
a5cfef0
 
9b44947
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5cfef0
9b44947
 
a5cfef0
9b44947
 
 
a5cfef0
9b44947
 
a5cfef0
9b44947
 
 
a5cfef0
9b44947
 
 
7644eac
9b44947
7644eac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
 
 
623f86a
a5cfef0
 
 
 
 
 
 
 
 
 
623f86a
7644eac
 
 
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
 
 
 
 
 
 
a5cfef0
 
 
 
 
 
 
 
 
 
 
7644eac
 
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b44947
7644eac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
"""
API endpoints for chat and milestone tracking functionality.
Supports both session-based auth (web) and JWT token auth (mobile).
"""
from flask import Blueprint, request, jsonify, current_app, g
from flask_login import login_required, current_user
from web_app.models import db, User, UserLearningPath, MilestoneProgress, LearningProgress
from src.data.document_store import DocumentStore
from datetime import datetime
from functools import wraps
import jwt

api_bp = Blueprint('api', __name__, url_prefix='/api')


def get_current_user_from_token():
    """Extract user from JWT token in Authorization header"""
    auth_header = request.headers.get('Authorization')
    if auth_header and auth_header.startswith('Bearer '):
        token = auth_header.split(' ')[1]
        try:
            payload = jwt.decode(
                token, current_app.config['SECRET_KEY'], algorithms=['HS256'])
            user_id = payload.get('user_id')
            if user_id:
                return User.query.get(user_id)
        except (jwt.ExpiredSignatureError, jwt.InvalidTokenError):
            pass
    return None


def api_auth_required(f):
    """
    Decorator that supports both session-based and JWT token authentication.
    For mobile apps using JWT tokens and web apps using session cookies.
    """
    @wraps(f)
    def decorated(*args, **kwargs):
        user = None

        # First try JWT token auth (for mobile)
        user = get_current_user_from_token()

        # If no token, fall back to session auth (for web)
        if not user and current_user.is_authenticated:
            user = current_user

        if not user:
            return jsonify({'error': 'Authentication required'}), 401

        # Store user in g for access in the route
        g.current_user = user
        return f(*args, **kwargs)

    return decorated


@api_bp.route('/save-path', methods=['POST'])
@api_auth_required
def save_path_json():
    """
    Save or update a learning path for the current user.
    Expects JSON: { path: <LearningPath dict> }
    Returns: { success, path_id }
    """
    try:
        payload = request.get_json(silent=True) or {}
        path_data = payload.get('path') or {}
        if not path_data or not isinstance(path_data, dict):
            return jsonify({'success': False, 'message': 'Invalid payload'}), 400

        path_id = path_data.get('id')
        if not path_id:
            import uuid as _uuid
            path_id = str(_uuid.uuid4())
            path_data['id'] = path_id

        # Upsert user path
        user_path = UserLearningPath.query.filter_by(
            id=path_id,
            user_id=g.current_user.id
        ).first()

        if user_path:
            user_path.path_data_json = path_data
            user_path.title = path_data.get('title', 'Untitled Path')
            user_path.topic = path_data.get('topic', 'General')
        else:
            user_path = UserLearningPath(
                id=path_id,
                user_id=g.current_user.id,
                path_data_json=path_data,
                title=path_data.get('title', 'Untitled Path'),
                topic=path_data.get('topic', 'General')
            )
            db.session.add(user_path)

        db.session.commit()

        # Ensure progress rows exist
        try:
            milestones = path_data.get('milestones', [])
            for i, _ in enumerate(milestones):
                exists = LearningProgress.query.filter_by(
                    user_learning_path_id=path_id,
                    milestone_identifier=str(i)
                ).first()
                if not exists:
                    db.session.add(LearningProgress(
                        user_learning_path_id=path_id,
                        milestone_identifier=str(i),
                        status='not_started'
                    ))
            db.session.commit()
        except Exception as _e:
            current_app.logger.warning(
                f"Failed to seed LearningProgress rows: {_e}")
            db.session.rollback()

        return jsonify({'success': True, 'path_id': path_id}), 200
    except Exception as e:
        current_app.logger.error(f"Error saving path: {str(e)}")
        db.session.rollback()
        return jsonify({'success': False, 'message': 'Failed to save path'}), 500


@api_bp.route('/paths', methods=['GET'])
@api_auth_required
def get_all_paths():
    """
    Get all learning paths for the current user.
    Returns: { paths: [...] }
    """
    try:
        user_paths = UserLearningPath.query.filter_by(
            user_id=g.current_user.id).all()
        paths = []
        for up in user_paths:
            path_data = up.path_data_json or {}
            path_data['id'] = up.id
            path_data['title'] = up.title
            path_data['topic'] = up.topic
            path_data['created_at'] = up.created_at.isoformat(
            ) if up.created_at else None

            # Include milestone completion progress
            progress_entries = LearningProgress.query.filter_by(
                user_learning_path_id=up.id
            ).all()
            completed_milestones = {}
            for progress in progress_entries:
                completed_milestones[progress.milestone_identifier] = (
                    progress.status == 'completed'
                )
            path_data['completedMilestones'] = completed_milestones

            paths.append(path_data)
        return jsonify({'paths': paths}), 200
    except Exception as e:
        current_app.logger.error(f"Error getting paths: {str(e)}")
        return jsonify({'paths': [], 'error': str(e)}), 500


@api_bp.route('/paths/<path_id>', methods=['GET'])
@api_auth_required
def get_path_by_id(path_id):
    """
    Get a specific learning path by ID.
    Returns: Learning path data
    """
    try:
        user_path = UserLearningPath.query.filter_by(
            id=path_id,
            user_id=g.current_user.id
        ).first()

        if not user_path:
            return jsonify({'error': 'Learning path not found'}), 404

        path_data = user_path.path_data_json or {}
        path_data['id'] = user_path.id
        path_data['title'] = user_path.title
        path_data['topic'] = user_path.topic
        path_data['created_at'] = user_path.created_at.isoformat(
        ) if user_path.created_at else None

        # Include milestone completion progress
        progress_entries = LearningProgress.query.filter_by(
            user_learning_path_id=path_id
        ).all()
        completed_milestones = {}
        for progress in progress_entries:
            completed_milestones[progress.milestone_identifier] = (
                progress.status == 'completed'
            )
        path_data['completedMilestones'] = completed_milestones

        return jsonify(path_data), 200
    except Exception as e:
        current_app.logger.error(f"Error getting path: {str(e)}")
        return jsonify({'error': str(e)}), 500


@api_bp.route('/paths/<path_id>', methods=['DELETE'])
@api_auth_required
def delete_path(path_id):
    """
    Delete a learning path by ID.
    Returns: { success: boolean }
    """
    try:
        user_path = UserLearningPath.query.filter_by(
            id=path_id,
            user_id=g.current_user.id
        ).first()

        if not user_path:
            return jsonify({'success': False, 'error': 'Learning path not found'}), 404

        db.session.delete(user_path)
        db.session.commit()

        return jsonify({'success': True}), 200
    except Exception as e:
        current_app.logger.error(f"Error deleting path: {str(e)}")
        db.session.rollback()
        return jsonify({'success': False, 'error': str(e)}), 500


@api_bp.route('/paths/<path_id>/milestone', methods=['POST'])
@api_auth_required
def update_milestone_status(path_id):
    """
    Update milestone completion status for a learning path.
    Expects JSON: { milestone_index: number, completed: boolean }
    """
    try:
        data = request.get_json()
        milestone_index = data.get('milestone_index')
        completed = data.get('completed', False)

        if milestone_index is None:
            return jsonify({'success': False, 'error': 'milestone_index is required'}), 400

        user_path = UserLearningPath.query.filter_by(
            id=path_id,
            user_id=g.current_user.id
        ).first()

        if not user_path:
            return jsonify({'success': False, 'error': 'Learning path not found'}), 404

        # Find or create progress entry
        progress = LearningProgress.query.filter_by(
            user_learning_path_id=path_id,
            milestone_identifier=str(milestone_index)
        ).first()

        if not progress:
            progress = LearningProgress(
                user_learning_path_id=path_id,
                milestone_identifier=str(milestone_index),
                status='completed' if completed else 'not_started'
            )
            db.session.add(progress)
        else:
            progress.status = 'completed' if completed else 'not_started'

        db.session.commit()

        return jsonify({'success': True}), 200
    except Exception as e:
        current_app.logger.error(f"Error updating milestone: {str(e)}")
        db.session.rollback()
        return jsonify({'success': False, 'error': str(e)}), 500


@api_bp.route('/track-milestone', methods=['POST'])
@api_auth_required
def track_milestone():
    """
    Track milestone completion status.
    Expects JSON: {path_id, milestone_index, completed}
    """
    try:
        data = request.get_json()
        path_id = data.get('path_id')
        milestone_index = data.get('milestone_index')
        completed = data.get('completed', False)

        if not path_id or milestone_index is None:
            return jsonify({'success': False, 'message': 'Missing required fields'}), 400

        # Verify the path belongs to the user
        user_path = UserLearningPath.query.filter_by(
            id=path_id,
            user_id=g.current_user.id
        ).first()

        if not user_path:
            return jsonify({'success': False, 'message': 'Learning path not found or access denied'}), 404

        # Find or create milestone progress entry
        progress = MilestoneProgress.query.filter_by(
            user_id=g.current_user.id,
            learning_path_id=path_id,
            milestone_index=milestone_index
        ).first()

        if not progress:
            progress = MilestoneProgress(
                user_id=g.current_user.id,
                learning_path_id=path_id,
                milestone_index=milestone_index,
                completed=completed
            )
            db.session.add(progress)
        else:
            progress.completed = completed
            progress.completed_at = datetime.utcnow() if completed else None

        db.session.commit()

        return jsonify({
            'success': True,
            'message': 'Milestone status updated successfully'
        }), 200

    except Exception as e:
        current_app.logger.error(f"Error tracking milestone: {str(e)}")
        db.session.rollback()
        return jsonify({'success': False, 'message': 'Failed to update milestone status'}), 500


@api_bp.route('/ask', methods=['POST'])
@api_auth_required
def ask_question():
    """
    Chat endpoint for asking questions about the learning path.
    Uses advanced RAG search to provide contextual answers.
    Expects JSON: {question, path_id}
    """
    try:
        data = request.get_json()
        question = data.get('question', '').strip()
        path_id = data.get('path_id')

        if not question:
            return jsonify({
                'success': False,
                'message': 'Question cannot be empty'
            }), 400

        if not path_id:
            return jsonify({
                'success': False,
                'message': 'Path ID is required'
            }), 400

        # Verify the path belongs to the user
        user_path = UserLearningPath.query.filter_by(
            id=path_id,
            user_id=g.current_user.id
        ).first()

        if not user_path:
            return jsonify({
                'success': False,
                'message': 'Learning path not found or access denied'
            }), 404

        # Get the learning path data
        path_data = user_path.path_data_json
        topic = path_data.get('topic', 'this topic')

        # Use DocumentStore's advanced RAG search
        document_store = DocumentStore()

        # Search for relevant documents
        relevant_docs = document_store.advanced_rag_search(
            query=f"{topic}: {question}",
            collection_name="learning_resources",
            top_k=3,
            use_cache=True
        )

        # Build context from retrieved documents
        context = "\n\n".join(
            [doc.page_content for doc in relevant_docs]) if relevant_docs else ""

        # Generate answer using the model orchestrator
        from src.ml.model_orchestrator import ModelOrchestrator
        orchestrator = ModelOrchestrator()
        orchestrator.init_language_model()

        prompt = f"""You are a helpful AI tutor for a learning path about {topic}.

User's question: {question}

Context from learning resources:
{context}

Please provide a clear, concise, and helpful answer to the user's question. Focus on being educational and encouraging. If the context doesn't contain relevant information, use your general knowledge about {topic} to provide a helpful response.

Answer:"""

        answer = orchestrator.generate_response(
            prompt=prompt,
            use_cache=False,
            temperature=0.7
        )

        return jsonify({
            'success': True,
            'data': {
                'answer': answer.strip(),
                'sources_count': len(relevant_docs)
            }
        }), 200

    except Exception as e:
        current_app.logger.error(f"Error processing question: {str(e)}")
        import traceback
        traceback.print_exc()
        return jsonify({
            'success': False,
            'message': 'An error occurred while processing your question'
        }), 500