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
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
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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
"""
FastAPI Backend for NotebookPRO
Handles RAG, LLM, file processing, and chat management
"""
import spaces
from fastapi import FastAPI, File, UploadFile, HTTPException, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse, FileResponse
from starlette.background import BackgroundTask
from pydantic import BaseModel
from typing import List, Optional, Dict, Any
from pathlib import Path
from datetime import datetime
from io import BytesIO

import torch

import json
import uuid
import sys
import warnings
import logging
import os
import shutil
import tempfile
import zipfile
import re
import html
import time

import weasyprint
import markdown
import latex2mathml.converter
import ziamath

# Suppress warnings
warnings.filterwarnings('ignore')
os.environ['PYTHONWARNINGS'] = 'ignore'
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
os.environ['TOKENIZERS_PARALLELISM'] = 'false'
os.environ.setdefault('OMP_NUM_THREADS', '2')
os.environ.setdefault('MKL_NUM_THREADS', '2')
os.environ.setdefault('OPENBLAS_NUM_THREADS', '2')
os.environ.setdefault('NUMEXPR_NUM_THREADS', '2')
logging.getLogger().setLevel(logging.ERROR)

# Add project root to path
sys.path.append(str(Path(__file__).parent.parent))

import config
from utils.document_processor import DocumentProcessor
from utils.vector_db import VectorDatabase
from utils.hybrid_retriever import HybridRetriever
from utils.llm_generator import LLMGenerator
from utils.config_manager import ConfigManager
from utils.spaces_manager import SpacesManager
from utils.studio_manager import StudioManager
from utils.studio_generator import StudioGenerator
from models.studio_models import (
    NotebookEntry,
    NotebookEntryCreate,
    NotebookEntryUpdate,
    Flashcard,
    FlashcardCreate,
    FlashcardUpdate,
    FlashcardReview,
    FlashcardGenerateRequest,
    MasteryLevel,
    Quiz,
    QuizCreate,
    QuizGenerateRequest,
    QuizSubmission,
    QuizResult,
    QuizHistory,
)

# Initialize FastAPI
app = FastAPI(title="NotebookPRO API", version="2.0.0")

# CORS - Allow Flutter web to connect
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # In production, specify your Flutter web URL
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Global instances
config_manager = ConfigManager()
spaces_manager = SpacesManager()
studio_manager = StudioManager()
studio_generator = None  # Will be initialized after LLM
vector_db = None
hybrid_retriever = None
llm_generator = None
current_space = None

# ==================== Pydantic Models ====================

class ChatMessage(BaseModel):
    role: str
    content: str
    timestamp: str
    sources: Optional[List[Dict[str, Any]]] = None
    suggested_follow_ups: Optional[List[str]] = None

class ChatRequest(BaseModel):
    query: str
    space_id: str
    chat_id: Optional[str] = None
    workflow: str = "chat"

class ChatResponse(BaseModel):
    response: str
    sources: List[Dict[str, Any]]
    chat_id: str
    timestamp: str
    ui_notice: Optional[str] = None
    suggested_follow_ups: Optional[List[str]] = None

class DeleteMessageRequest(BaseModel):
    timestamp: str
    role: str = "assistant"
    content: Optional[str] = None
    delete_related_user: bool = False

class SpaceCreate(BaseModel):
    name: str

class SpaceResponse(BaseModel):
    id: str
    name: str
    created_at: str
    file_count: int

class ChatInfo(BaseModel):
    id: str
    title: str
    preview: str
    created_at: str
    updated_at: str
    message_count: int

class ConfigResponse(BaseModel):
    gemini_api_key: Optional[str]
    nvidia_api_key: Optional[str] = None
    groq_api_key: Optional[str] = None

class ConfigUpdate(BaseModel):
    gemini_api_key: Optional[str] = None
    nvidia_api_key: Optional[str] = None
    groq_api_key: Optional[str] = None

class ChatToNotebookRequest(BaseModel):
    space_id: str
    question: str
    answer: str
    chat_id: Optional[str] = None
    assistant_timestamp: Optional[str] = None
    tags: List[str] = []
    space_name: Optional[str] = None

class ExportAnswerPdfRequest(BaseModel):
    space_id: str
    question: str
    answer: str
    title: Optional[str] = None

class QAItem(BaseModel):
    question: str
    answer: str

class ExportCombinedAnswersPdfRequest(BaseModel):
    space_id: str
    chat_title: Optional[str] = None
    items: List[QAItem]

class ExportSelectedNotebookPdfRequest(BaseModel):
    space_id: str
    entry_ids: List[str]
    title: Optional[str] = None

# ==================== Helper Functions ====================

def get_data_dir():
    """Get data directory path"""
    return Path(config.DATA_DIR)

def get_space_dir(space_id: str):
    """Get space-specific directory"""
    return get_data_dir() / "spaces" / space_id

def load_chats_for_space(space_id: str) -> List[Dict]:
    """Load all chats for a space"""
    chats_file = get_space_dir(space_id) / "chats.json"
    if chats_file.exists():
        with open(chats_file, 'r', encoding='utf-8') as f:
            return json.load(f)
    return []

def save_chats_for_space(space_id: str, chats: List[Dict]):
    """Save chats for a space"""
    chats_file = get_space_dir(space_id) / "chats.json"
    chats_file.parent.mkdir(parents=True, exist_ok=True)
    with open(chats_file, 'w', encoding='utf-8') as f:
        json.dump(chats, f, indent=2, ensure_ascii=False)

def get_chat_title(messages: List[Dict]) -> str:
    """Generate chat title from first user message"""
    for msg in messages:
        if msg['role'] == 'user':
            content = msg['content'][:50]
            return content + "..." if len(msg['content']) > 50 else content
    return "New Chat"

def _is_nlp_metrics_query(query: str) -> bool:
    """Detect metric-heavy NLP evaluation questions that need broader retrieval."""
    q = (query or "").lower()
    triggers = (
        "bleu",
        "rouge",
        "meteor",
        "bertscore",
        "sequence generation",
        "machine translation",
        "summarization",
        "n-gram",
        "brevity penalty",
        "evaluation metric",
        "nlp metric",
        "transformer",
    )
    return any(t in q for t in triggers)

def _extract_year_from_filename(filename: str) -> Optional[int]:
    """Infer publication year from filename (best-effort)."""
    if not filename:
        return None

    matches = re.findall(r'(?:19|20)\d{2}', filename)
    if not matches:
        return None

    try:
        years = [int(y) for y in matches]
        valid_years = [y for y in years if 1900 <= y <= 2100]
        if not valid_years:
            return None
        return max(valid_years)
    except Exception:
        return None

def _resolve_publication_year(meta: Optional[Dict[str, Any]]) -> Optional[int]:
    """Resolve publication year from metadata; fallback to filename inference."""
    if not meta:
        return None

    raw_year = meta.get('publication_year')
    if raw_year is not None:
        try:
            year = int(raw_year)
            if 1900 <= year <= 2100:
                return year
        except Exception:
            pass

    filename = str(meta.get('filename', ''))
    return _extract_year_from_filename(filename)

@spaces.GPU
def _build_retrieval_queries(query: str) -> List[str]:
    """Build retrieval query set; add targeted expansions for NLP metric topics."""
    queries = [query]
    if not _is_nlp_metrics_query(query):
        return queries

    queries.extend([
        f"{query} BLEU brevity penalty BP n-gram precision",
        f"{query} ROUGE-N ROUGE-L longest common subsequence LCS F1 recall precision",
        f"{query} METEOR stemming synonym WordNet exact-match limitation",
        f"{query} BERTScore contextual embeddings semantic similarity deep learning transformer",
    ])
    return queries
