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
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
# -*- coding: utf-8 -*-
"""app.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1e_M_kKgA4L3dmmiCjbOrNT3hBfnny_9P

#Core system
"""

# core_system.py - Modified with fixed exam functionality

import os
import json
import datetime
import time
from datetime import timedelta
from typing import List, Dict, Any, Optional

# LLM Integration using LangChain
class LLMService:
    def __init__(self, api_key):
        self.api_key = api_key
        # Changed from ChatOpenAI to ChatGroq
        try:
            from langchain_groq import ChatGroq
            self.chat_model = ChatGroq(
                model="llama3-70b-8192",  # Using a Groq compatible model
                temperature=0.2,
                groq_api_key=api_key
            )
        except ImportError:
            # Fallback to direct API calls if langchain_groq is not available
            import requests
            self.chat_model = None

    def create_chain(self, template: str, output_key: str = "result"):
        if self.chat_model:
            from langchain.prompts import ChatPromptTemplate
            from langchain.chains import LLMChain

            chat_prompt = ChatPromptTemplate.from_template(template)
            return LLMChain(
                llm=self.chat_model,
                prompt=chat_prompt,
                output_key=output_key,
                verbose=True
            )
        return None

    def get_completion(self, prompt: str) -> str:
        if self.chat_model:
            chain = self.create_chain(prompt)
            response = chain.invoke({"input": ""})
            return response["result"]
        else:
            # Direct API call if langchain is not available
            import requests
            headers = {
                "Authorization": f"Bearer {self.api_key}",
                "Content-Type": "application/json"
            }
            data = {
                "model": "llama3-70b-8192",
                "messages": [{"role": "user", "content": prompt}],
                "temperature": 0.7,
                "max_tokens": 2048
            }
            response = requests.post(
                "https://api.groq.com/openai/v1/chat/completions",
                headers=headers,
                json=data
            )

            if response.status_code == 200:
                return response.json()["choices"][0]["message"]["content"]
            else:
                raise Exception(f"API Error: {response.status_code} - {response.text}")

    def generate_module_content(self, day: int, topic: str) -> str:
        prompt = f"""
        Create a comprehensive Python programming module for Day {day} covering {topic}.
        The module should follow this structure in Markdown format:

        # [Module Title]

        ## Introduction
        [A brief introduction to the day's topics]

        ## Section 1: [Section Title]
        [Detailed explanation of concepts]

        ### Code Examples
        ```python
        # Example code with comments
        ```

        ### Practice Exercises
        [2-3 exercises with clear instructions]

        ## Section 2: [Section Title]
        [Repeat the pattern for all relevant topics]

        Make sure the content is:
        - Comprehensive but focused on the day's topic
        - Includes clear examples with comments
        - Has practice exercises that build skills progressively
        - Uses proper Markdown formatting
        """
        return self.get_completion(prompt)

    def generate_exam_questions(self, day: int, topic: str, previous_mistakes: List[Dict] = None) -> List[Dict]:
        mistake_context = ""
        if previous_mistakes and len(previous_mistakes) > 0:
            mistakes = "\n".join([
                f"- Question: {m['question']}\n  Wrong Answer: {m['user_answer']}\n  Correct Answer: {m['correct_answer']}"
                for m in previous_mistakes[:3]
            ])
            mistake_context = f"""
            Include variations of questions related to these previous mistakes:
            {mistakes}
            """

        prompt = f"""
        Create a 1-hour Python exam for Day {day} covering {topic}.
        {mistake_context}

        Include 5 questions with a mix of:
        - Multiple-choice (4 options each)
        - Short-answer (requiring 1-3 lines of text)
        - Coding exercises (simple functions or snippets)

        Return your response as a JSON array where each question is an object with these fields:
        - question_type: "multiple-choice", "short-answer", or "coding"
        - question_text: The full question text
        - options: Array of options (for multiple-choice only)
        - correct_answer: The correct answer or solution
        - explanation: Detailed explanation of the correct answer
        - difficulty: Number from 1 (easiest) to 5 (hardest)

        Example:
        [
          {{
            "question_type": "multiple-choice",
            "question_text": "What is the output of print(3 * '4' + '5')?",
            "options": ["12", "445", "4445", "Error"],
            "correct_answer": "4445",
            "explanation": "The * operator with a string repeats it, and + concatenates strings",
            "difficulty": 2
          }},
          {{
            "question_type": "coding",
            "question_text": "Write a function that returns the sum of all even numbers in a list.",
            "options": null,
            "correct_answer": "def sum_even(numbers):\\n    return sum(x for x in numbers if x % 2 == 0)",
            "explanation": "This solution uses a generator expression with the sum function to add only even numbers",
            "difficulty": 3
          }}
        ]

        ONLY return the valid JSON array. Do NOT include any explanatory text or code fences.
        """

        result = self.get_completion(prompt)

        # Clean up potential formatting issues
        result = result.strip()
        if result.startswith("```json"):
            result = result.split("```json")[1]
        if result.endswith("```"):
            result = result.rsplit("```", 1)[0]

        try:
            return json.loads(result)
        except json.JSONDecodeError as e:
            print(f"JSON decode error: {e}")
            print(f"Raw response: {result}")
            # Fall back to creating a minimal structure
            return [{"question_type": "short-answer",
                     "question_text": "There was an error generating questions. Please describe what you've learned today.",
                     "options": None,
                     "correct_answer": "Any reasonable summary",
                     "explanation": "This is a backup question",
                     "difficulty": 1}]

    def evaluate_answer(self, question: Dict, user_answer: str) -> Dict:
        prompt = f"""
        Grade this response to a Python programming question:

        Question Type: {question["question_type"]}
        Question: {question["question_text"]}
        Correct Answer: {question["correct_answer"]}
        Student's Answer: {user_answer}

        Return your evaluation as a JSON object with these fields:
        - is_correct: boolean (true/false)
        - feedback: detailed explanation of what was correct/incorrect
        - correct_solution: the correct solution with explanation if the answer was wrong

        For coding questions, be somewhat lenient - focus on logic correctness rather than exact syntax matching.
        For multiple choice, it must match the correct option.
        For short answer, assess if the key concepts are present and correct.

        ONLY return the valid JSON object. Do NOT include any explanatory text.
        """

        result = self.get_completion(prompt)

        # Clean up potential formatting issues
        result = result.strip()
        if result.startswith("```json"):
            result = result.split("```json")[1]
        if result.endswith("```"):
            result = result.rsplit("```", 1)[0]

        try:
            return json.loads(result)
        except json.JSONDecodeError as e:
            print(f"JSON decode error: {e}")
            print(f"Raw response: {result}")
            # Return a fallback response
            return {
                "is_correct": False,
                "feedback": "There was an error evaluating your answer. Please try again.",
                "correct_solution": question["correct_answer"]
            }

    def answer_student_question(self, question: str, context: Optional[str] = None) -> str:
        context_text = f"Context from previous questions: {context}\n\n" if context else ""

        prompt = f"""
        {context_text}You are an expert Python tutor. Answer this student's question clearly with explanations and examples:

        {question}

        - Use code examples where appropriate
        - Break down complex concepts step by step
        - Be comprehensive but concise
        - Use proper Markdown formatting for code
        """

        return self.get_completion(prompt)

