File size: 31,331 Bytes
39fdb35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9f4704
 
 
 
 
 
 
 
 
39fdb35
 
a9f4704
39fdb35
 
 
 
 
a9f4704
 
 
 
39fdb35
a9f4704
39fdb35
 
a9f4704
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39fdb35
a9f4704
 
 
 
 
 
 
 
 
 
 
 
39fdb35
 
a9f4704
39fdb35
 
a9f4704
 
39fdb35
a9f4704
 
 
 
 
 
 
 
 
 
 
 
 
 
39fdb35
 
 
 
 
 
 
a9f4704
39fdb35
 
a9f4704
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39fdb35
a9f4704
 
 
 
 
 
39fdb35
 
 
 
 
 
 
 
 
 
a9f4704
39fdb35
a9f4704
 
 
 
39fdb35
 
a9f4704
 
 
 
39fdb35
a9f4704
39fdb35
a9f4704
 
 
 
 
 
39fdb35
a9f4704
 
 
 
 
 
39fdb35
a9f4704
 
39fdb35
 
a9f4704
 
39fdb35
 
a9f4704
 
 
 
 
 
 
 
 
 
 
39fdb35
a9f4704
39fdb35
 
 
a9f4704
 
 
39fdb35
a9f4704
 
39fdb35
a9f4704
 
39fdb35
 
 
a9f4704
 
 
39fdb35
 
a9f4704
39fdb35
 
 
a9f4704
39fdb35
 
a9f4704
 
39fdb35
a9f4704
39fdb35
a9f4704
 
 
39fdb35
 
 
 
a9f4704
 
39fdb35
 
a9f4704
 
39fdb35
 
 
a9f4704
39fdb35
a9f4704
39fdb35
a9f4704
 
39fdb35
a9f4704
 
39fdb35
 
 
 
a9f4704
 
39fdb35
 
a9f4704
 
39fdb35
 
 
 
a9f4704
 
 
 
39fdb35
 
 
a9f4704
 
 
39fdb35
a9f4704
39fdb35
 
a9f4704
 
 
 
39fdb35
a9f4704
 
39fdb35
a9f4704
 
 
39fdb35
 
 
a9f4704
39fdb35
a9f4704
39fdb35
 
 
 
a9f4704
 
39fdb35
a9f4704
 
39fdb35
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
# from fastapi import FastAPI, HTTPException, Request
# from fastapi.middleware.cors import CORSMiddleware
# from fastapi.responses import JSONResponse
# from pydantic import BaseModel
# import openai
# import os
# import json
# import re
# from typing import Dict, List, Optional, Tuple, Any

# app = FastAPI(title="TestCreationAgent", 
#               description="An API for collecting test creation parameters through conversation")

# # Add CORS middleware to allow requests from frontend
# app.add_middleware(
#     CORSMiddleware,
#     allow_origins=["*"],  # Allows all origins
#     allow_credentials=True,
#     allow_methods=["*"],  # Allows all methods
#     allow_headers=["*"],  # Allows all headers
# )