@spaces.GPU
def _retrieve_documents_for_query(
    retriever: HybridRetriever,
    query: str,
    n_results: int,
    score_threshold: float,
):
    """Retrieve with optional query expansions and merge by max score."""
    retrieval_queries = _build_retrieval_queries(query)
    per_query_n = max(12, n_results // 2) if len(retrieval_queries) > 1 else n_results
    per_query_threshold = max(0.0, score_threshold - 0.01) if len(retrieval_queries) > 1 else score_threshold

    merged = {}
    for q in retrieval_queries:
        docs, metas, scores = retriever.retrieve(
            query=q,
            n_results=per_query_n,
            score_threshold=per_query_threshold,
        )
        for doc, meta, score in zip(docs, metas, scores):
            current = merged.get(doc)
            score_value = float(score)
            if current is None or score_value > current["score"]:
                merged[doc] = {
                    "doc": doc,
                    "meta": meta,
                    "score": score_value,
                }

    ranked = sorted(merged.values(), key=lambda item: item["score"], reverse=True)
    selected = ranked[:n_results]
    return (
        [item["doc"] for item in selected],
        [item["meta"] for item in selected],
        [item["score"] for item in selected],
    )

def _sanitize_retrieved_chunk(text: str) -> str:
    """Clean retrieval chunks to remove OCR/table-of-contents numeric noise."""
    if not text:
        return ""

    cleaned_lines: List[str] = []
    for raw_line in text.splitlines():
        line = raw_line.strip()
        if not line:
            cleaned_lines.append("")
            continue

        if re.search(r'(?:\d+\.){8,}\d*', line):
            continue

        compact = re.sub(r'\s+', '', line)
        if len(compact) >= 40 and re.fullmatch(r'[\d\W_]+', compact):
            continue

        line = re.sub(r'(?:\d+\.){8,}\d*', ' ', line)
        line = re.sub(r'\s{2,}', ' ', line).strip()
        if line:
            cleaned_lines.append(line)

    cleaned = "\n".join(cleaned_lines)
    cleaned = re.sub(r'\n{3,}', '\n\n', cleaned).strip()
    return cleaned

def _is_usable_context_chunk(text: str) -> bool:
    """Reject chunks that are mostly numeric garbage after cleanup."""
    if not text or len(text) < 80:
        return False

    letters = len(re.findall(r'[A-Za-z]', text))
    digits = len(re.findall(r'\d', text))

    if letters < 35:
        return False

    if digits > 0 and letters < max(80, int(digits * 0.35)):
        return False

    return True

def _format_sse_event(event: str, payload: Dict[str, Any]) -> str:
    """Format one Server-Sent Event frame."""
    return f"event: {event}\ndata: {json.dumps(payload, ensure_ascii=False)}\n\n"

def ensure_notebooks_for_existing_spaces() -> int:
    """Ensure every existing space has an associated notebook metadata record."""
    created_count = 0
    spaces = spaces_manager.get_all_spaces()

    for space in spaces:
        space_id = space.get('id')
        if not space_id:
            continue

        existing_notebook = studio_manager.get_space_notebook(space_id)
        if existing_notebook:
            continue

        studio_manager.ensure_space_notebook(space_id, space.get('name', space_id))
        created_count += 1

    return created_count
@spaces.GPU
def rebuild_space_index_if_missing(space_id: str, force: bool = False) -> int:
    """Rebuild a space index from uploaded files if the current index is empty."""
    if not vector_db:
        return 0

    if not force:
        try:
            existing_count = vector_db.get_collection_count()
            if existing_count > 0:
                print(f"[INDEX_REBUILD {space_id}] skip existing_vectors={existing_count}")
                return 0
        except Exception:
            pass

    uploads_dir = get_space_dir(space_id) / "uploads"
    if not uploads_dir.exists():
        return 0

    files = [
        p for p in uploads_dir.iterdir()
        if p.is_file() and p.suffix.lower() in {".pdf", ".docx", ".txt"}
    ]
    if not files:
        print(f"[INDEX_REBUILD {space_id}] skip no_supported_uploads")
        return 0

    print(f"[INDEX_REBUILD {space_id}] start files={len(files)}")

    processor = DocumentProcessor()
    texts: List[str] = []
    metadatas: List[Dict[str, Any]] = []
    ids: List[str] = []

    for file_path in files:
        try:
            print(f"[INDEX_REBUILD {space_id}] processing_file={file_path.name}")
            file_data = processor.process_file(file_path)
            chunk_filename = file_data.get('virtual_filename', file_path.name)
            chunks = processor.chunk_text(
                file_data['content'],
                chunk_size=config.CHUNK_SIZE,
                overlap=config.CHUNK_OVERLAP,
                semantic=True,
                source_filename=chunk_filename,
            )
            total_chunks = len(chunks)
            print(f"[INDEX_REBUILD {space_id}] file_chunks={total_chunks} file={chunk_filename}")
            publication_year = file_data.get('publication_year', '0000')
            for idx, chunk in enumerate(chunks):
                texts.append(chunk)
                metadatas.append({
                    'filename': chunk_filename,
                    'chunk_index': idx,
                    'total_chunks': total_chunks,
                    'source_type': file_data['format'],
                    'publication_year': publication_year,
                })
                ids.append(f"{space_id}_rebuild_{len(ids)}_{uuid.uuid4().hex[:8]}")
        except Exception as e:
            print(f"Index rebuild skipped {file_path.name}: {e}")

    if not texts:
        print(f"[INDEX_REBUILD {space_id}] skip no_chunks_generated")
        return 0

    batch_size = 5000
    for i in range(0, len(texts), batch_size):
        end = min(i + batch_size, len(texts))
        print(f"[INDEX_REBUILD {space_id}] batch_add start={i} end={end}")
        vector_db.add_documents(
            texts[i:i + batch_size],
            metadatas[i:i + batch_size],
            ids[i:i + batch_size],
        )

    print(f"[INDEX_REBUILD {space_id}] done chunks={len(texts)}")
    return len(texts)

def initialize_space(space_id: str):
    """Initialize vector DB and components for a space"""
    global vector_db, hybrid_retriever, llm_generator, studio_generator, current_space

    if current_space == space_id and vector_db is not None and llm_generator is not None:
        return

    switching_space = current_space is not None and current_space != space_id
    if switching_space:
        vector_db = None
        hybrid_retriever = None
        llm_generator = None
        studio_generator = None
        if config.SPACE_SWITCH_CLEAR_CUDA_CACHE:
            VectorDatabase.clear_runtime_caches(
                unload_embedding_model=config.SPACE_SWITCH_UNLOAD_EMBEDDING_MODEL,
            )
    
    gemini_key = config_manager.get_api_key('gemini')
    nvidia_key = config_manager.get_api_key('nvidia')
    groq_key = config_manager.get_api_key('groq')

    if not gemini_key and not nvidia_key and not groq_key:
        raise HTTPException(
            status_code=400,
            detail="No API key configured. Please add a Gemini or NVIDIA API key.",
        )
    
    space_dir = get_space_dir(space_id)
    vector_db = VectorDatabase(
        collection_name=f"space_{space_id}",
        persist_directory=str(space_dir / "vector_db"),
    )

    collection_reset = vector_db.ensure_collection_embedding_compatibility()
    if collection_reset:
        print(f"[INDEX_REBUILD {space_id}] reset_due_to_embedding_dimension_change=1")

    hybrid_retriever = HybridRetriever(vector_db, alpha=0.6)

    rebuild_space_index_if_missing(space_id)
    
    llm_generator = LLMGenerator(
        provider="gemini",
        gemini_api_key=gemini_key,
        nvidia_api_key=nvidia_key,
        groq_api_key=groq_key,
    )
    
    studio_generator = StudioGenerator(llm_generator, studio_manager)
    current_space = space_id

@app.on_event("startup")
async def startup_sync_notebooks():
    """Auto-create missing notebooks for pre-existing spaces when backend starts and preload GPU models."""
    try:
        from utils.vector_db import VectorDatabase
        print("Preloading embedding and reranker models to GPU...")
        VectorDatabase._get_or_create_embedding_model()
        VectorDatabase._get_or_create_reranker()
        print("Models successfully preloaded to GPU.")
    except Exception as e:
        print(f"Failed to preload models: {e}")

    try:
        created = ensure_notebooks_for_existing_spaces()
        if created > 0:
            print(f"Created {created} missing notebook(s) for existing spaces")
    except Exception as e:
        print(f"Notebook startup sync failed: {e}")

# ==================== API Endpoints ====================

@app.get("/")
async def root():
    """Health check"""
    return {"status": "NotebookPRO API is running", "version": "2.0.0"}

@app.get("/api/health")
async def health_check():
    """Fast health endpoint used by the Flutter app connection checks."""
    return {"status": "ok"}

@app.get("/api/config", response_model=ConfigResponse)
async def get_config():
    """Get current API keys (masked)"""
    gemini_key = config_manager.get_api_key('gemini')
    nvidia_key = config_manager.get_api_key('nvidia')
    groq_key = config_manager.get_api_key('groq')
    masked_gemini_key = "***" + gemini_key[-4:] if gemini_key else None
    masked_nvidia_key = "***" + nvidia_key[-4:] if nvidia_key else None
    masked_groq_key = "***" + groq_key[-4:] if groq_key else None
    
    return ConfigResponse(
        gemini_api_key=masked_gemini_key,
        nvidia_api_key=masked_nvidia_key,
        groq_api_key=masked_groq_key,
    )

@app.get("/api/system/export")
async def export_data():
    """Export the entire data directory as a ZIP file."""
    try:
        tmp_dir = Path(tempfile.gettempdir())
        zip_base_path = tmp_dir / f"notebookpro_backup_{int(time.time())}"
        
        shutil.make_archive(
            base_name=str(zip_base_path),
            format="zip",
            root_dir=str(get_data_dir())
        )
        
        zip_path = str(zip_base_path) + ".zip"
        
        return FileResponse(
            path=zip_path,
            filename="notebookpro_backup.zip",
            media_type="application/zip",
            background=BackgroundTask(lambda: os.remove(zip_path))
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Export failed: {e}")

@app.post("/api/system/import")
async def import_data(file: UploadFile = File(...)):
    """Import a ZIP file and overwrite the data directory."""
    if not file.filename.endswith('.zip'):
        raise HTTPException(status_code=400, detail="Must be a ZIP file")
        
    try:
        tmp_dir = Path(tempfile.gettempdir())
        zip_path = tmp_dir / f"notebookpro_import_{int(time.time())}.zip"
        
        with open(zip_path, "wb") as buffer:
            shutil.copyfileobj(file.file, buffer)
            
        with zipfile.ZipFile(zip_path, 'r') as zip_ref:
            pass
            
        extract_dir = tmp_dir / f"notebookpro_import_extracted_{int(time.time())}"
        if extract_dir.exists():
            shutil.rmtree(extract_dir)
        extract_dir.mkdir(parents=True)
        
        with zipfile.ZipFile(zip_path, 'r') as zip_ref:
            zip_ref.extractall(extract_dir)
            
        data_dir = get_data_dir()
        for item in extract_dir.iterdir():
            target = data_dir / item.name
            if target.exists():
                if target.is_dir():
                    shutil.rmtree(target)
                else:
                    target.unlink()
            shutil.move(str(item), str(target))
            
        os.remove(zip_path)
        shutil.rmtree(extract_dir)
        
        return {"status": "success", "message": "Data imported successfully. Please restart the backend."}
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Import failed: {e}")

@app.post("/api/config")
async def update_config(config_update: ConfigUpdate):
    """Update API keys"""
    if config_update.gemini_api_key:
        config_manager.set_api_key('gemini', config_update.gemini_api_key)

    if config_update.nvidia_api_key:
        config_manager.set_api_key('nvidia', config_update.nvidia_api_key)

    if config_update.groq_api_key:
        config_manager.set_api_key('groq', config_update.groq_api_key)
    
    return {"status": "success", "message": "Configuration updated"}

@app.get("/api/spaces", response_model=List[SpaceResponse])
async def get_spaces():
    """Get all spaces"""
    ensure_notebooks_for_existing_spaces()
    spaces = spaces_manager.get_all_spaces()
    
    result = []
    for space in spaces:
        space_id = space['id']
        space_dir = get_space_dir(space_id)
        processed_file = space_dir / "processed_files.json"
        
        file_count = 0
        if processed_file.exists():
            with open(processed_file, 'r', encoding='utf-8-sig') as f:
                file_count = len(json.load(f))
        
        result.append(SpaceResponse(
            id=space_id,
            name=space['name'],
            created_at=space['created_at'],
            file_count=file_count
        ))
    
    return result

@app.post("/api/spaces", response_model=SpaceResponse)
async def create_space(space_data: SpaceCreate):
    """Create a new space"""
    try:
        space = spaces_manager.create_space(space_data.name)
        studio_manager.ensure_space_notebook(space['id'], space['name'])
        
        return SpaceResponse(
            id=space['id'],
            name=space['name'],
            created_at=space['created_at'],
            file_count=0
        )
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))