# Content Generator with simplified storage
class ContentGenerator:
    def __init__(self, api_key):
        self.llm_service = LLMService(api_key)
        # Simplified in-memory storage
        self.modules = []
        self.questions = []
        self.responses = []
        self.chat_logs = []

    def generate_module(self, day: int) -> tuple:
        day_topics = {
            1: "Python fundamentals (variables, data types, control structures)",
            2: "Intermediate Python (functions, modules, error handling)",
            3: "Advanced Python (file I/O, object-oriented programming, key libraries)"
        }
        topic = day_topics.get(day, "Python programming")

        content = self.llm_service.generate_module_content(day, topic)

        # Extract title from content
        title = f"Day {day} Python Module"
        if content.startswith("# "):
            title_line = content.split("\n", 1)[0]
            title = title_line.replace("# ", "").strip()

        # Save to in-memory storage
        module_id = len(self.modules) + 1
        self.modules.append({
            "id": module_id,
            "day": day,
            "title": title,
            "content": content,
            "created_at": datetime.datetime.utcnow()
        })

        return content, module_id

    def generate_exam(self, day: int, module_id: int, previous_mistakes: List = None) -> tuple:
        day_topics = {
            1: "Python fundamentals (variables, data types, control structures)",
            2: "Intermediate Python (functions, modules, error handling)",
            3: "Advanced Python (file I/O, object-oriented programming, key libraries)"
        }
        topic = day_topics.get(day, "Python programming")

        # Generate questions for this day's topics
        try:
            questions_data = self.llm_service.generate_exam_questions(day, topic, previous_mistakes)

            if not questions_data:
                raise ValueError("Failed to generate exam questions")

            saved_questions = []
            for q_data in questions_data:
                question_id = len(self.questions) + 1

                question = {
                    "id": question_id,
                    "module_id": module_id,
                    "question_type": q_data["question_type"],
                    "question_text": q_data["question_text"],
                    "options": q_data.get("options"),
                    "correct_answer": q_data["correct_answer"],
                    "explanation": q_data["explanation"],
                    "difficulty": q_data.get("difficulty", 3)
                }

                self.questions.append(question)
                saved_questions.append(question)

            return questions_data, saved_questions
        except Exception as e:
            print(f"Error generating exam: {str(e)}")
            # Create a simple fallback question
            fallback_question = {
                "question_type": "short-answer",
                "question_text": f"Explain a key concept you learned in Day {day} about {topic}.",
                "options": None,
                "correct_answer": "Any reasonable explanation",
                "explanation": "This is a fallback question due to an error in question generation",
                "difficulty": 2
            }

            question_id = len(self.questions) + 1
            question = {
                "id": question_id,
                "module_id": module_id,
                "question_type": fallback_question["question_type"],
                "question_text": fallback_question["question_text"],
                "options": fallback_question.get("options"),
                "correct_answer": fallback_question["correct_answer"],
                "explanation": fallback_question["explanation"],
                "difficulty": fallback_question["difficulty"]
            }

            self.questions.append(question)
            return [fallback_question], [question]

    def grade_response(self, question_id: int, user_answer: str) -> Dict:
        # Find question in memory
        question = next((q for q in self.questions if q["id"] == question_id), None)

        if not question:
            return {"error": "Question not found"}

        try:
            feedback_data = self.llm_service.evaluate_answer(question, user_answer)

            # Save response to in-memory storage
            response_id = len(self.responses) + 1
            response = {
                "id": response_id,
                "question_id": question_id,
                "user_answer": user_answer,
                "is_correct": feedback_data.get("is_correct", False),
                "feedback": feedback_data.get("feedback", ""),
                "timestamp": datetime.datetime.utcnow()
            }
            self.responses.append(response)

            return feedback_data
        except Exception as e:
            print(f"Error grading response: {str(e)}")
            # Create a fallback response
            response_id = len(self.responses) + 1
            response = {
                "id": response_id,
                "question_id": question_id,
                "user_answer": user_answer,
                "is_correct": False,
                "feedback": f"Error evaluating answer: {str(e)}",
                "timestamp": datetime.datetime.utcnow()
            }
            self.responses.append(response)

            return {
                "is_correct": False,
                "feedback": f"Error evaluating answer: {str(e)}",
                "correct_solution": question["correct_answer"]
            }

    def get_previous_mistakes(self, day: int) -> List:
        """Get mistakes from previous days to inform adaptive content"""
        if day <= 1:
            return []

        previous_day = day - 1

        # Find modules from previous day
        previous_modules = [m for m in self.modules if m["day"] == previous_day]

        if not previous_modules:
            return []

        module_ids = [module["id"] for module in previous_modules]
        questions = [q for q in self.questions if q["module_id"] in module_ids]

        if not questions:
            return []

        question_ids = [question["id"] for question in questions]
        incorrect_responses = [r for r in self.responses if r["question_id"] in question_ids and not r["is_correct"]]

        mistakes = []
        for response in incorrect_responses:
            question = next((q for q in self.questions if q["id"] == response["question_id"]), None)
            if question:
                mistakes.append({
                    "question": question["question_text"],
                    "user_answer": response["user_answer"],
                    "correct_answer": question["correct_answer"]
                })

        return mistakes

    def answer_question(self, user_question: str, related_question_id: Optional[int] = None) -> str:
        # Get context from related question if available
        context = None
        if related_question_id:
            question = next((q for q in self.questions if q["id"] == related_question_id), None)
            if question:
                context = f"Question: {question['question_text']}\nCorrect Answer: {question['correct_answer']}\nExplanation: {question['explanation']}"

        response = self.llm_service.answer_student_question(user_question, context)

        # Log the interaction
        chat_log_id = len(self.chat_logs) + 1
        chat_log = {
            "id": chat_log_id,
            "user_question": user_question,
            "ai_response": response,
            "related_question_id": related_question_id,
            "timestamp": datetime.datetime.utcnow()
        }
        self.chat_logs.append(chat_log)

        return response