# # Define subject chapters mapping
# SUBJECT_CHAPTERS = {
#     "Mathematics": [
#         "Number Systems", "Polynomials", "Coordinate Geometry", "Linear Equations in Two Variables",
#         "Introduction to Euclid's Geometry", "Lines and Angles", "Triangles", "Quadrilaterals",
#         "Areas of Parallelograms and Triangles", "Circles", "Constructions", "Heron's Formula",
#         "Surface Areas and Volumes", "Statistics", "Probability", "Real Numbers",
#         "Pair of Linear Equations in Two Variables", "Quadratic Equations", "Arithmetic Progressions",
#         "Introduction to Trigonometry", "Some Applications of Trigonometry", "Areas Related to Circles",
#         "Sets", "Relations and Functions", "Trigonometric Functions", "Principle of Mathematical Induction",
#         "Complex Numbers and Quadratic Equations", "Linear Inequalities", "Permutations and Combinations",
#         "Binomial Theorem", "Sequences and Series", "Straight Lines", "Conic Sections",
#         "Introduction to Three Dimensional Geometry", "Limits and Derivatives",
#         "Inverse Trigonometric Functions", "Matrices", "Determinants",
#         "Continuity and Differentiability", "Application of Derivatives", "Integrals",
#         "Application of Integrals", "Differential Equations", "Vector Algebra",
#         "Three Dimensional Geometry", "Linear Programming"
#     ],
#     "Physics": [
#         "Motion", "Force and Laws of Motion", "Gravitation", "Work and Energy", "Sound",
#         "Light: Reflection and Refraction", "Human Eye and Colourful World", "Electricity",
#         "Magnetic Effects of Electric Current", "Physical World and Measurement", "Kinematics",
#         "Laws of Motion", "Work, Energy and Power", "Motion of System of Particles and Rigid Body",
#         "Properties of Bulk Matter", "Thermodynamics", "Behaviour of Perfect Gases and Kinetic Theory",
#         "Oscillations and Waves", "Electrostatics", "Current Electricity",
#         "Magnetic Effects of Current and Magnetism", "Electromagnetic Induction and Alternating Currents",
#         "Electromagnetic Waves", "Optics", "Dual Nature of Radiation and Matter", "Atoms", "Nuclei",
#         "Semiconductor Electronics: Materials, Devices and Simple Circuits", "Vectors"
#     ],
#     "Chemistry": [
#         "Matter in Our Surroundings", "Is Matter Around Us Pure?", "Atoms and Molecules",
#         "Structure of the Atom", "Chemical Reactions and Equations", "Acids, Bases and Salts",
#         "Metals and Non-metals", "Carbon and Its Compounds", "Periodic Classification of Elements",
#         "Some Basic Concepts of Chemistry", "Structure of Atom",
#         "Classification of Elements and Periodicity in Properties",
#         "Chemical Bonding and Molecular Structure", "States of Matter: Gases and Liquids",
#         "Thermodynamics", "Equilibrium", "Redox Reactions",
#         "Organic Chemistry: Some Basic Principles and Techniques", "Hydrocarbons",
#         "Environmental Chemistry", "Solid State", "Solutions", "Electrochemistry",
#         "Chemical Kinetics", "Surface Chemistry", "General Principles and Processes of Isolation of Elements",
#         "p-Block Elements", "d- and f-Block Elements", "Coordination Compounds",
#         "Haloalkanes and Haloarenes", "Alcohols, Phenols and Ethers",
#         "Aldehydes, Ketones and Carboxylic Acids", "Amines", "Biomolecules", "Polymers",
#         "Chemistry in Everyday Life"
#     ],
#     "Organic Chemistry": [
#         "Organic Chemistry: Some Basic Principles and Techniques", "Hydrocarbons",
#         "Haloalkanes and Haloarenes", "Alcohols, Phenols and Ethers",
#         "Aldehydes, Ketones and Carboxylic Acids", "Amines", "Biomolecules",
#         "Polymers", "Chemistry in Everyday Life"
#     ],
#     "Inorganic Chemistry": [
#         "Classification of Elements and Periodicity in Properties",
#         "Chemical Bonding and Molecular Structure", "Redox Reactions",
#         "p-Block Elements", "d- and f-Block Elements", "Coordination Compounds"
#     ]
# }

# # Create a flat mapping of misspelled/approximate chapter names to correct ones
# CHAPTER_MAPPING = {}
# for subject, chapters in SUBJECT_CHAPTERS.items():
#     for chapter in chapters:
#         # Add the correct chapter name
#         CHAPTER_MAPPING[chapter.lower()] = (subject, chapter)

#         # Add common misspellings/variations
#         if chapter.lower() == "thermodynamics":
#             CHAPTER_MAPPING["termodyanamics"] = (subject, chapter)
#             CHAPTER_MAPPING["termodyn"] = (subject, chapter)
#             CHAPTER_MAPPING["thermo"] = (subject, chapter)
#             CHAPTER_MAPPING["thermodynamic"] = (subject, chapter)