@app.delete("/api/spaces/{space_id}")
async def delete_space(space_id: str):
    """Delete a space"""
    try:
        spaces_manager.delete_space(space_id)
        
        space_dir = get_space_dir(space_id)
        if space_dir.exists():
            shutil.rmtree(space_dir)
        
        return {"status": "success", "message": f"Space {space_id} deleted"}
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error deleting space: {str(e)}")

@app.get("/api/spaces/{space_id}/chats", response_model=List[ChatInfo])
async def get_chats(space_id: str):
    """Get all chats for a space"""
    chats = load_chats_for_space(space_id)
    
    result = []
    for chat in chats:
        messages = chat.get('messages', [])
        result.append(ChatInfo(
            id=chat['id'],
            title=get_chat_title(messages),
            preview=messages[0]['content'][:100] if messages else "",
            created_at=chat.get('created_at', ''),
            updated_at=chat.get('updated_at', ''),
            message_count=len(messages)
        ))
    
    return result

@app.get("/api/spaces/{space_id}/chats/{chat_id}")
async def get_chat(space_id: str, chat_id: str):
    """Get specific chat by ID"""
    chats = load_chats_for_space(space_id)
    
    for chat in chats:
        if chat['id'] == chat_id:
            return chat
    
    raise HTTPException(status_code=404, detail="Chat not found")

@app.delete("/api/spaces/{space_id}/chats/{chat_id}")
async def delete_chat(space_id: str, chat_id: str):
    """Delete a chat"""
    chats = load_chats_for_space(space_id)
    chats = [c for c in chats if c['id'] != chat_id]
    save_chats_for_space(space_id, chats)
    
    return {"status": "success", "message": f"Chat {chat_id} deleted"}

@app.post("/api/spaces/{space_id}/chats/{chat_id}/messages/delete")
async def delete_chat_message(space_id: str, chat_id: str, request: DeleteMessageRequest):
    """Delete a specific message from a chat."""
    chats = load_chats_for_space(space_id)

    chat = next((c for c in chats if c.get('id') == chat_id), None)
    if chat is None:
        raise HTTPException(status_code=404, detail="Chat not found")

    messages = chat.get('messages', [])
    removed = False
    target_index = -1

    for idx, msg in enumerate(messages):
        if msg.get('role') != request.role:
            continue
        if msg.get('timestamp') != request.timestamp:
            continue
        if request.content is not None and msg.get('content') != request.content:
            continue

        target_index = idx
        removed = True
        break

    if not removed and request.content is not None:
        for idx in range(len(messages) - 1, -1, -1):
            msg = messages[idx]
            if msg.get('role') == request.role and msg.get('content') == request.content:
                target_index = idx
                removed = True
                break

    if not removed:
        raise HTTPException(status_code=404, detail="Message not found")

    indexes_to_remove = {target_index}

    if request.delete_related_user and target_index > 0:
        for idx in range(target_index - 1, -1, -1):
            if messages[idx].get('role') == 'user':
                indexes_to_remove.add(idx)
                break

    for idx in sorted(indexes_to_remove, reverse=True):
        if 0 <= idx < len(messages):
            del messages[idx]

    chat['messages'] = messages
    chat['updated_at'] = datetime.now().isoformat()
    save_chats_for_space(space_id, chats)

    return {
        "status": "success",
        "message": "Message deleted",
        "remaining_messages": len(messages),
    }

