File size: 28,820 Bytes
ec3fbd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.output_parsers import StrOutputParser
from src.pinecone_utils import retrieve_context,retrieve_icp_type
import os
import json
import uuid
import re
from datetime import datetime
from dotenv import load_dotenv

load_dotenv()

# ============================================================
# LLM Configuration (Dual-Engine Fallback)
# ============================================================
def get_llm():
    openai_key = os.getenv("OPENAI_API_KEY")
    gemini_key = os.getenv("GOOGLE_API_KEY")

    # If no OpenAI key β†’ directly use Gemini
    if not openai_key:
        print("[LLM] No OpenAI key found β†’ Using Gemini")
        return ChatGoogleGenerativeAI(
            model="gemini-2.0-flash",
            google_api_key=gemini_key,
            temperature=0.3
        )

    try:
        primary_llm = ChatOpenAI(
            api_key=openai_key,
            model="gpt-4o-mini",
            temperature=0.3
        )

        # Test call to validate API key
        primary_llm.invoke("ping")

        print("[LLM] βœ“ OpenAI is valid β†’ Using OpenAI with Gemini fallback")

        backup_llm = ChatGoogleGenerativeAI(
            model="gemini-2.0-flash",
            google_api_key=gemini_key,
            temperature=0.3
        )

        return primary_llm.with_fallbacks([backup_llm])

    except Exception as e:
        print(f"[LLM] OpenAI failed: {e}")
        print("[LLM] Switching completely to Gemini")

        return ChatGoogleGenerativeAI(
            model="gemini-2.0-flash",
            google_api_key=gemini_key,
            temperature=0.3
        )


llm = get_llm()