# class UserInput(BaseModel):
#     message: str
#     session_id: str


# class SessionState(BaseModel):
#     params: Dict[str, str] = {
#         "chapters_of_the_test": "",
#         "questions_per_chapter": "",
#         "difficulty_distribution": "",
#         "test_duration": "",
#         "test_date": "",
#         "test_time": ""
#     }
#     completed: bool = False
#     attempt_count: int = 0


# # In-memory session storage
# sessions = {}


# def normalize_chapter_name(chapter_input: str) -> Optional[Tuple[str, str]]:
#     """
#     Maps user input to standardized chapter names from the curriculum.
#     Returns tuple of (subject, correct_chapter_name) or None if no match.
#     """
#     if not chapter_input:
#         return None

#     # Direct mapping for exact matches or known misspellings
#     norm_input = chapter_input.lower().strip()
#     if norm_input in CHAPTER_MAPPING:
#         return CHAPTER_MAPPING[norm_input]

#     # Try fuzzy matching if no direct match
#     # Look for partial matches
#     for chapter_key, (subject, correct_name) in CHAPTER_MAPPING.items():
#         if norm_input in chapter_key or chapter_key in norm_input:
#             return (subject, correct_name)

#     # No match found
#     return None


# async def llm_extractParams(user_input: str, current_params: Dict[str, str]) -> Dict[str, str]:
#     """
#     Extracts structured test parameters from natural language input
#     and updates the provided params dictionary.
#     """
#     system_prompt = """
# You are an expert educational test creation assistant that extracts test setup parameters from user input.
# Extract ONLY the parameters explicitly mentioned in the user's message.

# Return a JSON object with all the following keys:
# - chapters_of_the_test (string: list of chapters or topics)
# - questions_per_chapter (string or number: how many questions per chapter)
# - difficulty_distribution (string: e.g., "easy:40%, medium:40%, hard:20%" or any format specified)
# - test_duration (string or number: time in minutes)
# - test_date (string: in any reasonable date format)
# - test_time (string: time of day)

# Important rules:
# - Do NOT make assumptions - if information isn't provided, leave as empty string ("")
# - Only fill in values explicitly mentioned by the user
# - For difficulty_distribution:
#   * Convert numeric sequences like "30 40 30" to "easy:30%, medium:40%, hard:30%" if they appear to be distributions
#   * Convert descriptions like "mostly hard" to approximate percentages (e.g., "easy:20%, medium:20%, hard:60%")
#   * Accept formats like "60 easy, 20 medium, 20 hard" and convert to percentages
# - Return valid JSON with all keys, even if empty
# """
#     messages = [
#         {"role": "system", "content": system_prompt},
#         {"role": "user", "content": user_input}
#     ]

#     try:
#         response = openai.chat.completions.create(
#             model="gpt-4o-mini",
#             messages=messages,
#             temperature=0.2
#         )

#         extracted_json = response.choices[0].message.content.strip()

#         # Handle potential JSON formatting issues by extracting JSON from response
#         if not extracted_json.startswith('{'):
#             # Find JSON object in text if it's not a clean JSON response
#             start_idx = extracted_json.find('{')
#             end_idx = extracted_json.rfind('}') + 1
#             if start_idx >= 0 and end_idx > start_idx:
#                 extracted_json = extracted_json[start_idx:end_idx]
#             else:
#                 raise ValueError("Unable to extract valid JSON from response")

#         # Parse and update the current_params safely
#         extracted_dict = json.loads(extracted_json)
#         updated_params = current_params.copy()
        
#         for key in updated_params:
#             if key.lower() in extracted_dict and extracted_dict[key.lower()]:
#                 updated_params[key] = extracted_dict[key.lower()]
#             elif key in extracted_dict and extracted_dict[key]:
#                 updated_params[key] = extracted_dict[key]

