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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Setup for MongoDB
from pymongo import MongoClient
from datetime import datetime
from werkzeug.security import generate_password_hash
import os
from dotenv import load_dotenv

load_dotenv()
MONGO_URI = os.getenv("MONGO_URI")

client = MongoClient(MONGO_URI)
try:
    client.admin.command("ping")
    print("MongoDB connection successful")
except Exception as e:
    print(f"MongoDB connection failed: {e}")

db = client["novascholar_db"]

########
# Research Assistant Schema
research_assistant_schema = {
    "bsonType": "object",
    "required": ["full_name", "password", "email", "courses_assisted"],
    "properties": {
        "full_name": {
            "bsonType": "string",
            "description": "Full name of the research assistant",
        },
        "password": {
            "bsonType": "string",
            "description": "Hashed password of the research assistant",
        },
        "email": {
            "bsonType": "string",
            "description": "Email address of the research assistant",
        },
        "courses_assisted": {
            "bsonType": "array",
            "description": "List of courses the research assistant is assisting",
            "items": {
                "bsonType": "object",
                "required": ["course_id"],
                "properties": {
                    "course_id": {
                        "bsonType": "string",
                        "description": "ID of the course",
                    }
                },
            },
        },
    },
}

# Create research assistants collection
research_assistants_collection = db["research_assistants"]

# Create indexes
research_assistants_collection.create_index("full_name", unique=True)
research_assistants_collection.create_index("email", unique=True)


# Optional: Sample data insertion function
def insert_sample_research_assistants():
    sample_research_assistants = [
        {
            "full_name": "John Doe RA",
            "password": generate_password_hash("password123"),
            "email": "[email protected]",
            "courses_assisted": [{"course_id": "CS101"}, {"course_id": "CS102"}],
        }
    ]

    try:
        research_assistants_collection.insert_many(sample_research_assistants)
        print("Sample research assistants inserted successfully!")
    except Exception as e:
        print(f"Error inserting sample research assistants: {e}")


###########

###############
# Add after research assistant schema

# Analyst Schema
analyst_schema = {
    "bsonType": "object",
    "required": ["full_name", "password", "email", "courses_analyzed"],
    "properties": {
        "full_name": {"bsonType": "string", "description": "Full name of the analyst"},
        "password": {
            "bsonType": "string",
            "description": "Hashed password of the analyst",
        },
        "email": {"bsonType": "string", "description": "Email address of the analyst"},
        "courses_analyzed": {
            "bsonType": "array",
            "description": "List of courses the analyst is analyzing",
            "items": {
                "bsonType": "object",
                "required": ["course_id"],
                "properties": {
                    "course_id": {
                        "bsonType": "string",
                        "description": "ID of the course",
                    }
                },
            },
        },
    },
}

# Create analysts collection
analysts_collection = db["analysts"]

# Create indexes for analysts
analysts_collection.create_index("full_name", unique=True)
analysts_collection.create_index("email", unique=True)


def insert_sample_analysts():
    sample_analysts = [
        {
            "full_name": "jane",
            "password": generate_password_hash("jane"),
            "email": "[email protected]",
            "courses_analyzed": [{"course_id": "CS101"}, {"course_id": "CS102"}],
        }
    ]

    try:
        analysts_collection.insert_many(sample_analysts)
        print("Sample analysts inserted successfully!")
    except Exception as e:
        print(f"Error inserting sample analysts: {e}")


##############@