# ============================================================
# Prompt Templates
# ============================================================
roadmap_prompt = PromptTemplate(
    input_variables=["context","icp_type"],
    template="""
You are a senior AI career strategist, roadmap architect, and career-state simulation engine for Vidya V3.

You are generating a deeply personalized career roadmap for ONE specific user.

The user context below contains:
- Resume/background
- Onboarding interview answers
- Career goals
- Skill gaps
- Learning preferences
- Work history
- Conversation summary
- Current learning discussions

USER CONTEXT:
{context}

USER ICP TYPE:
{icp_type}

YOUR TASK

Generate:
1. A personalized learning roadmap
2. A 7-stage career milestone progression (M01 β†’ M07)
3. A milestone-aligned weekly plan with mastery tracking

The roadmap must feel:
- psychologically believable
- emotionally specific
- professionally realistic
- personalized to THIS exact user

ICP DETECTION RULES

Infer the user's ICP TYPE from the context.

ICP-A = College Student
Signals: Student, Fresher, Internship seeking, Placement preparation, Campus hiring, Learning fundamentals, Tier 2/3 college
Tone: aspirational, placement-focused, confidence-building
Career evolution: intern-ready, screening-ready, placement-ready, offer-ready, job-ready

ICP-B = Working Professional / Service Engineer
Signals: Already employed, Service engineer, Support engineer, Working professional, Upskilling, Promotion goals, Career-switch goals
Tone: practical, professional, growth-focused, switch/promotion-oriented
Career evolution: reporting-ready, promotion-ready, stakeholder-ready, switch-ready, leadership-ready

MILESTONE DESIGN RULES

Milestones represent IDENTITY EVOLUTION, NOT course completion.
Milestones MUST:
- evolve progressively
- feel realistic
- reflect career maturity
- match the user's actual background

Each milestone must include:
- milestone_id: integer 1-7 (unique)
- estimated_days: integer (should equal weeks_in_milestone * 7)
- role: short role title
- title: milestone name
- description: 1-2 sentences
- quote: short, emotionally believable 1-sentence quote
- skills: 3-6 concise skill tags
- gaps: 2-4 real gaps
- career_progression: 2-4 outcomes the user can now claim
- new_opportunities: 2-4 realistic opportunities unlocked
- market_value: salary range string (example: "3-5 LPA" or "INR 10000-20000/month")
- modules: see milestone module breakdown rules

Milestones should feel personalized, not generic. Avoid repeating titles, roles, or quotes.

MILESTONE MODULE BREAKDOWN RULES

Each milestone must include exactly ONE "modules" object.
modules.week_range.start and modules.week_range.end must match the weeks list.
Weeks must be contiguous and non-overlapping across milestones.
Each week object must include:
- week: integer
- focus: short focus statement
- skills: list of skill tags
- status: completed | active | locked (only ONE active week overall)
- mastery_at_end: number between 0 and 1 for completed weeks, null otherwise

Set modules.mastery to a number between 0 and 1 that reflects progress across its weeks.
Milestone "modules" are separate from the top-level "Modules" list. Output both.

MODULE RULES

- Beginner β†’ 6-8 modules
- Intermediate β†’ 8-10 modules
- Advanced β†’ 6-8 modules

Each module:
- must contain 4-8 concise theoretical topics
- NO projects, NO coding assignments, NO implementation tasks
- MUST remain compatible with MCQ generation

KEEP EXISTING MODULE STRUCTURE UNCHANGED.

LANGUAGE RULES: ENGLISH ONLY. NO Hindi, NO Hinglish, NO Tamil, NO mixed language.

OUTPUT RULES: RETURN VALID JSON ONLY. NO markdown, NO explanations, NO code fences, NO extra text. RETURN RAW JSON ONLY.

RETURN JSON IN THIS EXACT STRUCTURE:

{{
    "CourseTitle": "string",
    "CourseDescription": "string",
    "DifficultyLevel": "Beginner|Intermediate|Advanced",
    "Weeks": 8,
    "LearningStyle": "theory",
    "WeeklyHours": 5,
    "Milestones": [
        {{
            "milestone_id": 1,
            "estimated_days": 14,
            "role": "string",
            "title": "string",
            "description": "string",
            "quote": "string",
            "skills": ["string"],
            "gaps": ["string"],
            "career_progression": ["string"],
            "new_opportunities": ["string"],
            "market_value": "string",
            "modules": {{
                "module_id": "M01",
                "week_range": {{ "start": 1, "end": 2 }},
                "mastery": 0.45,
                "weeks": [
                    {{
                        "week": 1,
                        "focus": "string",
                        "skills": ["string"],
                        "status": "completed",
                        "mastery_at_end": 0.35
                    }}
                ]
            }}
        }},
        {{
            "milestone_id": 2,
            "estimated_days": 7,
            "role": "string",
            "title": "string",
            "description": "string",
            "quote": "string",
            "skills": ["string"],
            "gaps": ["string"],
            "career_progression": ["string"],
            "new_opportunities": ["string"],
            "market_value": "string",
            "modules": {{
                "module_id": "M02",
                "week_range": {{ "start": 3, "end": 3 }},
                "mastery": 0.45,
                "weeks": [
                    {{
                        "week": 3,
                        "focus": "string",
                        "skills": ["string"],
                        "status": "locked",
                        "mastery_at_end": null
                    }}
                ]
            }}
        }},
        {{
            "milestone_id": 3,
            "estimated_days": 7,
            "role": "string",
            "title": "string",
            "description": "string",
            "quote": "string",
            "skills": ["string"],
            "gaps": ["string"],
            "career_progression": ["string"],
            "new_opportunities": ["string"],
            "market_value": "string",
            "modules": {{
                "module_id": "M03",
                "week_range": {{ "start": 4, "end": 4 }},
                "mastery": 0.45,
                "weeks": [
                    {{
                        "week": 4,
                        "focus": "string",
                        "skills": ["string"],
                        "status": "locked",
                        "mastery_at_end": null
                    }}
                ]
            }}
        }},
        {{
            "milestone_id": 4,
            "estimated_days": 7,
            "role": "string",
            "title": "string",
            "description": "string",
            "quote": "string",
            "skills": ["string"],
            "gaps": ["string"],
            "career_progression": ["string"],
            "new_opportunities": ["string"],
            "market_value": "string",
            "modules": {{
                "module_id": "M04",
                "week_range": {{ "start": 5, "end": 5 }},
                "mastery": 0.45,
                "weeks": [
                    {{
                        "week": 5,
                        "focus": "string",
                        "skills": ["string"],
                        "status": "locked",
                        "mastery_at_end": null
                    }}
                ]
            }}
        }},
        {{
            "milestone_id": 5,
            "estimated_days": 7,
            "role": "string",
            "title": "string",
            "description": "string",
            "quote": "string",
            "skills": ["string"],
            "gaps": ["string"],
            "career_progression": ["string"],
            "new_opportunities": ["string"],
            "market_value": "string",
            "modules": {{
                "module_id": "M05",
                "week_range": {{ "start": 6, "end": 6 }},
                "mastery": 0.45,
                "weeks": [
                    {{
                        "week": 6,
                        "focus": "string",
                        "skills": ["string"],
                        "status": "locked",
                        "mastery_at_end": null
                    }}
                ]
            }}
        }},
        {{
            "milestone_id": 6,
            "estimated_days": 7,
            "role": "string",
            "title": "string",
            "description": "string",
            "quote": "string",
            "skills": ["string"],
            "gaps": ["string"],
            "career_progression": ["string"],
            "new_opportunities": ["string"],
            "market_value": "string",
            "modules": {{
                "module_id": "M06",
                "week_range": {{ "start": 7, "end": 7 }},
                "mastery": 0.45,
                "weeks": [
                    {{
                        "week": 7,
                        "focus": "string",
                        "skills": ["string"],
                        "status": "locked",
                        "mastery_at_end": null
                    }}
                ]
            }}
        }},
        {{
            "milestone_id": 7,
            "estimated_days": 7,
            "role": "string",
            "title": "string",
            "description": "string",
            "quote": "string",
            "skills": ["string"],
            "gaps": ["string"],
            "career_progression": ["string"],
            "new_opportunities": ["string"],
            "market_value": "string",
            "modules": {{
                "module_id": "M07",
                "week_range": {{ "start": 8, "end": 8 }},
                "mastery": 0.45,
                "weeks": [
                    {{
                        "week": 8,
                        "focus": "string",
                        "skills": ["string"],
                        "status": "locked",
                        "mastery_at_end": null
                    }}
                ]
            }}
        }}
    ],
    "Modules": [
        {{
            "Week": 1,
            "ModuleName": "string",
            "Description": "string",
            "Topics": ["string"]
        }}
    ]
}}
"""
)


