
    }h                     t    S SK r S SKrS SKrS SKJr  \" \5      R                  S-  rS rS r	S	S jr
S rS rg)
    N)Pathquestions_jsonc                     U R                  5       R                  SS5      R                  SS5      R                  SS5      $ )z\
Normalize concept name to match filename
Example: "Average and Mean" -> "average_and_mean"
 _' -)lowerreplace)concept_names    2C:\teacher-association - Copy\utils_json_loader.pynormalize_concept_namer   
   s;    
 ''S199$CKKCQTUU    c                     [        U5      n[        U R                  5       -  SU 3-  U S3-  nUR                  5       (       a  U$ [	        SU 35        g)z
Get filepath for a JSON question file

Args:
    stream: 'CBSE' or 'ICSE'
    grade: '5', '6', '7', etc.
    concept_name: 'Average and Mean', 'Whole Numbers', etc.

Returns:
    Path object or None if file doesn't exist
grade_z.jsonu   ⚠️ JSON file not found: N)r   QUESTIONS_BASE_DIRr   existsprint)streamgrader   normalized_conceptfilepaths        r   get_json_filepathr      s_     0=!FLLN2veW5EEK]J^^cHddH	(

34r   c           	      8   [        XU5      nU(       d  / $  [        USSS9 n[        R                  " U5      nSSS5        WR	                  S/ 5      n	U	(       d  [        SU 35        / $ U(       aE  U	 V
s/ s H8  oR	                  SS5      R                  5       UR                  5       :X  d  M6  U
PM:     n	n
U(       a(  U	 V
s/ s H  oR	                  S	5      U;  d  M  U
PM     n	n
[        R                  " U	5        / nU	 H(  n
[        X5      nU(       d  M  UR                  U5        M*     U(       a  USU $ U$ ! , (       d  f       GN= fs  sn
f s  sn
f ! [         a:  n[        S
U S[        U5       35        SSKnUR                  5         / s SnA$ SnAff = f)a_  
Load questions from JSON file in YOUR format

Args:
    stream: Board (CBSE/ICSE)
    grade: Grade level
    concept_name: Concept name
    difficulty: 'easy', 'medium', 'hard' or None (for all)
    count: Number of questions (None = all)
    exclude_ids: Set of question IDs to exclude

Returns:
    List of question dictionaries in REQUIRED format
rutf-8encodingN	questionsu   ⚠️ No questions found in 
difficultyr	   idu   ❌ Error loading z: r   )r   openjsonloadgetr   r   randomshuffleconvert_json_to_required_formatappend	Exceptionstr	traceback	print_exc)r   r   r   r!   countexclude_idsr   fdatar    qconverted_questionsconverted_qer-   s                  r   load_questions_from_jsonr7   &   sr    !=H	&(C'2a99Q<D 3 HH["-	1(<=I $-gIq|R1H1N1N1PT^TdTdTf1fIIg $-PIqtK1OIIP 	y! !A9!JK{#**;7  &v..""? 32 h Q$  "8*Bs1vh78		si   E D90E *E 55E.E4E EE#/E  E 7E 9
EE 
F/FFFc           
      B    U R                  S0 5      nUR                  SS5      UR                  SS5      UR                  SS5      UR                  SS5      /nU R                  SS5      n[        UR                  5       5      [        S5      -
  nU R                  SS5      UUUU R                  S	S
X5    S35      U R                  SS5      U R                  SS5      U R                  S5      S.nU$ ! [         a!  n[	        S[        U5       35         SnAgSnAff = f)a  
Convert YOUR JSON format to REQUIRED format

Input format:
{
  "id": 1,
  "difficulty": "easy",
  "question": "What is the average of 5 and 7?",
  "options": {"A": "5", "B": "6", "C": "7", "D": "8"},
  "correct_answer": "B"
}

Output format:
{
  "question": "What is the average of 5 and 7?",
  "options": ["5", "6", "7", "8"],
  "correct_answer": 1,
  "concept": "Average and Mean",
  "explanation": "...",
  "hint": "..."
}
optionsAr	   BCDcorrect_answerquestionexplanationzThe correct answer is .hintz(Think step by step and choose carefully.r!   mediumr"   )r?   r9   r>   conceptr@   rB   difficulty_levelr"   u   ❌ Error converting question: N)r&   ordupperr+   r   r,   )json_questionr   options_dictoptions_listcorrect_lettercorrect_index	convertedr6   s           r   r)   r)   b   s3   . $((B7 S"%S"%S"%S"%	
 '**+;SAN0023c#h> &))*b9#+#(,,]>TUaUpTqqr<st!%%f.XY - 1 1, I##D)	
	  /Ax89s   C0C3 3
D=DDc                    [        XU5      nU(       d  0 $  [        USSS9 n[        R                  " U5      nSSS5        WR	                  S/ 5      n0 nU H:  nUR	                  SS5      R                  5       n	UR	                  U	S5      S	-   Xy'   M<     U$ ! , (       d  f       Nd= f! [         a#  n
[        S
[        U
5       35        0 s Sn
A
$ Sn
A
ff = f)zj
Get count of questions by difficulty level

Returns:
    dict: {'easy': 150, 'medium': 150, 'hard': 100}
r   r   r   Nr    r!   rC   r      u    ❌ Error reading distribution: )	r   r#   r$   r%   r&   r   r+   r   r,   )r   r   r   r   r1   r2   r    distributionr3   r!   r6   s              r   get_difficulty_distributionrQ      s     !=H	(C'2a99Q<D 3 HH["-	A|X6<<>J'3'7'7
A'F'JL$   32  0Q9:	s4   B( BAB( 
B%!B( (
C2C
CC)NNN)r$   osr'   pathlibr   __file__parentr   r   r   r7   r)   rQ    r   r   <module>rW      sE     	   (^**-== V*:x7rr   