# Define the course schema
course_schema = {
    "bsonType": "object",
    "required": [
        "course_id",
        "title",
        "description",
        "faculty",
        "faculty_id",
        "duration",
        "created_at",
    ],
    "properties": {
        "course_id": {
            "bsonType": "string",
            "description": "Unique identifier for the course",
        },
        "title": {"bsonType": "string", "description": "Title of the course"},
        "description": {
            "bsonType": "string",
            "description": "Description of the course",
        },
        "faculty": {"bsonType": "string", "description": "Name of the faculty"},
        "duration": {"bsonType": "string", "description": "Duration of the course"},
        "created_at": {
            "bsonType": "date",
            "description": "Date when the course was created",
        },
        "sessions": {
            "bsonType": "array",
            "description": "List of sessions associated with the course",
            "items": {
                "bsonType": "object",
                "required": ["session_id", "title", "date", "status", "created_at"],
                "properties": {
                    "session_id": {
                        "bsonType": "string",
                        "description": "Unique identifier for the session",
                    },
                    "title": {
                        "bsonType": "string",
                        "description": "Title of the session",
                    },
                    "date": {"bsonType": "date", "description": "Date of the session"},
                    "status": {
                        "bsonType": "string",
                        "description": "Status of the session (e.g., completed, upcoming)",
                    },
                    "created_at": {
                        "bsonType": "date",
                        "description": "Date when the session was created",
                    },
                    "pre_class": {
                        "bsonType": "object",
                        "description": "Pre-class segment data",
                        "properties": {
                            "resources": {
                                "bsonType": "array",
                                "description": "List of pre-class resources",
                                "items": {
                                    "bsonType": "object",
                                    "required": ["type", "title", "url"],
                                    "properties": {
                                        "type": {
                                            "bsonType": "string",
                                            "description": "Type of resource (e.g., pdf, video)",
                                        },
                                        "title": {
                                            "bsonType": "string",
                                            "description": "Title of the resource",
                                        },
                                        "url": {
                                            "bsonType": "string",
                                            "description": "URL of the resource",
                                        },
                                        "vector": {
                                            "bsonType": "array",
                                            "description": "Vector representation of the resource",
                                            "items": {"bsonType": "double"},
                                        },
                                    },
                                },
                            },
                            "completion_required": {
                                "bsonType": "bool",
                                "description": "Indicates if completion of pre-class resources is required",
                            },
                        },
                    },
                    "in_class": {
                        "bsonType": "object",
                        "description": "In-class segment data",
                        "properties": {
                            "topics": {
                                "bsonType": "array",
                                "description": "List of topics covered in the session",
                                "items": {"bsonType": "string"},
                            },
                            "quiz": {
                                "bsonType": "object",
                                "description": "Quiz data",
                                "properties": {
                                    "title": {
                                        "bsonType": "string",
                                        "description": "Title of the quiz",
                                    },
                                    "questions": {
                                        "bsonType": "int",
                                        "description": "Number of questions in the quiz",
                                    },
                                    "duration": {
                                        "bsonType": "int",
                                        "description": "Duration of the quiz in minutes",
                                    },
                                },
                            },
                            "polls": {
                                "bsonType": "array",
                                "description": "List of polls conducted during the session",
                                "items": {
                                    "bsonType": "object",
                                    "required": ["question", "options"],
                                    "properties": {
                                        "question": {
                                            "bsonType": "string",
                                            "description": "Poll question",
                                        },
                                        "options": {
                                            "bsonType": "array",
                                            "description": "List of poll options",
                                            "items": {"bsonType": "string"},
                                        },
                                        "responses": {
                                            "bsonType": "object",
                                            "description": "Responses to the poll",
                                            "additionalProperties": {"bsonType": "int"},
                                        },
                                    },
                                },
                            },
                        },
                    },
                    "post_class": {
                        "bsonType": "object",
                        "description": "Post-class segment data",
                        "properties": {
                            "assignments": {
                                "bsonType": "array",
                                "description": "List of assignments",
                                "items": {
                                    "bsonType": "object",
                                    "required": ["id", "title", "due_date", "status"],
                                    "properties": {
                                        "id": {
                                            "bsonType": "int",
                                            "description": "Assignment ID",
                                        },
                                        "title": {
                                            "bsonType": "string",
                                            "description": "Title of the assignment",
                                        },
                                        "due_date": {
                                            "bsonType": "date",
                                            "description": "Due date of the assignment",
                                        },
                                        "status": {
                                            "bsonType": "string",
                                            "description": "Status of the assignment (e.g., pending, completed)",
                                        },
                                        "submissions": {
                                            "bsonType": "array",
                                            "description": "List of submissions",
                                            "items": {
                                                "bsonType": "object",
                                                "required": [
                                                    "student_id",
                                                    "file_url",
                                                    "submitted_at",
                                                ],
                                                "properties": {
                                                    "student_id": {
                                                        "bsonType": "string",
                                                        "description": "ID of the student who submitted the assignment",
                                                    },
                                                    "file_url": {
                                                        "bsonType": "string",
                                                        "description": "URL of the submitted file",
                                                    },
                                                    "submitted_at": {
                                                        "bsonType": "date",
                                                        "description": "Date when the assignment was submitted",
                                                    },
                                                },
                                            },
                                        },
                                    },
                                },
                            }
                        },
                    },
                },
            },
        },
    },
}

# Create the collection with the schema
# db.create_collection("courses_collection2", validator={"$jsonSchema": course_schema})