mcq_prompt = PromptTemplate(
    input_variables=["module_name", "module_description", "topics"],
    template="""
You are an expert quiz creator. Generate 5 high-quality multiple-choice questions for this learning module.

Module: {module_name}
Description: {module_description}
Topics Covered: {topics}

**REQUIREMENTS:**
- Questions should test understanding, not just memorization
- Each question must have 4 options (A, B, C, D)
- Only ONE correct answer per question
- Include a brief explanation for the correct answer

**LANGUAGE RULE (CRITICAL):**
- The entire response MUST be in ENGLISH ONLY
- DO NOT use Tamil, Hindi, Hinglish, or any other language
- DO NOT translate based on user context
- ALWAYS output in English

**Return ONLY valid JSON array:**

[
  {{
    "question": "Clear, specific question text?",
    "options": ["Option A", "Option B", "Option C", "Option D"],
    "correct_answer": "Option A",
    "explanation": "Brief explanation of why this is correct"
  }}
]

**DO NOT include any text outside the JSON array.**
**DO NOT use markdown code blocks.**
**Return raw JSON only.**
"""
)

roadmap_chain = roadmap_prompt | llm | StrOutputParser()
mcq_chain = mcq_prompt | llm | StrOutputParser()

# ============================================================
# Logic Functions
# ============================================================