@app.post("/api/chat", response_model=ChatResponse)
async def chat(request: ChatRequest):
    """Process a chat message with RAG"""
    try:
        trace_id = uuid.uuid4().hex[:8]
        t_start = time.perf_counter()

        t_init = time.perf_counter()
        initialize_space(request.space_id)
        
        global hybrid_retriever
        if hybrid_retriever is None:
            hybrid_retriever = HybridRetriever(vector_db, alpha=0.6)

        retrieval_n = int(os.getenv("CHAT_RETRIEVAL_RESULTS", "16"))
        retrieval_n = max(8, min(retrieval_n, 30))
        retrieval_threshold = float(os.getenv("CHAT_RETRIEVAL_THRESHOLD", "0.01"))
        chat_max_tokens = int(os.getenv("CHAT_MAX_TOKENS", "1800"))
        chat_max_tokens = max(700, min(chat_max_tokens, 2600))
        
        documents, metadatas, scores = _retrieve_documents_for_query(
            retriever=hybrid_retriever,
            query=request.query,
            n_results=retrieval_n,
            score_threshold=retrieval_threshold,
        )
        
        context_parts = []
        sources = []
        filtered_metadatas = []
        dropped_chunks = 0
        seen_context_fingerprints = set()
        
        for doc, meta, score in zip(documents, metadatas, scores):
            cleaned_doc = _sanitize_retrieved_chunk(doc)
            if not _is_usable_context_chunk(cleaned_doc):
                dropped_chunks += 1
                continue

            fingerprint = re.sub(r'\s+', ' ', cleaned_doc[:320]).lower()
            if fingerprint in seen_context_fingerprints:
                dropped_chunks += 1
                continue
            seen_context_fingerprints.add(fingerprint)

            filename = meta.get('filename', 'Unknown')
            clean_name = filename.replace('.pdf', '').replace('.docx', '').replace('.txt', '')
            publication_year = _resolve_publication_year(meta)
            year_label = str(publication_year) if publication_year else "UNKNOWN"
            source_idx = len(context_parts) + 1
            context_parts.append(f"Source [{source_idx}] [YEAR: {year_label}] ({clean_name}):\n{cleaned_doc}\n")
            filtered_metadatas.append(meta)
            sources.append({
                "content": cleaned_doc[:200] + "..." if len(cleaned_doc) > 200 else cleaned_doc,
                "metadata": meta,
                "score": float(score)
            })

        if not context_parts:
            return ChatResponse(
                response=(
                    "I found related retrieval hits, but they were too noisy to trust for a grounded answer. "
                    "Please re-upload a cleaner source file (or a text-based PDF) and try again."
                ),
                sources=[],
                chat_id=request.chat_id or str(uuid.uuid4()),
                timestamp=datetime.now().isoformat(),
                ui_notice="Context chunks were filtered due to low text quality.",
                suggested_follow_ups=[],
            )
        
        context = "\n".join(context_parts)
        
        response = llm_generator.generate_response(
            prompt=request.query,
            context=context,
            use_case=request.workflow if request.workflow in ["summary", "explanation", "qa", "notes"] else "qa",
            metadatas=filtered_metadatas,
            temperature=0.35,
            max_tokens=chat_max_tokens,
        )

        suggested_follow_ups = llm_generator.generate_follow_up_questions(
            prompt=request.query,
            context=context,
            answer=response,
            max_questions=3,
        )
        ui_notice = getattr(llm_generator, 'last_notice', None)

        chat_id = request.chat_id or str(uuid.uuid4())
        chats = load_chats_for_space(request.space_id)
        
        chat = next((c for c in chats if c['id'] == chat_id), None)
        
        if not chat:
            chat = {
                'id': chat_id,
                'messages': [],
                'created_at': datetime.now().isoformat(),
                'updated_at': datetime.now().isoformat()
            }
            chats.append(chat)
        
        timestamp = datetime.now().isoformat()
        chat['messages'].extend([
            {'role': 'user', 'content': request.query, 'timestamp': timestamp},
            {
                'role': 'assistant',
                'content': response,
                'timestamp': timestamp,
                'sources': sources,
                'suggested_follow_ups': suggested_follow_ups,
            }
        ])
        chat['updated_at'] = timestamp
        
        save_chats_for_space(request.space_id, chats)
        
        return ChatResponse(
            response=response,
            sources=sources,
            chat_id=chat_id,
            timestamp=timestamp,
            ui_notice=ui_notice,
            suggested_follow_ups=suggested_follow_ups,
        )
    
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/api/chat/stream")
async def chat_stream(request: ChatRequest):
    """SSE streaming compatibility endpoint for the Flutter client."""
    trace_id = "unknown"
    t_stream = time.perf_counter()
    try:
        trace_id = uuid.uuid4().hex[:8]
        @space.GPU
        def event_stream():
            try:
                initialize_space(request.space_id)
                global hybrid_retriever
                if hybrid_retriever is None:
                    hybrid_retriever = HybridRetriever(vector_db, alpha=0.6)
                
                retrieval_n = int(os.getenv("CHAT_RETRIEVAL_RESULTS", "16"))
                retrieval_n = max(8, min(retrieval_n, 30))
                retrieval_threshold = float(os.getenv("CHAT_RETRIEVAL_THRESHOLD", "0.01"))
                chat_max_tokens = int(os.getenv("CHAT_MAX_TOKENS", "1800"))
                chat_max_tokens = max(700, min(chat_max_tokens, 2600))

                documents, metadatas, scores = _retrieve_documents_for_query(
                    retriever=hybrid_retriever,
                    query=request.query,
                    n_results=retrieval_n,
                    score_threshold=retrieval_threshold,
                )

                context_parts = []
                sources = []
                filtered_metadatas = []
                seen_context_fingerprints = set()
                
                for doc, meta, score in zip(documents, metadatas, scores):
                    cleaned_doc = _sanitize_retrieved_chunk(doc)
                    if not _is_usable_context_chunk(cleaned_doc):
                        continue
                    
                    fingerprint = re.sub(r'\s+', ' ', cleaned_doc[:320]).lower()
                    if fingerprint in seen_context_fingerprints:
                        continue
                    seen_context_fingerprints.add(fingerprint)

                    filename = meta.get('filename', 'Unknown')
                    clean_name = filename.replace('.pdf', '').replace('.docx', '').replace('.txt', '')
                    publication_year = _resolve_publication_year(meta)
                    year_label = str(publication_year) if publication_year else "UNKNOWN"
                    source_idx = len(context_parts) + 1
                    
                    context_parts.append(f"Source [{source_idx}] [YEAR: {year_label}] ({clean_name}):\n{cleaned_doc}\n")
                    filtered_metadatas.append(meta)
                    sources.append({
                        "content": cleaned_doc[:200] + "..." if len(cleaned_doc) > 200 else cleaned_doc,
                        "metadata": meta,
                        "score": float(score)
                    })

                chat_id = request.chat_id or str(uuid.uuid4())
                timestamp = datetime.now().isoformat()

                if not context_parts:
                    yield _format_sse_event("meta", {
                        "chat_id": chat_id,
                        "timestamp": timestamp,
                        "sources": [],
                        "ui_notice": "Context chunks were filtered due to low text quality.",
                    })
                    msg = "I found related retrieval hits, but they were too noisy to trust for a grounded answer. Please re-upload a cleaner source file (or a text-based PDF) and try again."
                    yield _format_sse_event("token", {"delta": msg})
                    yield _format_sse_event("done", {
                        "chat_id": chat_id,
                        "timestamp": timestamp,
                        "response": msg,
                        "sources": [],
                        "ui_notice": "Context chunks were filtered due to low text quality.",
                        "suggested_follow_ups": [],
                    })
                    return

                context = "\n".join(context_parts)

                yield _format_sse_event("meta", {
                    "chat_id": chat_id,
                    "timestamp": timestamp,
                    "sources": sources,
                    "ui_notice": getattr(llm_generator, 'last_notice', None),
                })

                full_response = ""
                generator = llm_generator.generate_stream(
                    prompt=request.query,
                    context=context,
                    use_case=request.workflow if request.workflow in ["summary", "explanation", "qa", "notes"] else "qa",
                    metadatas=filtered_metadatas,
                    temperature=0.35,
                    max_tokens=chat_max_tokens,
                )

                for chunk in generator:
                    if chunk:
                        full_response += chunk
                        yield _format_sse_event("token", {"delta": chunk})

                suggested_follow_ups = llm_generator.generate_follow_up_questions(
                    prompt=request.query,
                    context=context,
                    answer=full_response,
                    max_questions=3,
                )

                if suggested_follow_ups:
                    yield _format_sse_event("followups", {
                        "suggested_follow_ups": suggested_follow_ups,
                    })

                chats = load_chats_for_space(request.space_id)
                chat = next((c for c in chats if c['id'] == chat_id), None)
                if not chat:
                    chat = {
                        'id': chat_id,
                        'messages': [],
                        'created_at': timestamp,
                        'updated_at': timestamp
                    }
                    chats.append(chat)
                
                chat['messages'].extend([
                    {'role': 'user', 'content': request.query, 'timestamp': timestamp},
                    {
                        'role': 'assistant',
                        'content': full_response,
                        'timestamp': timestamp,
                        'sources': sources,
                        'suggested_follow_ups': suggested_follow_ups,
                    }
                ])
                chat['updated_at'] = timestamp
                save_chats_for_space(request.space_id, chats)

                yield _format_sse_event("done", {
                    "chat_id": chat_id,
                    "timestamp": timestamp,
                    "response": full_response,
                    "sources": sources,
                    "ui_notice": getattr(llm_generator, 'last_notice', None),
                    "suggested_follow_ups": suggested_follow_ups,
                })

            except Exception as e:
                print(f"[CHAT_STREAM ERROR] {trace_id}: {str(e)}")
                yield _format_sse_event("error", {"message": str(e)})

        return StreamingResponse(
            event_stream(),
            media_type="text/event-stream",
            headers={
                "Cache-Control": "no-cache",
                "Connection": "keep-alive",
                "X-Accel-Buffering": "no",
            },
        )

    except Exception as exc:
        error_message = str(exc)

        def error_stream(message: str = error_message):
            yield _format_sse_event("error", {"message": message})

        return StreamingResponse(
            error_stream(),
            media_type="text/event-stream",
            status_code=500,
        )

