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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
from typing import List, Dict
import logging
import json
import re
import math
from json_repair import repair_json
from pydantic import parse_obj_as
from collections import defaultdict
from config import  get_settings
from routes.schemas.Exam_Models import *
from stores.llm.LLMProviderFactory import LLMProviderFactory
from generation.AssistantRagGenerator import ProviderLLMWrapper
from generation.prompts import ExamPromptBuilder
from indexing.indexingController import IndexingController

class ExamService:
    MAX_CHUNK_CHARS = 2000
    MAX_TOTAL_CONTEXT = 8000
    MAX_SCORE = 40
    PASS_THRESHOLD = int(MAX_SCORE * 0.8)
    MAX_GENERATION_ATTEMPTS = 3

    def __init__(self):
        self.logger = logging.getLogger(__name__)
        self._models_initialized = False
        self.settings=get_settings()
        self._init_models()
        self.prompts=ExamPromptBuilder()
        self.controller = IndexingController()
        self.store = self.controller.vector_store
        self.BATCH_SIZE=10
   
    def _init_models(self):
        if self._models_initialized:
            return
        factory = LLMProviderFactory(self.settings)
        self.generator = factory.create(self.settings.GENERATION_BACKEND)
        self.generator.set_generation_model(self.settings.GENERATION_MODEL_ID)
        self.embedding_provider = factory.create(self.settings.EMBEDDING_BACKEND)
        self.embedding_provider.set_embedding_model(
            self.settings.EMBEDDING_MODEL_ID,
            self.settings.EMBEDDING_MODEL_SIZE
        )
        self.llm = ProviderLLMWrapper(provider=self.generator)
        self._models_initialized = True
   
    def _extract_json(self, text: str) -> dict:
        """
        Extract the first valid JSON object from LLM output. Attempts to repair malformed JSON using `repair_json`.
        """
        match = re.search(r"\{.*\}", text, re.DOTALL)
        if not match:
            self.logger.error("No JSON found in LLM response:\n%s", text)
            raise ValueError("LLM returned no JSON")
        json_str = match.group(0)
        # Try to load directly
        try:
            return json.loads(json_str)
        except json.JSONDecodeError:
            self.logger.warning("Invalid JSON extracted, attempting repair...")
            try:
                repaired_str = repair_json(json_str)
                return json.loads(repaired_str)
            except Exception as e:
                self.logger.error("Failed to repair JSON:\n%s\nError: %s", json_str, e)
                raise

    def normalize_exam_dict(self, data: dict):
        # Normalize difficulty enum
        if "difficulty" in data:
            diff = data["difficulty"]
            if isinstance(diff, str):
                if "." in diff:
                    diff = diff.split(".")[-1]
                data["difficulty"] = diff.lower()
        # Normalize questions
        questions = data.get("questions")
        if not isinstance(questions, list):
            return data
        normalized_questions = []
        for q in questions:
            if not isinstance(q, dict):
                continue
            q.pop("id", None)
            q.pop("question_id", None)
            q.pop("points", None)

            # normalize type
            q_type = q.get("type")
            if isinstance(q_type, str):
                q_type = q_type.lower().strip()
                if q_type == "truefalse":
                    q_type = "true_false"
            q["type"] = q_type

            # normalize question text
            if "question" in q:
                q["question"] = str(q["question"]).strip()

            # MCQ normalization
            if q_type == "mcq":
                options = q.get("options")
                # dict -> list
                if isinstance(options, dict):
                    options = list(options.values())
                # string -> split into options
                elif isinstance(options, str):
                    parts = re.split(r"[A-D]\)|\n|\r", options)
                    options = [
                        p.strip(" .-")
                        for p in parts
                        if p.strip()
                    ]
                # ensure list[str]
                if isinstance(options, list):
                    options = [str(o).strip() for o in options]
                else:
                    options = []
                q["options"] = options

                # normalize correct answer
                correct = q.get("correct_answer")
                if correct is not None:
                    correct = str(correct).strip()
                    q["correct_answer"] = correct
                    # ensure correct answer exists in options
                    if correct not in q["options"]:
                        q["options"].append(correct)
                # ensure explanation exists
                q.setdefault("explanation", "")

            # True/False normalization
            elif q_type == "true_false":
                ans = q.get("correct_answer")
                if isinstance(ans, str):
                    ans = ans.lower()
                    if ans in ["true", "t", "1", "yes"]:
                        ans = True
                    elif ans in ["false", "f", "0", "no"]:
                        ans = False
                q["correct_answer"] = ans
                q.setdefault("explanation", "")

            # Short Answer normalization
            elif q_type == "short_answer":
                if "answer" in q:
                    q["answer"] = str(q["answer"]).strip()
                q.setdefault("explanation", "")

            # Essay normalization
            elif q_type == "essay":
                if "expected_keywords" in q:
                    keywords = q.pop("expected_keywords")
                    if isinstance(keywords, list):
                        q["answer_guidelines"] = ", ".join(keywords)
                    else:
                        q["answer_guidelines"] = str(keywords)
                q.setdefault("answer_guidelines", "")

            # Code question normalization
            elif q_type == "code":
                if "solution" in q:
                    q["solution"] = str(q["solution"])
                q.setdefault("starter_code", None)
                q.setdefault("explanation", "")
            normalized_questions.append(q)
        data["questions"] = normalized_questions

        return data
    
    def generate_exam(self, request: ExamGenerationRequest, context: str, llm, batch_size: int) -> List[QuestionUnion]:
        """
        Generate a batch of questions from the LLM, ensuring valid QuestionUnion objects.Repairs incomplete MCQs automatically.
        """
        # Prepare the prompt for the batch
        batch_request = request.model_copy()
        batch_request.total_questions = batch_size

        prompt = self.prompts.build_exam_generation_prompt(batch_request, context)
        raw_text = llm._call(prompt)

        if not raw_text:
            raise RuntimeError("LLM generation failed")

        cleaned = re.sub(r"```[a-zA-Z]*|```", "", raw_text).strip()

        try:
            exam_dict = self._extract_json(cleaned)
            exam_dict = self.normalize_exam_dict(exam_dict)

            questions = exam_dict.get("questions") or []
            questions = questions[:batch_size]

            # Repair incomplete MCQs or missing fields
            repaired_questions = []
            for q in questions:
                if not isinstance(q, dict):
                    continue  # skip invalid entries
                q_type = q.get("type")
                if q_type == "mcq":
                    if not q.get("options"):
                        self.logger.warning(f"Skipping MCQ with no options: {q}")
                        continue
                    if not q.get("correct_answer"):
                        q["correct_answer"] = q["options"][0]  # safe placeholder
                repaired_questions.append(q)

            # Convert to Pydantic QuestionUnion objects
            questions = parse_obj_as(List[QuestionUnion], repaired_questions)

            self.logger.info(
                "Batch requested=%d | received=%d | kept=%d",
                batch_size,
                len(exam_dict.get("questions", [])),
                len(questions),
            )

        except json.JSONDecodeError:
            self.logger.error("Invalid JSON from LLM:\n%s", raw_text)
            raise

        return questions

    def evaluate_exam(self, request: ExamGenerationRequest, exam: ExamResponse, llm):
        prompt = self.prompts.build_exam_evaluation_prompt(request, exam)
        raw_text = llm._call(prompt)

        if not raw_text:
            raise RuntimeError("Evaluation generation failed")

        cleaned = re.sub(r"```[a-zA-Z]*|```", "", raw_text).strip()

        try:
            evaluation_dict = self._extract_json(cleaned)
        except json.JSONDecodeError:
            self.logger.error("Invalid evaluation JSON:\n%s", raw_text)
            raise

        return EvaluationResult.model_validate(evaluation_dict)

    def split_chunks_by_topic_batches(self, exam_chunks, num_batches):

        self.logger.info(f"Topics retrieved: {list(exam_chunks.keys())}")
        self.logger.info(f"Number of batches: {num_batches}")

        batches = [[] for _ in range(num_batches)]

        for topic, chunks in exam_chunks.items():
            total_chunks = len(chunks)
            self.logger.info(f"Topic '{topic}' -> {total_chunks} chunks distributed across batches")

            for idx, chunk in enumerate(chunks):
                batch_index = idx % num_batches
                batches[batch_index].append(chunk)

        # Log batch composition
        for i, batch in enumerate(batches):
            topic_counter = defaultdict(int)
            for chunk in batch:
                topic = chunk.get("metadata", {}).get("topic", "unknown")
                topic_counter[topic] += 1
            self.logger.info(f"Batch {i+1} contains {len(batch)} chunks -> {dict(topic_counter)}")

        return batches


    def exam_task(self, request_dict: dict) -> ExamResponse:
        """
        Generate a full exam using batching, safety break, and validated QuestionUnion questions.Each batch receives a portion of the retrieved chunks.
        """
        request = ExamGenerationRequest.model_validate(request_dict)
        # Prepare context from knowledge store
        topics_with_embeddings = self.prepare_topics_with_embeddings(request.topics)
        exam_chunks = self.store.retrieve_for_exam(topics_with_embeddings,request.username,request.course,request.references)

        # Determine number of batches
        num_batches = math.ceil(request.total_questions / self.BATCH_SIZE)
        self.logger.info(f"Raw exam_chunks structure: {type(exam_chunks)}")

        for k, v in exam_chunks.items():
            self.logger.info(f"Topic={k} | type={type(v)} | len={len(v) if hasattr(v,'__len__') else 'NA'}")
        
        chunk_batches = self.split_chunks_by_topic_batches(exam_chunks,num_batches)

        feedback_context = ""

        best_exam = None
        best_score = 0

        for attempt in range(self.MAX_GENERATION_ATTEMPTS):
            self.logger.info(f"Generating exam attempt {attempt+1}")

            remaining_distribution: Dict[QuestionType, int] = dict(request.question_types_distribution)
            all_questions: List[QuestionUnion] = []
            batch_index = 0

            # Batch generation loop
            while len(all_questions) < request.total_questions:
                remaining = request.total_questions - len(all_questions)
                batch_size = min(self.BATCH_SIZE, remaining)
                # Determine batch distribution
                batch_distribution: Dict[QuestionType, int] = {}
                slots_left = batch_size

                for qtype, count in remaining_distribution.items():
                    if slots_left <= 0:
                        break

                    take = min(count, slots_left)

                    if take > 0:
                        batch_distribution[qtype] = take
                        slots_left -= take

                if not batch_distribution:
                    break

                batch_request = request.model_copy()
                batch_request.total_questions = sum(batch_distribution.values())
                batch_request.question_types_distribution = batch_distribution

                # Select chunk subset for this batch

                chunk_subset = chunk_batches[batch_index % len(chunk_batches)]
                self.logger.info(f"\n===== BATCH {batch_index+1} CHUNKS =====")

                for i, chunk in enumerate(chunk_subset):

                    meta = chunk.get("metadata", {})
                    topic = meta.get("topic", "unknown")
                    page = meta.get("page", "NA")

                    # Try common text keys
                    text = chunk.get("text") or chunk.get("content") or chunk.get("page_content") or ""

                    preview = text[:200].replace("\n", " ")

                    self.logger.info(
                        f"Chunk {i+1} | Topic={topic} | Page={page} | Preview={preview}"
                    )

                self.logger.info("=====================================\n")

                batch_index += 1

                batch_context = self.build_exam_context(chunk_subset)

                if feedback_context:
                    batch_context += f"\n\nEvaluator Feedback:\n{feedback_context}"

                # Generate questions

                batch_questions = self.generate_exam(batch_request,batch_context,self.llm,batch_request.total_questions)

                # Filter generated questions
                for q in batch_questions:
                    if remaining_distribution.get(q.type, 0) > 0:
                        all_questions.append(q)
                        remaining_distribution[q.type] -= 1
                    if len(all_questions) >= request.total_questions:
                        break

            # Build final exam

            exam_dict = {
                "exam_id": request.exam_id,
                "difficulty": request.difficulty,
                "total_questions": request.total_questions,
                "expected_distribution": request.question_types_distribution,
                "questions": all_questions[:request.total_questions],
            }

            try:
                exam = ExamResponse.model_validate(exam_dict)
            except Exception as e:
                self.logger.error(f"Exam validation failed: {e}")
                raise

            evaluation = self.evaluate_exam(request, exam, self.llm)
            self.logger.info(f"Evaluation score: {evaluation.overall_score}")

            if evaluation.overall_score > best_score:
                best_score = evaluation.overall_score
                best_exam = exam

            if evaluation.overall_score >= self.PASS_THRESHOLD:
                break

            feedback_context = evaluation.feedback

        if best_exam is None:
            raise RuntimeError("Exam generation failed after retries")

        return best_exam



    def build_exam_context(self, exam_chunks) -> str:
        """
        Accepts either:
        1) {topic: [chunks]}
        2) [chunks]
        """

        # Normalize structure
        if isinstance(exam_chunks, list):
            topic_chunks = defaultdict(list)

            for c in exam_chunks:
                topic = c.get("metadata", {}).get("topic", "Unknown")
                topic_chunks[topic].append(c)

            exam_chunks = topic_chunks

        context_parts = []
        total_length = 0

        for topic, chunks in exam_chunks.items():

            topic_header = f"\n### Topic: {topic}\n"

            if total_length + len(topic_header) > self.MAX_TOTAL_CONTEXT:
                break

            context_parts.append(topic_header)
            total_length += len(topic_header)

            for c in chunks:

                text = c.get("payload", {}).get("text", "")
                source = c.get("metadata", {}).get("source", "")
                bookmark = c.get("metadata", {}).get("bookmark_path", "")

                if not isinstance(text, str):
                    continue

                if len(text) > self.MAX_CHUNK_CHARS:
                    text = text[:self.MAX_CHUNK_CHARS]

                formatted_chunk = (f"[Source: {source} | Bookmark: {bookmark}]\n{text}\n")

                if total_length + len(formatted_chunk) > self.MAX_TOTAL_CONTEXT:
                    break

                context_parts.append(formatted_chunk)
                total_length += len(formatted_chunk)

        return "\n".join(context_parts)


    def prepare_topics_with_embeddings(self, topics: List[str]):
        results = []
        for topic in topics:
            try:
                embedding = self.embedding_provider.embed_text(topic)
                results.append((topic, embedding))
            except Exception as e:
                self.logger.warning(f"Embedding failed for topic '{topic}': {e}")
        self.logger.info(f"Prepared {len(results)} topic embeddings")
        return results