#         # Apply chapter mapping if chapters were specified
#         if updated_params["chapters_of_the_test"] and updated_params["chapters_of_the_test"] != current_params["chapters_of_the_test"]:
#             chapters_input = updated_params["chapters_of_the_test"]
#             # Split multiple chapters if comma-separated
#             chapter_list = [ch.strip() for ch in re.split(r',|;', chapters_input)]

#             mapped_chapters = []
#             for chapter in chapter_list:
#                 result = normalize_chapter_name(chapter)
#                 if result:
#                     subject, correct_name = result
#                     mapped_chapters.append(f"{correct_name} ({subject})")
#                 else:
#                     mapped_chapters.append(chapter)  # Keep as-is if no mapping found

#             updated_params["chapters_of_the_test"] = ", ".join(mapped_chapters)

#         return updated_params

#     except json.JSONDecodeError as e:
#         print(f"Error: Could not parse response as JSON: {e}")
#         return current_params
#     except Exception as e:
#         print(f"Error during parameter extraction: {e}")
#         return current_params


# def gate(params: Dict[str, str]) -> List[str]:
#     """
#     Checks which fields are still empty in the params.
#     Returns a list of missing parameter keys.
#     """
#     return [key for key, val in params.items() if not val]


# async def llm_getMissingParams(missing_keys: List[str]) -> str:
#     """
#     Generates a human-readable prompt to ask user for missing fields.
#     """
#     # Create context-aware prompts for specific missing fields
#     context_details = {
#         "chapters_of_the_test": "such as Math, Science, History, etc.",
#         "questions_per_chapter": "the number of questions for each chapter",
#         "difficulty_distribution": "as percentages or numbers (easy, medium, hard)",
#         "test_duration": "in minutes",
#         "test_date": "when the test will be given",
#         "test_time": "the time of day for the test"
#     }

#     # Create a more specific prompt based on what's missing
#     if len(missing_keys) == 1:
#         key = missing_keys[0]
#         prompt = f"Please provide the {key.replace('_', ' ')} {context_details.get(key, '')}."
#     else:
#         formatted_missing = [f"{key.replace('_', ' ')} ({context_details.get(key, '')})" for key in missing_keys]
#         prompt = f"The following test details are still needed: {', '.join(formatted_missing)}."

#     messages = [
#         {"role": "system", "content": "You are a helpful assistant who creates clear, concise questions to collect missing test setup information. Keep your response under 2 sentences and focus only on what's missing."},
#         {"role": "user", "content": prompt}
#     ]

#     try:
#         response = openai.chat.completions.create(
#             model="gpt-4o-mini",
#             messages=messages,
#             temperature=0.3
#         )
#         return response.choices[0].message.content.strip()
#     except Exception as e:
#         print(f"Error generating prompt for missing values: {e}")
#         return f"Please provide the following missing information: {', '.join(missing_keys)}."


# @app.on_event("startup")
# async def startup_event():
#     # Set up OpenAI API key from environment variable
#     openai.api_key = os.getenv("OPENAI_API_KEY")
#     if not openai.api_key:
#         print("⚠️ WARNING: OPENAI_API_KEY environment variable not set.")


# @app.get("/")
# async def root():
#     return {"message": "Test Creation Agent API is running"}


# @app.post("/chat")
# async def chat(user_input: UserInput):
#     session_id = user_input.session_id
    
#     # Initialize session if it doesn't exist
#     if session_id not in sessions:
#         sessions[session_id] = SessionState()
    
#     session = sessions[session_id]
    
#     # If this is the first message, send a welcome message
#     if session.attempt_count == 0:
#         session.attempt_count += 1
#         return {
#             "response": "πŸ‘‹ Welcome! Please provide the test setup details. I need: chapters, questions per chapter, difficulty distribution, test duration, date, and time.",
#             "session_state": {
#                 "params": session.params,
#                 "completed": False
#             }
#         }
    