# Learning System Class
class LearningSystem:
    def __init__(self, api_key):
        self.content_generator = ContentGenerator(api_key)
        self.current_day = 1
        self.current_module_id = None
        self.exam_start_time = None
        self.exam_in_progress = False
        self.exam_questions = []
        self.questions_data = []  # Store the questions data for display

    def generate_day_content(self):
        content, module_id = self.content_generator.generate_module(self.current_day)
        self.current_module_id = module_id
        return content

    def start_exam(self):
        try:
            if not self.current_module_id:
                # Check if we already have a module for this day
                existing_module = next((m for m in self.content_generator.modules if m["day"] == self.current_day), None)
                if existing_module:
                    self.current_module_id = existing_module["id"]
                else:
                    # Generate content for the day if not already done
                    content, module_id = self.content_generator.generate_module(self.current_day)
                    self.current_module_id = module_id

            # Get previous mistakes for adaptive learning
            previous_mistakes = self.content_generator.get_previous_mistakes(self.current_day)

            # Generate exam questions
            self.questions_data, self.exam_questions = self.content_generator.generate_exam(
                self.current_day,
                self.current_module_id,
                previous_mistakes
            )

            if not self.questions_data or not self.exam_questions:
                return "Failed to generate exam questions. Please try again."

            self.exam_start_time = datetime.datetime.now()
            self.exam_in_progress = True

            # Format the exam for display
            exam_text = f"# Day {self.current_day} Python Exam\n\n"
            exam_text += f"**Time Limit:** 1 hour\n"
            exam_text += f"**Start Time:** {self.exam_start_time.strftime('%H:%M:%S')}\n"
            exam_text += f"**End Time:** {(self.exam_start_time + timedelta(hours=1)).strftime('%H:%M:%S')}\n\n"

            # Add adaptive learning notice if applicable
            if previous_mistakes and len(previous_mistakes) > 0:
                exam_text += f"**Note:** This exam includes questions based on topics you had difficulty with previously.\n\n"

            for i, question in enumerate(self.questions_data):
                exam_text += f"## Question {i+1}: {question['question_type'].title()}\n\n"
                exam_text += f"{question['question_text']}\n\n"

                if question['question_type'] == "multiple-choice" and question.get('options'):
                    for j, option in enumerate(question['options']):
                        exam_text += f"- {chr(65+j)}. {option}\n"

                exam_text += "\n"

            exam_text += "## Instructions for submitting answers:\n\n"
            exam_text += "1. For multiple-choice questions, input the letter of your answer (A, B, C, or D)\n"
            exam_text += "2. For short-answer questions, write your complete answer\n"
            exam_text += "3. For coding questions, write your complete code solution\n"
            exam_text += "4. **Separate each answer with two line breaks**\n\n"

            return exam_text
        except Exception as e:
            self.exam_in_progress = False
            return f"Error starting exam: {str(e)}"

    def submit_exam(self, answers_text):
        try:
            if not self.exam_in_progress:
                return "No exam is currently in progress. Please start an exam first."

            if not self.exam_questions:
                return "No exam questions available. Please restart the exam."

            # Check time
            current_time = datetime.datetime.now()
            if current_time > self.exam_start_time + timedelta(hours=1):
                time_overrun = current_time - (self.exam_start_time + timedelta(hours=1))
                overrun_minutes = time_overrun.total_seconds() / 60
                time_notice = f"Time limit exceeded by {overrun_minutes:.1f} minutes. Your answers are being processed anyway."
            else:
                time_notice = "Exam completed within the time limit."

            # Split answers by question (double newline separator)
            answers = [ans.strip() for ans in answers_text.split("\n\n") if ans.strip()]

            feedback_text = f"# Day {self.current_day} Exam Results\n\n"
            feedback_text += f"{time_notice}\n\n"

            correct_count = 0
            total_evaluated = 0

            # Ensure we don't exceed the number of questions
            num_questions = min(len(self.exam_questions), len(answers))

            # If the user provided fewer answers than questions, fill in blanks
            while len(answers) < len(self.exam_questions):
                answers.append("")

            for i in range(len(self.exam_questions)):
                question = self.exam_questions[i]
                answer = answers[i] if i < len(answers) else ""

                # Handle empty answers
                if not answer:
                    feedback_text += f"## Question {i+1}\n\n"
                    feedback_text += "**Your Answer:** No answer provided\n\n"
                    feedback_text += "**Result:** Incorrect\n\n"
                    feedback_text += f"**Correct Solution:** {question['correct_answer']}\n\n"
                    total_evaluated += 1
                    continue

                try:
                    # Grade the response
                    feedback = self.content_generator.grade_response(question["id"], answer)
                    total_evaluated += 1

                    # Format feedback
                    feedback_text += f"## Question {i+1}\n\n"
                    feedback_text += f"**Your Answer:**\n{answer}\n\n"
                    feedback_text += f"**Result:** {'✅ Correct' if feedback.get('is_correct', False) else '❌ Incorrect'}\n\n"
                    feedback_text += f"**Feedback:**\n{feedback.get('feedback', '')}\n\n"

                    if feedback.get('is_correct', False):
                        correct_count += 1
                    else:
                        feedback_text += f"**Correct Solution:**\n{feedback.get('correct_solution', '')}\n\n"
                except Exception as e:
                    feedback_text += f"## Question {i+1}\n\n"
                    feedback_text += f"**Error grading answer:** {str(e)}\n\n"

            # Calculate score
            if total_evaluated > 0:
                score = correct_count / total_evaluated * 100
            else:
                score = 0

            feedback_text += f"# Final Score: {score:.1f}%\n\n"

            # Suggestions for improvement
            if score < 100:
                feedback_text += "## Suggestions for Improvement\n\n"
                if score < 60:
                    feedback_text += "- Review the fundamental concepts again\n"
                    feedback_text += "- Practice more with the code examples\n"
                    feedback_text += "- Use the Q&A Sandbox to ask about difficult topics\n"
                elif score < 80:
                    feedback_text += "- Focus on the specific areas where you made mistakes\n"
                    feedback_text += "- Try rewriting the solutions for incorrect answers\n"
                else:
                    feedback_text += "- Great job! Just a few minor issues to review\n"
                    feedback_text += "- Look at the explanations for the few questions you missed\n"
            else:
                feedback_text += "## Excellent Work!\n\n"
                feedback_text += "You've mastered today's content. Ready for the next day's material!\n"

            self.exam_in_progress = False
            return feedback_text
        except Exception as e:
            self.exam_in_progress = False
            return f"Error submitting exam: {str(e)}"

    def answer_sandbox_question(self, question):
        return self.content_generator.answer_question(question)

    def advance_to_next_day(self):
        if self.current_day < 3:
            self.current_day += 1
            self.current_module_id = None
            self.exam_questions = []
            return f"Advanced to Day {self.current_day}."
        else:
            return "You have completed the 3-day curriculum."

    def get_learning_progress(self):
        try:
            modules = self.content_generator.modules
            questions = self.content_generator.questions
            responses = self.content_generator.responses

            total_questions = len(questions)
            answered_questions = len(responses)
            correct_answers = sum(1 for r in responses if r["is_correct"])

            if answered_questions > 0:
                accuracy = correct_answers / answered_questions * 100
            else:
                accuracy = 0

            report = "# Learning Progress Summary\n\n"
            report += f"## Overall Statistics\n"
            report += f"- Total modules completed: {len(modules)}\n"
            report += f"- Total questions attempted: {answered_questions}/{total_questions}\n"
            report += f"- Overall accuracy: {accuracy:.1f}%\n\n"

            # Day-by-day progress with adaptive learning info
            for day in range(1, 4):
                day_modules = [m for m in modules if m["day"] == day]

                report += f"## Day {day}: "
                if day_modules:
                    report += f"{day_modules[0]['title']}\n"

                    day_questions = [q for q in questions if q["module_id"] in [m["id"] for m in day_modules]]
                    day_responses = [r for r in responses if r["question_id"] in [q["id"] for q in day_questions]]

                    day_total = len(day_questions)
                    day_answered = len(day_responses)
                    day_correct = sum(1 for r in day_responses if r["is_correct"])

                    if day_answered > 0:
                        day_accuracy = day_correct / day_answered * 100
                        report += f"- **Exam Score:** {day_accuracy:.1f}%\n"
                    else:
                        report += "- **Exam:** Not taken yet\n"

                    report += f"- Questions attempted: {day_answered}/{day_total}\n"

                    # Show adaptive learning details
                    if day > 1:
                        previous_mistakes = self.content_generator.get_previous_mistakes(day)
                        if previous_mistakes:
                            report += f"- **Adaptive Learning:** {len(previous_mistakes)} topics from Day {day-1} reinforced\n"

                    # Show exam results if available
                    if day_answered > 0:
                        report += "### Exam Performance\n"

                        # Group by question type
                        question_types = set(q["question_type"] for q in day_questions)
                        for q_type in question_types:
                            type_questions = [q for q in day_questions if q["question_type"] == q_type]
                            type_responses = [r for r in day_responses if r["question_id"] in [q["id"] for q in type_questions]]
                            type_correct = sum(1 for r in type_responses if r["is_correct"])

                            if type_responses:
                                type_accuracy = type_correct / len(type_responses) * 100
                                report += f"- **{q_type.title()}:** {type_accuracy:.1f}% correct\n"

                        # Common mistakes
                        incorrect_responses = [r for r in day_responses if not r["is_correct"]]
                        if incorrect_responses:
                            report += "\n### Areas for Improvement\n"

                            for resp in incorrect_responses[:3]:  # Show top 3 mistakes
                                question = next((q for q in questions if q["id"] == resp["question_id"]), None)
                                if question:
                                    report += f"- **Question:** {question['question_text'][:100]}...\n"
                                    report += f"  **Your Answer:** {resp['user_answer'][:100]}...\n"
                                    report += f"  **Correct Answer:** {question['correct_answer'][:100]}...\n\n"
                else:
                    report += "Not started yet\n"

                report += "\n"

            # Learning recommendations
            report += "## Recommendations\n\n"
            if correct_answers < answered_questions * 0.7:
                report += "- Review the modules before moving to the next day\n"
                report += "- Focus on practicing code examples\n"
                report += "- Use the Q&A Sandbox to clarify difficult concepts\n"
            else:
                report += "- Continue with the current pace\n"
                report += "- Try to implement small projects using what you've learned\n"

            return report
        except Exception as e:
            return f"Error generating progress report: {str(e)}"