def generate_module_mcqs(module: dict) -> list:
    module_name = module.get("ModuleName", "Unknown Module")
    module_description = module.get("Description", "")
    topics = module.get("Topics", [])
    topics_str = " | ".join(topics) if isinstance(topics[0], str) else " | ".join(
        [t.get("TopicName", "") for t in topics]
    ) if topics else ""

    print(f"[MCQ] Generating quiz for: {module_name}")

    # Maximum retry attempts
    max_retries = 3
    retry_count = 0
    
    while retry_count < max_retries:
        try:
            result = mcq_chain.invoke({
                "module_name": module_name,
                "module_description": module_description,
                "topics": topics_str
            })

            clean_result = result.strip()
            
            # --- JSON REPAIR LOGIC START (MCQ) ---
            # Using a trick to avoid breaking the code parser with markdown backticks
            markdown_marker = "`" * 3
            if markdown_marker in clean_result:
                clean_result = clean_result.replace(markdown_marker + "json", "").replace(markdown_marker, "").strip()
            
            start_idx = clean_result.find('[')
            end_idx = clean_result.rfind(']') + 1
            if start_idx != -1 and end_idx != 0:
                clean_result = clean_result[start_idx:end_idx]
            # --- JSON REPAIR LOGIC END ---

            # Attempt to parse JSON
            mcqs = json.loads(clean_result)
            
            # Validate that we have a list and it has 5 questions
            if not isinstance(mcqs, list):
                print(f"[MCQ] βœ— Expected list but got {type(mcqs)} for {module_name}, retrying... ({retry_count + 1}/{max_retries})")
                retry_count += 1
                continue
                
            # Check if we got exactly 5 questions
            if len(mcqs) == 5:
                # Don't add ai_quiz_id or sequence_number here anymore
                print(f"[MCQ] βœ“ Generated {len(mcqs)} questions for {module_name}")
                return mcqs
            else:
                print(f"[MCQ] ⚠ Got {len(mcqs)} questions instead of 5 for {module_name}, retrying... ({retry_count + 1}/{max_retries})")
                retry_count += 1
                
        except json.JSONDecodeError as e:
            print(f"[MCQ] βœ— JSON Parse Error for {module_name} (attempt {retry_count + 1}/{max_retries}): {e}")
            print(f"[MCQ] Attempting to repair malformed JSON...")
            
            # Repair strategy 1: Remove trailing commas before closing brackets
            repaired_json = re.sub(r',\s*([\]}])', r'\1', clean_result)
            
            # Repair strategy 2: Add missing quotes around property names
            repaired_json = re.sub(r'([{,])\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*:', r'\1"\2":', repaired_json)
            
            # Repair strategy 3: Replace single quotes with double quotes
            repaired_json = repaired_json.replace("'", '"')
            
            # Repair strategy 4: Remove any control characters
            repaired_json = re.sub(r'[\x00-\x1f\x7f-\x9f]', '', repaired_json)
            
            try:
                mcqs = json.loads(repaired_json)
                if isinstance(mcqs, list) and len(mcqs) == 5:
                    # Don't add ai_quiz_id or sequence_number here anymore
                    print(f"[MCQ] βœ“ Successfully repaired JSON for {module_name}")
                    return mcqs
                else:
                    print(f"[MCQ] ⚠ After repair, got {len(mcqs) if isinstance(mcqs, list) else 'invalid'} questions, retrying...")
                    retry_count += 1
            except json.JSONDecodeError as e2:
                print(f"[MCQ] βœ— Repair failed for {module_name} (attempt {retry_count + 1}/{max_retries}): {e2}")
                
                # Repair strategy 5: Try to extract valid JSON array using regex
                try:
                    # Find anything that looks like a JSON array with objects
                    array_pattern = r'\[\s*\{.*?\}\s*\]'
                    json_match = re.search(array_pattern, clean_result, re.DOTALL)
                    if json_match:
                        extracted_json = json_match.group(0)
                        mcqs = json.loads(extracted_json)
                        if isinstance(mcqs, list) and len(mcqs) == 5:
                            # Don't add ai_quiz_id or sequence_number here anymore
                            print(f"[MCQ] βœ“ Extracted valid JSON array for {module_name}")
                            return mcqs
                except:
                    pass
                
                retry_count += 1

        except Exception as e:
            print(f"[MCQ] βœ— Error generating MCQs for {module_name} (attempt {retry_count + 1}/{max_retries}): {e}")
            retry_count += 1

    # If we've exhausted all retries, return empty list (no fallbacks)
    print(f"[MCQ] βœ— All {max_retries} attempts failed for {module_name}. No questions generated.")
    return []