#     # Process user input to extract parameters
#     session.params = await llm_extractParams(user_input.message, session.params)
#     session.attempt_count += 1
    
#     # Check if we have all required parameters
#     missing = gate(session.params)
    
#     # If we have all parameters or exceeded max attempts, return completion
#     max_attempts = 10
#     if not missing or session.attempt_count > max_attempts:
#         session.completed = True
#         if not missing:
#             result = "βœ… All test parameters are now complete:"
#         else:
#             result = "⚠️ Some parameters could not be filled after multiple attempts:"
        
#         # Format the parameters as a readable string
#         for k, v in session.params.items():
#             result += f"\n- {k.replace('_', ' ').title()}: {v or 'Not provided'}"
        
#         return {
#             "response": result,
#             "session_state": {
#                 "params": session.params,
#                 "completed": True
#             }
#         }
    
#     # Otherwise, ask for missing parameters
#     follow_up_prompt = await llm_getMissingParams(missing)
    
#     return {
#         "response": follow_up_prompt,
#         "session_state": {
#             "params": session.params,
#             "completed": False
#         }
#     }


# @app.get("/session/{session_id}")
# async def get_session(session_id: str):
#     if session_id not in sessions:
#         raise HTTPException(status_code=404, detail="Session not found")
    
#     session = sessions[session_id]
#     return {
#         "params": session.params,
#         "completed": session.completed,
#         "attempt_count": session.attempt_count
#     }


# @app.delete("/session/{session_id}")
# async def delete_session(session_id: str):
#     if session_id in sessions:
#         del sessions[session_id]
#     return {"message": "Session deleted successfully"}


# @app.post("/reset")
# async def reset_session(user_input: UserInput):
#     session_id = user_input.session_id
#     sessions[session_id] = SessionState()
    
#     return {
#         "response": "Session reset. πŸ‘‹ Welcome! Please provide the test setup details. I need: chapters, questions per chapter, difficulty distribution, test duration, date, and time.",
#         "session_state": {
#             "params": sessions[session_id].params,
#             "completed": False
#         }
#     }


# if __name__ == "__main__":
#     import uvicorn
#     uvicorn.run("app:app", host="0.0.0.0", port=int(os.getenv("PORT", 8000)), reload=True)

from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel
import openai
import os
import json
import re
from typing import Dict, List, Optional, Tuple, Any
import uuid
from datetime import datetime, timedelta

app = FastAPI(
    title="TestCreationAgent",
    description="An API for collecting test creation parameters through conversation",
    version="1.0.0"
)