@app.post("/api/spaces/{space_id}/upload")
async def upload_files(space_id: str, files: List[UploadFile] = File(...)):
    """Upload and process files for a space"""
    try:
        global hybrid_retriever
        initialize_space(space_id)
        
        space_dir = get_space_dir(space_id)
        uploads_dir = space_dir / "uploads"
        uploads_dir.mkdir(parents=True, exist_ok=True)
        
        processor = DocumentProcessor()
        all_chunks = []
        processed_files = []
        skipped_files = []

        processed_file_path = space_dir / "processed_files.json"
        existing_filenames = set()
        if processed_file_path.exists():
            with open(processed_file_path, 'r', encoding='utf-8-sig') as f:
                try:
                    for item in json.load(f):
                        existing_filenames.add(item.get('original_filename', item.get('filename')))
                except Exception:
                    pass
        
        for file in files:
            filename = (file.filename or "unnamed_upload").strip()

            if filename in existing_filenames:
                skipped_files.append({"filename": filename, "reason": "File already exists in this space"})
                continue

            try:
                await file.seek(0)
            except Exception:
                pass

            content = await file.read()
            content_size = len(content) if content else 0
            if content_size == 0:
                reason = "Upload payload was empty (0 bytes)"
                skipped_files.append({"filename": filename, "reason": reason})
                continue

            file_path = uploads_dir / filename
            with open(file_path, "wb") as f:
                f.write(content)
                f.flush()
                os.fsync(f.fileno())

            try:
                if file_path.stat().st_size == 0:
                    reason = "File became 0 bytes after write"
                    skipped_files.append({"filename": filename, "reason": reason})
                    file_path.unlink(missing_ok=True)
                    continue
            except Exception:
                pass
            
            try:
                file_data = processor.process_file(file_path)
                content = file_data['content']
                chunk_filename = file_data.get('virtual_filename', filename)
                
                chunks = processor.chunk_text(
                    content,
                    chunk_size=config.CHUNK_SIZE,
                    overlap=config.CHUNK_OVERLAP,
                    semantic=True,
                    source_filename=chunk_filename,
                )
                
                formatted_chunks = []
                publication_year = file_data.get('publication_year', '0000')
                for idx, chunk in enumerate(chunks):
                    formatted_chunks.append({
                        'content': chunk,
                        'metadata': {
                            'filename': chunk_filename,
                            'chunk_index': idx,
                            'total_chunks': len(chunks),
                            'source_type': file_data['format'],
                            'publication_year': publication_year,
                        }
                    })
                
                all_chunks.extend(formatted_chunks)
                processed_files.append({
                    'filename': chunk_filename,
                    'original_filename': filename,
                    'publication_year': publication_year,
                    'chunks': len(chunks),
                    'processed_at': datetime.now().isoformat()
                })
                
                try:
                    p_file = space_dir / "processed_files.json"
                    ex = []
                    if p_file.exists():
                        with open(p_file, 'r', encoding='utf-8-sig') as f:
                            ex = json.load(f)
                    ex = [x for x in ex if x.get('original_filename') != filename and x.get('filename') != chunk_filename]
                    ex.append(processed_files[-1])
                    with open(p_file, 'w', encoding='utf-8') as f:
                        json.dump(ex, f, indent=2)
                except Exception as e:
                    print(f"Error saving progressive processed_files.json: {e}")
                    
            except Exception as e:
                err = str(e)
                skipped_files.append({"filename": filename, "reason": err})
                continue
        
        if all_chunks:
            texts = [chunk['content'] for chunk in all_chunks]
            metadatas = [chunk['metadata'] for chunk in all_chunks]
            ids = [f"{space_id}_{idx}_{uuid.uuid4().hex[:8]}" for idx in range(len(all_chunks))]
            
            batch_size = 5000
            for i in range(0, len(texts), batch_size):
                batch_texts = texts[i:i + batch_size]
                batch_metadatas = metadatas[i:i + batch_size]
                batch_ids = ids[i:i + batch_size]
                
                vector_db.add_documents(batch_texts, batch_metadatas, batch_ids)

            hybrid_retriever = HybridRetriever(vector_db, alpha=0.6)
        
        processed_file = space_dir / "processed_files.json"
        existing = []
        if processed_file.exists():
            try:
                with open(processed_file, 'r', encoding='utf-8-sig') as f:
                    existing = json.load(f)
            except Exception:
                existing = []
        
        new_originals = {p.get('original_filename') for p in processed_files}
        new_virtuals = {p.get('filename') for p in processed_files}
        existing = [x for x in existing if x.get('original_filename') not in new_originals and x.get('filename') not in new_virtuals]
        
        existing.extend(processed_files)
        tmp_file = processed_file.with_suffix('.tmp')
        with open(tmp_file, 'w', encoding='utf-8') as f:
            json.dump(existing, f, indent=2, ensure_ascii=False)
        tmp_file.replace(processed_file)
        
        return {
            "status": "success",
            "files_processed": len(processed_files),
            "total_chunks": len(all_chunks),
            "files_skipped": len(skipped_files),
            "skipped_files": skipped_files,
        }
    
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/spaces/{space_id}/files")
async def get_files(space_id: str):
    """Get processed files for a space"""
    processed_file = get_space_dir(space_id) / "processed_files.json"
    
    if processed_file.exists():
        with open(processed_file, 'r', encoding='utf-8-sig') as f:
            return json.load(f)
    
    return []