def transform_to_backend_format(roadmap: dict) -> dict:
    chapters = []
    total_topics = 0
    total_quizzes = 0

    for idx, module in enumerate(roadmap.get("Modules", [])):
        topics_raw = module.get("Topics", [])

        # First create topic objects
        topic_objects = []
        for t_idx, topic in enumerate(topics_raw):
            title = topic if isinstance(topic, str) else topic.get("TopicName", "Untitled")
            topic_objects.append({
                "ai_topic_id": f"ai_topic_{uuid.uuid4().hex[:12]}",
                "title": title,
                "content_type": "video",
                "sequence_number": t_idx + 1
            })

        total_topics += len(topic_objects)
        
        # Generate MCQs
        quiz_questions = generate_module_mcqs(module)
        
        # Add ai_quiz_id and associate with topics
        if quiz_questions and topic_objects:
            # Distribute quiz questions across topics 
            for q_idx, quiz in enumerate(quiz_questions):
                quiz["ai_quiz_id"] = f"ai_quiz_{uuid.uuid4().hex[:12]}"
                quiz["sequence_number"] = q_idx + 1
                # Associate with a topic (distribute evenly)
                topic_idx = q_idx % len(topic_objects)
                quiz["ai_topic_id"] = topic_objects[topic_idx]["ai_topic_id"]
            
            total_quizzes += len(quiz_questions)
        elif quiz_questions:
            # If no topics, still add quizzes but without ai_topic_id
            for q_idx, quiz in enumerate(quiz_questions):
                quiz["ai_quiz_id"] = f"ai_quiz_{uuid.uuid4().hex[:12]}"
                quiz["sequence_number"] = q_idx + 1
            total_quizzes += len(quiz_questions)

        chapters.append({
            "ai_chapter_id": f"ai_chapter_{uuid.uuid4().hex[:12]}",
            "title": module.get("ModuleName", "Untitled Chapter"),
            "sequence_number": idx + 1,
            "topics": topic_objects,
            "quiz_questions": quiz_questions
        })

    course = {
        "ai_course_id": f"ai_course_{uuid.uuid4().hex[:12]}",
        "title": roadmap.get("CourseTitle", "Personalized Learning Roadmap"),
        "description": roadmap.get("CourseDescription", ""),
        "difficulty_level": roadmap.get("DifficultyLevel", "intermediate").lower(),
        "chapters": chapters
    }

    return {
        "course": course,
        "metadata": {
            "total_courses": 1,
            "total_chapters": len(chapters),
            "total_topics": total_topics,
            "total_quiz_questions": total_quizzes
        }
    }