# Add CORS middleware to allow requests from frontend
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Define subject chapters mapping
SUBJECT_CHAPTERS = {
    "Mathematics": [
        "Number Systems", "Polynomials", "Coordinate Geometry", "Linear Equations in Two Variables",
        "Introduction to Euclid's Geometry", "Lines and Angles", "Triangles", "Quadrilaterals",
        "Areas of Parallelograms and Triangles", "Circles", "Constructions", "Heron's Formula",
        "Surface Areas and Volumes", "Statistics", "Probability", "Real Numbers",
        "Pair of Linear Equations in Two Variables", "Quadratic Equations", "Arithmetic Progressions",
        "Introduction to Trigonometry", "Some Applications of Trigonometry", "Areas Related to Circles",
        "Sets", "Relations and Functions", "Trigonometric Functions", "Principle of Mathematical Induction",
        "Complex Numbers and Quadratic Equations", "Linear Inequalities", "Permutations and Combinations",
        "Binomial Theorem", "Sequences and Series", "Straight Lines", "Conic Sections",
        "Introduction to Three Dimensional Geometry", "Limits and Derivatives",
        "Inverse Trigonometric Functions", "Matrices", "Determinants",
        "Continuity and Differentiability", "Application of Derivatives", "Integrals",
        "Application of Integrals", "Differential Equations", "Vector Algebra",
        "Three Dimensional Geometry", "Linear Programming"
    ],
    "Physics": [
        "Motion", "Force and Laws of Motion", "Gravitation", "Work and Energy", "Sound",
        "Light: Reflection and Refraction", "Human Eye and Colourful World", "Electricity",
        "Magnetic Effects of Electric Current", "Physical World and Measurement", "Kinematics",
        "Laws of Motion", "Work, Energy and Power", "Motion of System of Particles and Rigid Body",
        "Properties of Bulk Matter", "Thermodynamics", "Behaviour of Perfect Gases and Kinetic Theory",
        "Oscillations and Waves", "Electrostatics", "Current Electricity",
        "Magnetic Effects of Current and Magnetism", "Electromagnetic Induction and Alternating Currents",
        "Electromagnetic Waves", "Optics", "Dual Nature of Radiation and Matter", "Atoms", "Nuclei",
        "Semiconductor Electronics: Materials, Devices and Simple Circuits", "Vectors"
    ],
    "Chemistry": [
        "Matter in Our Surroundings", "Is Matter Around Us Pure?", "Atoms and Molecules",
        "Structure of the Atom", "Chemical Reactions and Equations", "Acids, Bases and Salts",
        "Metals and Non-metals", "Carbon and Its Compounds", "Periodic Classification of Elements",
        "Some Basic Concepts of Chemistry", "Structure of Atom",
        "Classification of Elements and Periodicity in Properties",
        "Chemical Bonding and Molecular Structure", "States of Matter: Gases and Liquids",
        "Thermodynamics", "Equilibrium", "Redox Reactions",
        "Organic Chemistry: Some Basic Principles and Techniques", "Hydrocarbons",
        "Environmental Chemistry", "Solid State", "Solutions", "Electrochemistry",
        "Chemical Kinetics", "Surface Chemistry", "General Principles and Processes of Isolation of Elements",
        "p-Block Elements", "d- and f-Block Elements", "Coordination Compounds",
        "Haloalkanes and Haloarenes", "Alcohols, Phenols and Ethers",
        "Aldehydes, Ketones and Carboxylic Acids", "Amines", "Biomolecules", "Polymers",
        "Chemistry in Everyday Life"
    ],
    "Organic Chemistry": [
        "Organic Chemistry: Some Basic Principles and Techniques", "Hydrocarbons",
        "Haloalkanes and Haloarenes", "Alcohols, Phenols and Ethers",
        "Aldehydes, Ketones and Carboxylic Acids", "Amines", "Biomolecules",
        "Polymers", "Chemistry in Everyday Life"
    ],
    "Inorganic Chemistry": [
        "Classification of Elements and Periodicity in Properties",
        "Chemical Bonding and Molecular Structure", "Redox Reactions",
        "p-Block Elements", "d- and f-Block Elements", "Coordination Compounds"
    ]
}

# Create a flat mapping of misspelled/approximate chapter names to correct ones
CHAPTER_MAPPING = {}
for subject, chapters in SUBJECT_CHAPTERS.items():
    for chapter in chapters:
        CHAPTER_MAPPING[chapter.lower()] = (subject, chapter)
        # Add common misspellings/variations
        if chapter.lower() == "thermodynamics":
            CHAPTER_MAPPING["termodyanamics"] = (subject, chapter)
            CHAPTER_MAPPING["termodyn"] = (subject, chapter)
            CHAPTER_MAPPING["thermo"] = (subject, chapter)
            CHAPTER_MAPPING["thermodynamic"] = (subject, chapter)

class UserInput(BaseModel):
    message: str
    session_id: Optional[str] = None

class SessionState(BaseModel):
    params: Dict[str, str] = {
        "chapters_of_the_test": "",
        "questions_per_chapter": "",
        "difficulty_distribution": "",
        "test_duration": "",
        "test_date": "",
        "test_time": ""
    }
    completed: bool = False
    attempt_count: int = 0
    created_at: datetime = datetime.utcnow()
    last_accessed: datetime = datetime.utcnow()

# In-memory session storage with automatic cleanup
sessions: Dict[str, SessionState] = {}