@app.delete("/api/spaces/{space_id}/files/{filename}")
async def delete_file(space_id: str, filename: str):
    """Delete a specific file from a space"""
    try:
        original_filename = filename
        processed_file = get_space_dir(space_id) / "processed_files.json"
        if processed_file.exists():
            with open(processed_file, 'r', encoding='utf-8-sig') as f:
                files_data = json.load(f)

            for item in files_data:
                if item.get('filename') == filename:
                    original_filename = item.get('original_filename', filename)
                    break
                if item.get('original_filename') == filename:
                    original_filename = item.get('original_filename', filename)
                    break

            files_data = [
                f for f in files_data
                if f.get('filename') != filename and f.get('original_filename') != filename
            ]
            with open(processed_file, 'w', encoding='utf-8') as f:
                json.dump(files_data, f, indent=2, ensure_ascii=False)

        file_path = get_space_dir(space_id) / "uploads" / original_filename
        if file_path.exists():
            file_path.unlink()

        try:
            ids_to_delete: List[str] = []
            candidate_names = {filename, original_filename}
            try:
                for candidate in candidate_names:
                    filtered = vector_db.collection.get(where={"filename": candidate})
                    if filtered and filtered.get('ids'):
                        ids_to_delete.extend(filtered.get('ids', []))
            except Exception:
                all_results = vector_db.collection.get()
                all_ids = all_results.get('ids', []) if all_results else []
                all_metas = all_results.get('metadatas', []) if all_results else []
                for doc_id, metadata in zip(all_ids, all_metas):
                    if metadata and metadata.get('filename') in candidate_names:
                        ids_to_delete.append(doc_id)

            if ids_to_delete:
                vector_db.collection.delete(ids=list(set(ids_to_delete)))
                vector_db.client.persist()
        except Exception as e:
            print(f"Vector cleanup warning for {filename}: {e}")

        return {
            "status": "success",
            "message": f"File {filename} deleted"
        }

    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# ==================== Document Export Helpers ====================

def _render_latex_to_mathml(text: str) -> str:
    """Find math expressions in text and convert using ziamath."""
    text = re.sub(r'\s*(?:\*\*)?\[\d+(?:,\s*\d+)*\](?:\*\*)?', '', text)

    def render_math(m, display_mode):
        try:
            math_str = m.group(1).strip()
            svg = ziamath.Math.fromlatex(math_str).svg()
            if display_mode == 'block':
                return f'<div style="text-align: center; margin: 1em 0;">{svg}</div>'
            else:
                return f'<span style="display:inline-block; vertical-align:middle;">{svg}</span>'
        except Exception as e:
            print("MATH RENDER ERROR: ", e)
            return m.group(0)

    text = re.sub(r'\\\[([\s\S]+?)\\\]', lambda m: render_math(m, 'block'), text)
    text = re.sub(r'\\\(([\s\S]+?)\\\)', lambda m: render_math(m, 'inline'), text)
    text = re.sub(r'\$\$([\s\S]+?)\$\$', lambda m: render_math(m, 'block'), text)
    text = re.sub(r'\$(?!\$)([\s\S]+?)\$(?!\$)', lambda m: render_math(m, 'inline'), text)

    return text

def _render_html_page(title: str, body_html: str, space_id: str) -> bytes:
    """Generates WeasyPrint PDF from HTML."""
    exported_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    html_content = f"""
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>{html.escape(title)}</title>
        <style>
            body {{ font-family: sans-serif; font-size: 11pt; line-height: 1.5; color: #333; }}
            h1 {{ font-size: 18pt; margin-bottom: 5px; }}
            .meta {{ font-size: 9pt; color: #666; margin-bottom: 20px; }}
            h2 {{ font-size: 14pt; margin-top: 15px; margin-bottom: 5px; }}
            h3 {{ font-size: 12.5pt; margin-top: 10px; margin-bottom: 5px; }}
            table {{ border-collapse: collapse; width: 100%; margin-bottom: 10px; }}
            th, td {{ border: 1px solid #ccc; padding: 6px; text-align: left; font-size: 10pt; }}
            pre {{ background-color: #f5f5f5; padding: 10px; font-size: 9.5pt; overflow-x: auto; }}
            code {{ font-family: monospace; background-color: #f5f5f5; padding: 2px 4px; }}
            blockquote {{ border-left: 3px solid #ccc; margin: 0; padding-left: 10px; color: #555; }}
            math {{ font-family: "Cambria Math", "Latin Modern Math", "STIX Two Math", math, serif; font-size: 1.1em; }}
            .answer-block {{ margin-bottom: 20px; }}
        </style>
    </head>
    <body>
        <h1>{html.escape(title)}</h1>
        <div class="meta">Space: {html.escape(space_id)} <br> Exported at: {exported_at}</div>
        {body_html}
    </body>
    </html>
    """
    pdf_bytes = weasyprint.HTML(string=html_content).write_pdf()
    return pdf_bytes

def _build_answers_pdf_bytes(title: str, items: list[dict], space_id: str) -> bytes:
    body_parts = []
    for item in items:
        q = item.get('question', '')
        a = item.get('answer', '')
        
        q_math = _render_latex_to_mathml(q)
        a_math = _render_latex_to_mathml(a)
        
        q_html = markdown.markdown(q_math, extensions=['tables', 'fenced_code'])
        a_html = markdown.markdown(a_math, extensions=['tables', 'fenced_code'])
        
        body_parts.append(f"<div class='answer-block'><h2>Question: {q_html}</h2><div class='answer'>{a_html}</div></div><hr>")
        
    return _render_html_page(title, "".join(body_parts), space_id)

def _safe_pdf_filename(name: str) -> str:
    base = (name or 'notebookpro_export').strip().replace(' ', '_')
    base = re.sub(r'[^A-Za-z0-9_\-]', '', base)
    if not base:
        base = 'notebookpro_export'
    return f"{base}.pdf"

def _apply_inline_format(text: str, attrs: dict) -> str:
    if not text: return text
    
    l_space = len(text) - len(text.lstrip(' \t\n'))
    r_space = len(text) - len(text.rstrip(' \t\n'))
    
    inner = text.strip(' \t\n')
    if not inner: return text
    
    if attrs.get('bold'): inner = f"**{inner}**"
    if attrs.get('italic'): inner = f"*{inner}*"
    if attrs.get('strike'): inner = f"~~{inner}~~"
    if attrs.get('code'): inner = f"`{inner}`"
    if attrs.get('link'): inner = f"[{inner}]({attrs.get('link')})"
    
    return text[:l_space] + inner + text[len(text)-r_space:] if r_space > 0 else text[:l_space] + inner