# sample_course = {
#     "course_id": "CS101",
#     "title": "Introduction to Computer Science",
#     "description": "This course covers the basics of computer science and programming.",
#     "faculty": "Dr. John Doe",
#     "faculty_id": "F101",
#     "duration": "10 weeks",
#     "created_at": datetime.utcnow(),
#     "sessions": [
#         {
#             "session_id": "S101",
#             "title": "Introduction to Programming Fundamentals",
#             "date": datetime.utcnow() - timedelta(days=7),
#             "status": "completed",
#             "created_at": datetime.utcnow() - timedelta(days=7),
#             "pre_class": {
#                 "resources": [
#                     {
#                         "type": "pdf",
#                         "title": "Introduction to Python Basics",
#                         "url": "/assets/python_basics.pdf",
#                         "vector": [0.1, 0.2, 0.3]  # Example vector
#                     }
#                 ],
#                 "completion_required": True
#             },
#             "in_class": {
#                 "topics": ["Variables", "Data Types", "Basic Operations"],
#                 "quiz": {
#                     "title": "Python Basics Quiz",
#                     "questions": 5,
#                     "duration": 15
#                 },
#                 "polls": [
#                     {
#                         "question": "How comfortable are you with Python syntax?",
#                         "options": ["Very", "Somewhat", "Not at all"],
#                         "responses": {"Very": 10, "Somewhat": 5, "Not at all": 2}
#                     }
#                 ]
#             },
#             "post_class": {
#                 "assignments": [
#                     {
#                         "id": 1,
#                         "title": "Basic Python Programs",
#                         "due_date": datetime.utcnow() + timedelta(days=2),
#                         "status": "pending",
#                         "submissions": []
#                     }
#                 ]
#             }
#         },
#         {
#             "session_id": "S102",
#             "title": "Control Flow and Functions",
#             "date": datetime.utcnow() - timedelta(days=3),
#             "status": "completed",
#             "created_at": datetime.utcnow() - timedelta(days=3),
#             "pre_class": {
#                 "resources": [
#                     {
#                         "type": "pdf",
#                         "title": "Control Flow in Python",
#                         "url": "/assets/control_flow.pdf",
#                         "vector": [0.4, 0.5, 0.6]  # Example vector
#                     }
#                 ],
#                 "completion_required": True
#             },
#             "in_class": {
#                 "topics": ["If-else statements", "Loops", "Function definitions"],
#                 "quiz": {
#                     "title": "Control Flow Quiz",
#                     "questions": 8,
#                     "duration": 20
#                 },
#                 "polls": [
#                     {
#                         "question": "Which loop type do you find more intuitive?",
#                         "options": ["For loops", "While loops", "Both"],
#                         "responses": {"For loops": 12, "While loops": 8, "Both": 10}
#                     }
#                 ]
#             },
#             "post_class": {
#                 "assignments": [
#                     {
#                         "id": 2,
#                         "title": "Function Implementation Exercise",
#                         "due_date": datetime.utcnow() + timedelta(days=4),
#                         "status": "pending",
#                         "submissions": []
#                     }
#                 ]
#             }
#         }
#     ]
# }
courses_collection2 = db["courses_collection2"]


# Define the users schema
users_schema = {
    "bsonType": "object",
    "required": ["user_id", "username", "password", "role", "created_at"],
    "properties": {
        "user_id": {
            "bsonType": "string",
            "description": "Unique identifier for the user",
        },
        "username": {"bsonType": "string", "description": "Name of the User"},
        "password": {"bsonType": "string", "description": "Password of the user"},
        "role": {
            "bsonType": "string",
            "description": "Type of user (e.g., student, faculty)",
        },
        "created_at": {
            "bsonType": "date",
            "description": "Date when the user was created",
        },
    },
}
# Create the collection with the schema
# db.create_collection("users", validator={"$jsonSchema": users_schema})
users_collection = db["users"]