def run_pipeline(user_id: str, trigger_mcq: bool = True, ai_session_id: str = None, ai_roadmap_id: str = None) -> dict:
    print(f"\n[ROADMAP AGENT] Starting for user: {user_id}")
    print("=" * 60)

    session_was_provided = bool(ai_session_id)

    if not ai_session_id:
        ai_session_id = f"ai_sess_{datetime.utcnow().strftime('%Y%m%d%H%M%S')}_{uuid.uuid4().hex[:8]}"
        print(f"[ROADMAP AGENT] ⚠ No session ID provided - generated: {ai_session_id}")
    else:
        print(f"[ROADMAP AGENT] βœ“ Using session ID: {ai_session_id}")

    if not ai_roadmap_id:
        ai_roadmap_id = f"ai_roadmap_{datetime.utcnow().strftime('%Y%m%d%H%M%S')}_{uuid.uuid4().hex[:8]}"

    print(f"  - ai_session_id: {ai_session_id}")
    print(f"  - ai_roadmap_id: {ai_roadmap_id}")

    print(f"\n[ROADMAP AGENT] Retrieving user context...")
    icp_type = retrieve_icp_type(user_id)
    if not icp_type:
        print("[ICP] Onboarding missing or icp_type not set")
        return {
            "error": "Please complete onboarding first.",
            "user_id": user_id,
            "ai_session_id": ai_session_id
        }

    print(f"[ICP] User classified as: {icp_type}")

    context = retrieve_context(user_id)

    if not context:
        print("[ROADMAP AGENT] βœ— No context found!")
        return {
            "error": "No user data found. Please complete onboarding first.",
            "user_id": user_id,
            "ai_session_id": ai_session_id
        }

    print(f"[ROADMAP AGENT] βœ“ Context retrieved: {len(context)} chars")

    print(f"\n[ROADMAP AGENT] Generating roadmap with Dual-Engine (OpenAI -> Gemini)...")

    try:
        result = roadmap_chain.invoke({"context": context, "icp_type": icp_type})

        clean_result = result.strip()
        
        # --- JSON REPAIR LOGIC START (ROADMAP) ---
        markdown_marker = "`" * 3
        if markdown_marker in clean_result:
            clean_result = clean_result.replace(markdown_marker + "json", "").replace(markdown_marker, "").strip()
            
        start_idx = clean_result.find('{')
        end_idx = clean_result.rfind('}') + 1
        if start_idx != -1 and end_idx != 0:
            clean_result = clean_result[start_idx:end_idx]

        # --- JSON REPAIR LOGIC END ---

        roadmap_data = json.loads(clean_result)

        milestones = roadmap_data.get("Milestones", [])

        if len(milestones) != 7:
            raise ValueError("Exactly 7 milestones required")

        for idx, milestone in enumerate(milestones):
            if not isinstance(milestone, dict):
                raise ValueError("Each milestone must be an object")

            milestone_id = milestone.get("milestone_id", idx + 1)
            try:
                milestone_id = int(milestone_id)
            except (TypeError, ValueError):
                milestone_id = idx + 1

            milestone["milestone_id"] = milestone_id

            modules = milestone.get("modules", {})
            if isinstance(modules, list):
                modules = modules[0] if modules else {}

            if not isinstance(modules, dict):
                raise ValueError(
                    f"Milestone modules must be an object in {milestone_id}"
                )

            weeks = modules.get("weeks", [])
            if weeks is None:
                weeks = []
            if not isinstance(weeks, list):
                raise ValueError(
                    f"Milestone weeks must be a list in {milestone_id}"
                )

            week_range = modules.get("week_range", {})
            if not isinstance(week_range, dict):
                week_range = {}

            if weeks:
                first_week = weeks[0].get("week")
                last_week = weeks[-1].get("week")
                if isinstance(first_week, int) and isinstance(last_week, int):
                    week_range.setdefault("start", first_week)
                    week_range.setdefault("end", last_week)

            if "start" in week_range and "end" in week_range:
                modules["week_range"] = week_range
                if milestone.get("estimated_days") in (None, ""):
                    try:
                        start_week = int(week_range["start"])
                        end_week = int(week_range["end"])
                        milestone["estimated_days"] = max(
                            0, (end_week - start_week + 1) * 7
                        )
                    except (TypeError, ValueError):
                        pass

            if modules.get("mastery") is None:
                mastery_values = [
                    week.get("mastery_at_end")
                    for week in weeks
                    if isinstance(week.get("mastery_at_end"), (int, float))
                ]
                if mastery_values:
                    modules["mastery"] = round(
                        sum(mastery_values) / len(mastery_values), 2
                    )

            modules["weeks"] = weeks
            milestone["modules"] = modules



        print(f"[ROADMAP AGENT] βœ“ Generated: {roadmap_data.get('CourseTitle')}")
        print(f"[ROADMAP AGENT]    Modules: {len(roadmap_data.get('Modules', []))}")

        print(f"\n[ROADMAP AGENT] Transforming to backend format & generating MCQs...")
        roadmap_structure = transform_to_backend_format(roadmap_data)

        meta = roadmap_structure["metadata"]
        print(f"[ROADMAP AGENT] βœ“ Complete!")
        print(f"  Chapters: {meta['total_chapters']}, Topics: {meta['total_topics']}, Quizzes: {meta['total_quiz_questions']}")

        now = datetime.utcnow().isoformat()

        return {
            "id": str(uuid.uuid4()),
            "user_id": int(user_id),
            "ai_session_id": ai_session_id,
            "ai_roadmap_id": ai_roadmap_id,
            "title": roadmap_data.get("CourseTitle", "Personalized Learning Path"),
            "description": roadmap_data.get("CourseDescription", ""),
            "estimated_duration_weeks": roadmap_data.get("Weeks", 6),
            "difficulty_level": roadmap_data.get("DifficultyLevel", "intermediate").lower(),
            "roadmap_structure": roadmap_structure,
            "milestones": roadmap_data.get("Milestones", []),
            "ai_metadata": {
                "generated_at": now,
                "weekly_hours": roadmap_data.get("WeeklyHours", 5),
                "learning_style": roadmap_data.get("LearningStyle", "theory"),
                "session_source": "pinecone" if session_was_provided else "generated",
                "generation_model": "roadmap-gen-v2.1",
                "personalization_score": 0.92
            },
            "status": "confirmed",
            "payment_id": None,
            "is_paid": False,
            "created_at": now,
            "updated_at": now,
            "confirmed_at": now,
            "published_at": None
        }

    except json.JSONDecodeError as e:
        print(f"[ROADMAP AGENT] βœ— JSON Parse Error: {e}")
        return {
            "error": f"Invalid JSON generated: {str(e)}",
            "user_id": user_id,
            "ai_session_id": ai_session_id,
            "raw_output": result[:500] if 'result' in locals() else ""
        }

    except Exception as e:
        print(f"[ROADMAP AGENT] βœ— Error: {e}")
        print(clean_result[:1000])
        import traceback
        traceback.print_exc()
        return {
            "error": f"Failed to generate roadmap: {str(e)}",
            "user_id": user_id,
            "ai_session_id": ai_session_id
        }