def _quill_or_plain_to_text(content: str) -> str:
    """Convert plain text or Quill delta JSON string into Markdown text."""
    if not content:
        return ""

    stripped = content.strip()
    if not stripped:
        return ""

    try:
        parsed = json.loads(stripped)
        ops = []
        if isinstance(parsed, dict) and isinstance(parsed.get('ops'), list):
            ops = parsed.get('ops', [])
        elif isinstance(parsed, list):
            ops = parsed
        else:
            return stripped

        lines = []
        current_line_ops = []
        
        for op in ops:
            if not isinstance(op, dict):
                continue
                
            insert_value = op.get('insert')
            if not isinstance(insert_value, str):
                continue
                
            attrs = op.get('attributes', {})
            parts = insert_value.split('\n')
            
            for i, part in enumerate(parts):
                if i > 0:
                    line_md = ""
                    for l_op, l_attrs in current_line_ops:
                        line_md += _apply_inline_format(l_op, l_attrs)
                    
                    if attrs.get('header') == 1: line_md = f"# {line_md}"
                    elif attrs.get('header') == 2: line_md = f"## {line_md}"
                    elif attrs.get('header') == 3: line_md = f"### {line_md}"
                    elif attrs.get('header') == 4: line_md = f"#### {line_md}"
                    elif attrs.get('blockquote'): line_md = f"> {line_md}"
                    elif attrs.get('code-block'): line_md = f"```\n{line_md}\n```"
                    elif attrs.get('list') == 'bullet': line_md = f"* {line_md}"
                    elif attrs.get('list') == 'ordered': line_md = f"1. {line_md}"
                    
                    lines.append(line_md)
                    current_line_ops = []
                
                if part:
                    current_line_ops.append((part, attrs))
                    
        if current_line_ops:
            line_md = ""
            for l_op, l_attrs in current_line_ops:
                line_md += _apply_inline_format(l_op, l_attrs)
            lines.append(line_md)
            
        text = '\n'.join(lines)
        text = re.sub(r'\n{3,}', '\n\n', text)
        return text.strip()
    except Exception:
        return stripped

def _extract_chat_qa(text: str) -> Optional[Dict[str, str]]:
    """Extract chat-style Q/A blocks from plain text."""
    if not text:
        return None

    match = re.match(r'^\s*Q:\s*(.*?)\s*\n\s*\n\s*A:\s*(.*)$', text, flags=re.DOTALL | re.IGNORECASE)
    if not match:
        return None

    question = (match.group(1) or '').strip()
    answer = (match.group(2) or '').strip()
    if not question or not answer:
        return None
    return {'question': question, 'answer': answer}

def _normalize_compare_text(value: str) -> str:
    v = (value or '').lower().strip()
    v = v.replace('...', ' ')
    v = re.sub(r'[^a-z0-9\s]', ' ', v)
    v = re.sub(r'\s+', ' ', v).strip()
    return v

def _is_same_question_title(title: str, question: str) -> bool:
    """Fuzzy equality to match truncated titles with full questions."""
    t = _normalize_compare_text(title)
    q = _normalize_compare_text(question)
    if not t or not q:
        return False

    if t == q:
        return True

    if len(t) >= 18 and q.startswith(t):
        return True
    if len(q) >= 18 and t.startswith(q):
        return True

    t_tokens = t.split()
    q_tokens = q.split()
    common = min(len(t_tokens), len(q_tokens), 8)
    if common >= 4 and t_tokens[:common] == q_tokens[:common]:
        return True

    return False

def _normalize_chat_entry_body(entry: NotebookEntry, body_text: str) -> str:
    """For chat entries, avoid repeating question in body when title already carries it."""
    qa = _extract_chat_qa(body_text)
    if not qa:
        return body_text

    title = (entry.title or '').strip()
    question = qa['question'].strip()

    if not title:
        return f"Question: {qa['question']}\n\nAnswer: {qa['answer']}"

    if _is_same_question_title(title, question):
        return qa['answer']

    return f"Question: {qa['question']}\n\nAnswer: {qa['answer']}"

def _build_notebook_entries_pdf_bytes(title: str, entries: list, space_id: str) -> bytes:
    body_parts = []
    for idx, entry in enumerate(entries, 1):
        body_text = _quill_or_plain_to_text(entry.content)
        entry_title = entry.title or f"Note {idx}"

        if entry.source_type == 'chat':
            qa = _extract_chat_qa(body_text)
            if qa and qa.get('question'):
                entry_title = qa['question']
            body_text = _normalize_chat_entry_body(entry, body_text)

        updated = entry.updated_at.strftime('%Y-%m-%d %H:%M') if entry.updated_at else ''
        
        body_text = _render_latex_to_mathml(body_text)
        body_html = markdown.markdown(body_text, extensions=['tables', 'fenced_code'])
        
        updated_str = f"<div class='meta'>Updated: {updated}</div>" if updated else ""
        body_parts.append(f"<div class='answer-block'><h2>{idx}. {html.escape(entry_title)}</h2>{updated_str}<div>{body_html}</div></div><hr>")
        
    return _render_html_page(title, "".join(body_parts), space_id)

# ==================== Studio / Notebook Routes ====================

@app.post("/api/studio/notebook", response_model=NotebookEntry)
async def create_notebook_entry(entry_data: NotebookEntryCreate):
    """Create a new notebook entry"""
    try:
        entry = studio_manager.create_notebook_entry(entry_data)
        return entry
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/studio/notebook/space/{space_id}")
async def get_space_notebook(space_id: str):
    """Get or create notebook metadata for a space."""
    try:
        space = spaces_manager.get_space(space_id)
        space_name = space['name'] if space else space_id
        notebook = studio_manager.ensure_space_notebook(space_id, space_name)
        return notebook
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/api/studio/notebook/from-chat", response_model=NotebookEntry)
async def add_chat_to_notebook(request: ChatToNotebookRequest):
    """Add a chat question/answer pair into a space notebook."""
    try:
        space = spaces_manager.get_space(request.space_id)
        resolved_space_name = request.space_name or (space['name'] if space else request.space_id)

        entry = studio_manager.create_notebook_entry_from_chat(
            space_id=request.space_id,
            question=request.question,
            answer=request.answer,
            chat_id=request.chat_id,
            assistant_timestamp=request.assistant_timestamp,
            tags=request.tags,
            space_name=resolved_space_name
        )
        return entry
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/studio/notebook", response_model=List[NotebookEntry])
async def list_notebook_entries(space_id: Optional[str] = None):
    """List all notebook entries, optionally filtered by space"""
    try:
        entries = studio_manager.list_notebook_entries(space_id)
        return entries
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/studio/notebook/{entry_id}", response_model=NotebookEntry)
async def get_notebook_entry(entry_id: str):
    """Get a single notebook entry"""
    entry = studio_manager.get_notebook_entry(entry_id)
    if not entry:
        raise HTTPException(status_code=404, detail="Notebook entry not found")
    return entry

@app.put("/api/studio/notebook/{entry_id}", response_model=NotebookEntry)
async def update_notebook_entry(entry_id: str, update_data: NotebookEntryUpdate):
    """Update a notebook entry"""
    entry = studio_manager.update_notebook_entry(entry_id, update_data)
    if not entry:
        raise HTTPException(status_code=404, detail="Notebook entry not found")
    return entry

@app.delete("/api/studio/notebook/{entry_id}")
async def delete_notebook_entry(entry_id: str):
    """Delete a notebook entry"""
    success = studio_manager.delete_notebook_entry(entry_id)
    if not success:
        raise HTTPException(status_code=404, detail="Notebook entry not found")
    return {"status": "success", "message": "Notebook entry deleted"}