# Defining the Student Collection
student_schema = {
    "bsonType": "object",
    "required": ["SID", "full_name", "password", "enrolled_courses", "created_at"],
    "properties": {
        "SID": {
            "bsonType": "string",
            "description": "Unique identifier for the student",
        },
        "full_name": {"bsonType": "string", "description": "Full name of the student"},
        "password": {
            "bsonType": "string",
            "description": "Hashed password of the student",
        },
        "enrolled_courses": {
            "bsonType": "array",
            "description": "List of courses the student is enrolled in",
            "items": {
                "bsonType": "object",
                "required": ["course_id", "title"],
                "properties": {
                    "course_id": {
                        "bsonType": "string",
                        "description": "Unique identifier for the course",
                    },
                    "title": {
                        "bsonType": "string",
                        "description": "Title of the course",
                    },
                },
            },
        },
        "created_at": {
            "bsonType": "date",
            "description": "Date when the student was created",
        },
    },
}
# Defining the Faculty Collection
faculty_schema = {
    "bsonType": "object",
    "required": ["TID", "full_name", "password", "courses_taught", "created_at"],
    "properties": {
        "TID": {
            "bsonType": "string",
            "description": "Unique identifier for the faculty",
        },
        "full_name": {"bsonType": "string", "description": "Full name of the faculty"},
        "password": {
            "bsonType": "string",
            "description": "Hashed password of the faculty",
        },
        "courses_taught": {
            "bsonType": "array",
            "description": "List of courses the faculty is teaching",
            "items": {
                "bsonType": "object",
                "required": ["course_id", "title"],
                "properties": {
                    "course_id": {
                        "bsonType": "string",
                        "description": "Unique identifier for the course",
                    },
                    "title": {
                        "bsonType": "string",
                        "description": "Title of the course",
                    },
                },
            },
        },
        "created_at": {
            "bsonType": "date",
            "description": "Date when the faculty was created",
        },
    },
}
# Creating the Collections
# db.create_collection("students", validator={"$jsonSchema": student_schema})
# db.create_collection("faculty", validator={"$jsonSchema": faculty_schema})

students_collection = db["students"]
faculty_collection = db["faculty"]

# Defining the Vector Collection Schema
vector_schema = {
    "bsonType": "object",
    "required": ["resource_id", "vector"],
    "properties": {
        "resource_id": {
            "bsonType": "objectId",
            "description": "Unique identifier for the resource",
        },
        "vector": {
            "bsonType": "array",
            "description": "Vector representation of the resource",
            "items": {"bsonType": "double"},
        },
        "text": {"bsonType": "string", "description": "Text content of the resource"},
        "created_at": {
            "bsonType": "date",
            "description": "Date when the vector was created",
        },
    },
}
# Creating the Vector Collection
# db.create_collection("vectors", validator={"$jsonSchema": vector_schema})
vectors_collection = db["vectors"]


# Creating a Chat-History Collection
# Creating a Chat-History Collection
chat_history_schema = {
    "bsonType": "object",
    "required": ["user_id", "session_id", "messages", "timestamp"],
    "properties": {
        "user_id": {
            "bsonType": "objectId",
            "description": "Unique identifier for the user",
        },
        "session_id": {
            "bsonType": "string",
            "description": "Identifier for the session",
        },
        "timestamp": {
            "bsonType": "date",
            "description": "Timestamp when the chat session started",
        },
        "messages": {
            "bsonType": "array",
            "description": "List of chat messages",
            "items": {
                "bsonType": "object",
                "properties": {
                    "prompt": {
                        "bsonType": "string",
                        "description": "User's question or prompt",
                    },
                    "response": {
                        "bsonType": "string",
                        "description": "Assistant's response",
                    },
                    "timestamp": {
                        "bsonType": "date",
                        "description": "Timestamp of the message",
                    },
                },
            },
        },
    },
}

# Create the collection with the schema
# db.create_collection("chat_history", validator={"$jsonSchema": chat_history_schema})
chat_history_collection = db["chat_history"]


# Database setup for Research Assistant
# Research Assistant Schema
research_assistant_schema = {
    "bsonType": "object",
    "required": ["full_name", "password", "email", "courses_assisted"],
    "properties": {
        "full_name": {
            "bsonType": "string",
            "description": "Full name of the research assistant",
        },
        "password": {
            "bsonType": "string",
            "description": "Hashed password of the research assistant",
        },
        "email": {
            "bsonType": "string",
            "description": "Email address of the research assistant",
        },
        "courses_assisted": {
            "bsonType": "array",
            "description": "List of courses the research assistant is assisting",
            "items": {
                "bsonType": "object",
                "required": ["course_id"],
                "properties": {
                    "course_id": {
                        "bsonType": "string",
                        "description": "ID of the course",
                    }
                },
            },
        },
    },
}

# Create research assistants collection
research_assistants_collection = db["research_assistants"]

# Create indexes
research_assistants_collection.create_index("full_name", unique=True)
research_assistants_collection.create_index("email", unique=True)


# Optional: Sample data insertion function
# def insert_sample_research_assistants():
#     sample_research_assistants = [
#         {
#             "full_name": "John Doe RA",
#             "password": generate_password_hash("password123"),
#             "email": "[email protected]",
#             "courses_assisted": [{"course_id": "CS101"}, {"course_id": "CS102"}],
#         }
#     ]

#     try:
#         research_assistants_collection.insert_many(sample_research_assistants)
#         print("Sample research assistants inserted successfully!")
#     except Exception as e:
#         print(f"Error inserting sample research assistants: {e}")

# if __name__ == "__main__":
#     insert_sample_analysts()