def normalize_chapter_name(chapter_input: str) -> Optional[Tuple[str, str]]:
    """Maps user input to standardized chapter names from the curriculum."""
    if not chapter_input:
        return None

    norm_input = chapter_input.lower().strip()
    if norm_input in CHAPTER_MAPPING:
        return CHAPTER_MAPPING[norm_input]

    # Try fuzzy matching if no direct match
    for chapter_key, (subject, correct_name) in CHAPTER_MAPPING.items():
        if norm_input in chapter_key or chapter_key in norm_input:
            return (subject, correct_name)

    return None

async def cleanup_sessions():
    """Remove sessions older than 24 hours"""
    now = datetime.utcnow()
    expired = [sid for sid, session in sessions.items() 
               if now - session.last_accessed > timedelta(hours=24)]
    for sid in expired:
        del sessions[sid]

async def llm_extract_params(user_input: str, current_params: Dict[str, str]) -> Dict[str, str]:
    """Extracts structured test parameters from natural language input."""
    system_prompt = """
You are an expert educational test creation assistant that extracts test setup parameters from user input.
Extract ONLY the parameters explicitly mentioned in the user's message.
Return a JSON object with all the following keys:
- chapters_of_the_test (string: list of chapters or topics)
- questions_per_chapter (string or number: how many questions per chapter)
- difficulty_distribution (string: e.g., "easy:40%, medium:40%, hard:20%" or any format specified)
- test_duration (string or number: time in minutes)
- test_date (string: in any reasonable date format)
- test_time (string: time of day)
Important rules:
- Do NOT make assumptions - if information isn't provided, leave as empty string ("")
- Only fill in values explicitly mentioned by the user
- For difficulty_distribution:
  * Convert numeric sequences like "30 40 30" to "easy:30%, medium:40%, hard:30%" if they appear to be distributions
  * Convert descriptions like "mostly hard" to approximate percentages (e.g., "easy:20%, medium:20%, hard:60%")
  * Accept formats like "60 easy, 20 medium, 20 hard" and convert to percentages
- Return valid JSON with all keys, even if empty
"""
    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": user_input}
    ]

    try:
        response = openai.ChatCompletion.create(
            model="gpt-4o-mini",
            messages=messages,
            temperature=0.2
        )

        extracted_json = response.choices[0].message.content.strip()
        
        # Safely parse the JSON response
        try:
            extracted_dict = json.loads(extracted_json)
        except json.JSONDecodeError:
            # Try to extract JSON from malformed response
            start = extracted_json.find('{')
            end = extracted_json.rfind('}') + 1
            if start >= 0 and end > start:
                extracted_dict = json.loads(extracted_json[start:end])
            else:
                raise ValueError("Invalid JSON response from LLM")

        updated_params = current_params.copy()
        
        for key in updated_params:
            if key in extracted_dict and extracted_dict[key]:
                updated_params[key] = str(extracted_dict[key])

        # Apply chapter mapping if chapters were specified
        if updated_params["chapters_of_the_test"] and updated_params["chapters_of_the_test"] != current_params["chapters_of_the_test"]:
            chapters_input = updated_params["chapters_of_the_test"]
            chapter_list = [ch.strip() for ch in re.split(r'[,;]', chapters_input)]
            mapped_chapters = []
            
            for chapter in chapter_list:
                result = normalize_chapter_name(chapter)
                if result:
                    subject, correct_name = result
                    mapped_chapters.append(f"{correct_name} ({subject})")
                else:
                    mapped_chapters.append(chapter)

            updated_params["chapters_of_the_test"] = ", ".join(mapped_chapters)

        return updated_params

    except Exception as e:
        print(f"Error during parameter extraction: {str(e)}")
        return current_params

def get_missing_params(params: Dict[str, str]) -> List[str]:
    """Returns list of keys with empty values."""
    return [key for key, val in params.items() if not val]