@app.post("/api/studio/notebook/export/answer-pdf")
async def export_single_answer_pdf(request: ExportAnswerPdfRequest):
    """Export one Q/A pair as a downloadable PDF."""
    try:
        title = request.title or "NotebookPRO Answer Export"
        pdf_bytes = _build_answers_pdf_bytes(
            title=title,
            space_id=request.space_id,
            items=[{"question": request.question, "answer": request.answer}],
        )
        filename = _safe_pdf_filename(title)
        return Response(
            content=pdf_bytes,
            media_type="application/pdf",
            headers={"Content-Disposition": f'attachment; filename="{filename}"'},
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Failed to export answer PDF: {e}")

@app.post("/api/studio/notebook/export/combined-pdf")
async def export_combined_answers_pdf(request: ExportCombinedAnswersPdfRequest):
    """Export all provided Q/A pairs as one combined downloadable PDF."""
    try:
        if not request.items:
            raise HTTPException(status_code=400, detail="No answers provided for export")

        title = request.chat_title or "NotebookPRO Combined Answers"
        pdf_bytes = _build_answers_pdf_bytes(
            title=title,
            space_id=request.space_id,
            items=[{"question": i.question, "answer": i.answer} for i in request.items],
        )
        filename = _safe_pdf_filename(title)
        return Response(
            content=pdf_bytes,
            media_type="application/pdf",
            headers={"Content-Disposition": f'attachment; filename="{filename}"'},
        )
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Failed to export combined PDF: {e}")

@app.get("/api/studio/notebook/export/entry/{entry_id}/pdf")
async def export_notebook_entry_pdf(entry_id: str):
    """Export one notebook entry as a downloadable PDF."""
    try:
        entry = studio_manager.get_notebook_entry(entry_id)
        if not entry:
            raise HTTPException(status_code=404, detail="Notebook entry not found")

        title = f"Notebook Entry - {entry.title}"
        pdf_bytes = _build_notebook_entries_pdf_bytes(title, [entry], entry.space_id)
        filename = _safe_pdf_filename(entry.title or f"entry_{entry.id}")
        return Response(
            content=pdf_bytes,
            media_type="application/pdf",
            headers={"Content-Disposition": f'attachment; filename="{filename}"'},
        )
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Failed to export notebook entry PDF: {e}")

@app.get("/api/studio/notebook/export/space/{space_id}/combined-pdf")
async def export_space_notebook_pdf(space_id: str):
    """Export all notebook entries for a space as a single PDF."""
    try:
        entries = studio_manager.list_notebook_entries(space_id=space_id)
        if not entries:
            raise HTTPException(status_code=404, detail="No notebook entries found for this space")

        space = spaces_manager.get_space(space_id)
        space_name = space['name'] if space else space_id
        title = f"Notebook - {space_name}"
        pdf_bytes = _build_notebook_entries_pdf_bytes(title, entries, space_id)
        filename = _safe_pdf_filename(f"{space_name}_notebook")
        return Response(
            content=pdf_bytes,
            media_type="application/pdf",
            headers={"Content-Disposition": f'attachment; filename="{filename}"'},
        )
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Failed to export space notebook PDF: {e}")

@app.post("/api/studio/notebook/export/selected-pdf")
async def export_selected_notebook_pdf(request: ExportSelectedNotebookPdfRequest):
    """Export selected notebook entries for a space as a single combined PDF."""
    try:
        if not request.entry_ids:
            raise HTTPException(status_code=400, detail="No notebook entries selected for export")

        entries = []
        for entry_id in request.entry_ids:
            entry = studio_manager.get_notebook_entry(entry_id)
            if not entry:
                raise HTTPException(status_code=404, detail=f"Notebook entry not found: {entry_id}")
            if entry.space_id != request.space_id:
                raise HTTPException(
                    status_code=400,
                    detail=f"Notebook entry does not belong to this space: {entry_id}",
                )
            entries.append(entry)

        if not entries:
            raise HTTPException(status_code=404, detail="No notebook entries found for export")

        space = spaces_manager.get_space(request.space_id)
        space_name = space['name'] if space else request.space_id
        title = request.title or f"Notebook Selection - {space_name}"
        pdf_bytes = _build_notebook_entries_pdf_bytes(title, entries, request.space_id)
        filename = _safe_pdf_filename(f"{space_name}_notebook_selected")

        return Response(
            content=pdf_bytes,
            media_type="application/pdf",
            headers={"Content-Disposition": f'attachment; filename="{filename}"'},
        )
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Failed to export selected notebook PDF: {e}")

# ==================== Flashcard Routes ====================

@app.post("/api/studio/flashcards", response_model=Flashcard)
async def create_flashcard(card_data: FlashcardCreate):
    """Create a new flashcard"""
    try:
        card = studio_manager.create_flashcard(card_data)
        return card
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/studio/flashcards", response_model=List[Flashcard])
async def list_flashcards(
    space_id: Optional[str] = None,
    mastery: Optional[MasteryLevel] = None
):
    """List all flashcards, optionally filtered"""
    try:
        cards = studio_manager.list_flashcards(space_id, mastery)
        return cards
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/studio/flashcards/{card_id}", response_model=Flashcard)
async def get_flashcard(card_id: str):
    """Get a single flashcard"""
    card = studio_manager.get_flashcard(card_id)
    if not card:
        raise HTTPException(status_code=404, detail="Flashcard not found")
    return card

@app.put("/api/studio/flashcards/{card_id}", response_model=Flashcard)
async def update_flashcard(card_id: str, update_data: FlashcardUpdate):
    """Update a flashcard"""
    card = studio_manager.update_flashcard(card_id, update_data)
    if not card:
        raise HTTPException(status_code=404, detail="Flashcard not found")
    return card

@app.post("/api/studio/flashcards/{card_id}/review", response_model=Flashcard)
async def review_flashcard(card_id: str, review: FlashcardReview):
    """Record a flashcard review"""
    card = studio_manager.review_flashcard(card_id, review)
    if not card:
        raise HTTPException(status_code=404, detail="Flashcard not found")
    return card

@app.delete("/api/studio/flashcards/{card_id}")
async def delete_flashcard(card_id: str):
    """Delete a flashcard"""
    success = studio_manager.delete_flashcard(card_id)
    if not success:
        raise HTTPException(status_code=404, detail="Flashcard not found")
    return {"status": "success", "message": "Flashcard deleted"}

@app.post("/api/studio/flashcards/generate", response_model=List[Flashcard])
async def generate_flashcards(request: FlashcardGenerateRequest):
    """Generate flashcards from content using LLM"""
    global studio_generator
    
    if not studio_generator:
        raise HTTPException(status_code=503, detail="LLM not initialized")
    
    try:
        cards = await studio_generator.generate_flashcards(request)
        return cards
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# ==================== Quiz Routes ====================

@app.post("/api/studio/quizzes", response_model=Quiz)
async def create_quiz(quiz_data: QuizCreate):
    """Create a new quiz"""
    try:
        quiz = studio_manager.create_quiz(quiz_data)
        return quiz
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/studio/quizzes", response_model=List[Quiz])
async def list_quizzes(space_id: Optional[str] = None):
    """List all quizzes, optionally filtered by space"""
    try:
        quizzes = studio_manager.list_quizzes(space_id)
        return quizzes
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/studio/quizzes/{quiz_id}", response_model=Quiz)
async def get_quiz(quiz_id: str):
    """Get a quiz by ID"""
    quiz = studio_manager.get_quiz(quiz_id)
    if not quiz:
        raise HTTPException(status_code=404, detail="Quiz not found")
    return quiz

@app.delete("/api/studio/quizzes/{quiz_id}")
async def delete_quiz(quiz_id: str):
    """Delete a quiz"""
    success = studio_manager.delete_quiz(quiz_id)
    if not success:
        raise HTTPException(status_code=404, detail="Quiz not found")
    return {"status": "success", "message": "Quiz deleted"}

@app.post("/api/studio/quizzes/generate", response_model=Quiz)
async def generate_quiz(request: QuizGenerateRequest):
    """Generate a quiz from content using LLM"""
    global studio_generator
    
    if not studio_generator:
        raise HTTPException(status_code=503, detail="LLM not initialized")
    
    try:
        quiz = await studio_generator.generate_quiz(request)
        if not quiz:
            raise HTTPException(status_code=500, detail="Failed to generate quiz")
        return quiz
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/api/studio/quizzes/{quiz_id}/submit", response_model=QuizResult)
async def submit_quiz(quiz_id: str, submission: QuizSubmission):
    """Submit quiz answers and get results"""
    try:
        result = studio_manager.submit_quiz(quiz_id, submission.answers)
        if not result:
            raise HTTPException(status_code=404, detail="Quiz not found")
        return result
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/studio/quizzes/{quiz_id}/history", response_model=QuizHistory)
async def get_quiz_history(quiz_id: str):
    """Get quiz attempt history"""
    try:
        history = studio_manager.get_quiz_history(quiz_id)
        if not history:
            raise HTTPException(status_code=404, detail="Quiz not found")
        return history
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# ==================== Run Server ====================

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860, log_level="error")