"""#gradio"""

# Gradio UI - Modified for Google Colab
import os
import gradio as gr

# Note: We're not importing from core_system
# Instead, we'll use the classes already defined in the previous cell

def create_interface():
    # System initialization section
    def initialize_system(api_key_value):
        if not api_key_value or len(api_key_value) < 10:  # Basic validation
            return "Please enter a valid API key.", gr.update(visible=False), None

        try:
            # Test API connection
            test_service = LLMService(api_key_value)
            test_response = test_service.get_completion("Say hello")

            if len(test_response) > 0:
                learning_system = LearningSystem(api_key_value)
                return "✅ System initialized successfully! You can now use the learning system.", gr.update(visible=True), learning_system
            else:
                return "❌ API connection test failed. Please check your API key.", gr.update(visible=False), None
        except Exception as e:
            return f"❌ Error initializing system: {str(e)}", gr.update(visible=False), None

    with gr.Blocks(title="AI-Powered Python Learning System", theme="soft") as interface:
        # Store learning system state
        learning_system_state = gr.State(None)

        # Header
        gr.Markdown(
            """
            <div style="text-align: center; margin-bottom: 20px;">
                <h1 style="color: #4a69bd; font-size: 2.5em;">AI-Powered Python Learning System</h1>
                <p style="font-size: 1.2em; color: #444;">Master Python programming with personalized AI tutoring</p>
            </div>
            """
        )

        # API Key input - outside the tabs
        with gr.Row():
            # Try to get API key from environment variable
            API_KEY = os.environ.get("GROQ_API_KEY", "")
            api_key_input = gr.Textbox(
                label="Enter your Groq API Key",
                placeholder="gsk_...",
                type="password",
                value=API_KEY  # Use environment variable if available
            )
            init_btn = gr.Button("Initialize System", variant="primary")

        init_status = gr.Markdown("Enter your Groq API key and click 'Initialize System' to begin.")

        # Main interface container - hidden until initialized
        with gr.Column(visible=False) as main_interface:
            with gr.Tabs() as tabs:
                # Content & Learning tab
                with gr.Tab("Content & Learning"):
                    with gr.Row():
                        day_display = gr.Markdown("## Current Day: 1")

                    with gr.Row():
                        generate_content_btn = gr.Button("Generate Today's Content", variant="primary")
                        next_day_btn = gr.Button("Advance to Next Day", variant="secondary")

                    content_display = gr.Markdown("Click 'Generate Today's Content' to begin.")

                # Exam tab
                with gr.Tab("Exam"):
                    with gr.Row():
                        start_exam_btn = gr.Button("Start Exam", variant="primary")

                    exam_display = gr.Markdown("Click 'Start Exam' to begin the assessment.")

                    with gr.Row():
                        exam_answers = gr.Textbox(
                            label="Enter your answers (separate each answer with two line breaks)",
                            placeholder="Answer 1\n\nAnswer 2\n\nAnswer 3...",
                            lines=15
                        )

                    submit_exam_btn = gr.Button("Submit Exam", variant="primary")

                    exam_feedback = gr.Markdown("Your exam results will appear here.")

                # Q&A Sandbox tab
                with gr.Tab("Q&A Sandbox"):
                    with gr.Row():
                        question_input = gr.Textbox(
                            label="Ask any question about Python",
                            placeholder="Enter your question here...",
                            lines=3
                        )

                    ask_btn = gr.Button("Ask Question", variant="primary")

                    answer_display = gr.Markdown("Ask a question to get started.")

                # Progress Report tab
                with gr.Tab("Progress Report"):
                    with gr.Row():
                        report_btn = gr.Button("Generate Progress Report", variant="primary")

                    progress_display = gr.Markdown("Click 'Generate Progress Report' to see your learning statistics.")

        # Custom functions to handle state
        def generate_content(learning_system):
            if not learning_system:
                return "Please initialize the system first."
            return learning_system.generate_day_content()

        def advance_day(learning_system):
            if not learning_system:
                return "Please initialize the system first.", "## Current Day: 1"
            result = learning_system.advance_to_next_day()
            return result, f"## Current Day: {learning_system.current_day}"

        def start_exam(learning_system):
            if not learning_system:
                return "Please initialize the system first."
            try:
                exam_content = learning_system.start_exam()
                return exam_content
            except Exception as e:
                return f"Error starting exam: {str(e)}"

        def submit_exam(learning_system, answers):
            if not learning_system:
                return "Please initialize the system first."
            if not answers.strip():
                return "Please provide answers before submitting."

            try:
                feedback = learning_system.submit_exam(answers)
                return feedback
            except Exception as e:
                return f"Error evaluating exam: {str(e)}"

        def ask_question(learning_system, question):
            if not learning_system:
                return "Please initialize the system first."
            if not question.strip():
                return "Please enter a question."

            try:
                answer = learning_system.answer_sandbox_question(question)
                return answer
            except Exception as e:
                return f"Error processing question: {str(e)}"

        def generate_progress_report(learning_system):
            if not learning_system:
                return "Please initialize the system first."

            try:
                report = learning_system.get_learning_progress()
                return report
            except Exception as e:
                return f"Error generating progress report: {str(e)}"

        # Set up event handlers
        init_btn.click(
            initialize_system,
            inputs=[api_key_input],
            outputs=[init_status, main_interface, learning_system_state]
        )

        generate_content_btn.click(
            generate_content,
            inputs=[learning_system_state],
            outputs=[content_display]
        )

        next_day_btn.click(
            advance_day,
            inputs=[learning_system_state],
            outputs=[content_display, day_display]
        )

        start_exam_btn.click(
            start_exam,
            inputs=[learning_system_state],
            outputs=[exam_display]
        )

        submit_exam_btn.click(
            submit_exam,
            inputs=[learning_system_state, exam_answers],
            outputs=[exam_feedback]
        )

        ask_btn.click(
            ask_question,
            inputs=[learning_system_state, question_input],
            outputs=[answer_display]
        )

        report_btn.click(
            generate_progress_report,
            inputs=[learning_system_state],
            outputs=[progress_display]
        )

    return interface

# Create and launch the interface
# For Colab, make sure to install gradio first if you haven't
# !pip install gradio
interface = create_interface()
interface.launch(share=True)