async def llm_generate_prompt(missing_keys: List[str]) -> str:
    """Generates a human-readable prompt to ask user for missing fields."""
    context_details = {
        "chapters_of_the_test": "such as Math, Science, History, etc.",
        "questions_per_chapter": "the number of questions for each chapter",
        "difficulty_distribution": "as percentages or numbers (easy, medium, hard)",
        "test_duration": "in minutes",
        "test_date": "when the test will be given",
        "test_time": "the time of day for the test"
    }

    if len(missing_keys) == 1:
        key = missing_keys[0]
        return f"Please provide the {key.replace('_', ' ')} {context_details.get(key, '')}."
    else:
        formatted_missing = [f"{key.replace('_', ' ')} ({context_details.get(key, '')})" 
                            for key in missing_keys]
        return f"Please provide: {', '.join(formatted_missing)}."

@app.on_event("startup")
async def startup_event():
    """Initialize the application."""
    openai.api_key = os.getenv("OPENAI_API_KEY")
    if not openai.api_key:
        raise RuntimeError("OPENAI_API_KEY environment variable not set")

@app.get("/")
async def health_check():
    """Health check endpoint."""
    return {"status": "healthy", "timestamp": datetime.utcnow().isoformat()}

@app.post("/chat")
async def chat(user_input: UserInput):
    """Main chat endpoint for test parameter collection."""
    await cleanup_sessions()
    
    # Create new session if none provided
    if not user_input.session_id or user_input.session_id not in sessions:
        session_id = str(uuid.uuid4())
        sessions[session_id] = SessionState()
    else:
        session_id = user_input.session_id
    
    session = sessions[session_id]
    session.last_accessed = datetime.utcnow()
    
    # Initial welcome message
    if session.attempt_count == 0:
        session.attempt_count += 1
        return {
            "response": "πŸ‘‹ Welcome! Let's set up your test. Please provide: chapters, questions per chapter, difficulty, duration, date, and time.",
            "session_id": session_id,
            "session_state": session.dict(),
            "completed": False
        }
    
    # Process user input
    session.params = await llm_extract_params(user_input.message, session.params)
    session.attempt_count += 1
    
    # Check for completion
    missing = get_missing_params(session.params)
    max_attempts = 8
    
    if not missing or session.attempt_count >= max_attempts:
        session.completed = True
        response = ["βœ… Test setup complete:" if not missing else "⚠️ Partial information collected:"]
        
        for k, v in session.params.items():
            response.append(f"- {k.replace('_', ' ').title()}: {v or 'Not provided'}")
        
        return {
            "response": "\n".join(response),
            "session_id": session_id,
            "session_state": session.dict(),
            "completed": True
        }
    
    # Ask for missing information
    follow_up = await llm_generate_prompt(missing)
    
    return {
        "response": follow_up,
        "session_id": session_id,
        "session_state": session.dict(),
        "completed": False
    }

@app.get("/session/{session_id}")
async def get_session(session_id: str):
    """Retrieve session state."""
    await cleanup_sessions()
    
    if session_id not in sessions:
        raise HTTPException(status_code=404, detail="Session not found")
    
    sessions[session_id].last_accessed = datetime.utcnow()
    return {
        "session_state": sessions[session_id].dict(),
        "completed": sessions[session_id].completed
    }

@app.delete("/session/{session_id}")
async def delete_session(session_id: str):
    """Delete a session."""
    if session_id in sessions:
        del sessions[session_id]
    return {"message": "Session deleted"}

@app.post("/reset")
async def reset_session(user_input: UserInput):
    """Reset a session."""
    if not user_input.session_id or user_input.session_id not in sessions:
        raise HTTPException(status_code=400, detail="Invalid session ID")
    
    sessions[user_input.session_id] = SessionState()
    return {
        "response": "Session reset. Please provide test details.",
        "session_id": user_input.session_id,
        "session_state": sessions[user_input.session_id].dict(),
        "completed": False
    }

# For Hugging Face Spaces deployment
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 8000)))