File size: 109,904 Bytes
e344fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
7c7b0c4
1f1dd7e
e344fcd
 
1f1dd7e
 
e344fcd
 
 
 
 
 
 
 
a6b66ad
 
 
 
 
 
 
 
 
 
 
e344fcd
a6b66ad
e344fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c563f2f
 
e344fcd
 
 
c563f2f
b1f11a7
37cea3a
e344fcd
66fc039
 
 
 
af9e2c1
 
 
 
1f1dd7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1f11a7
a40763c
b1f11a7
 
 
 
 
 
a40763c
 
b1f11a7
 
a40763c
 
 
b1f11a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a40763c
 
b1f11a7
 
 
a40763c
b1f11a7
 
 
 
a40763c
 
 
 
 
b1f11a7
 
a40763c
 
b1f11a7
 
 
 
a40763c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1f11a7
 
 
 
a40763c
 
b1f11a7
 
a40763c
 
b1f11a7
 
 
a40763c
 
 
 
 
 
b1f11a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6b66ad
 
 
 
 
 
 
df23939
 
 
 
 
 
 
 
 
 
a6b66ad
 
df23939
a6b66ad
 
 
 
df23939
 
 
a6b66ad
 
 
 
 
 
 
 
 
 
66fc039
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af9e2c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66fc039
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af9e2c1
 
66fc039
 
 
 
 
 
 
 
 
 
 
 
 
af9e2c1
 
66fc039
 
 
 
 
 
 
 
 
 
af9e2c1
66fc039
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e344fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48a9d7d
e344fcd
48a9d7d
 
 
 
 
e344fcd
 
 
48a9d7d
 
e344fcd
 
48a9d7d
 
c14e337
 
48a9d7d
c563f2f
48a9d7d
c14e337
48a9d7d
 
 
c14e337
 
c563f2f
48a9d7d
 
 
c563f2f
 
48a9d7d
c563f2f
48a9d7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c563f2f
48a9d7d
 
c563f2f
 
48a9d7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c563f2f
 
 
c14e337
e344fcd
 
 
df23939
 
 
 
 
 
 
c563f2f
e344fcd
 
 
48a9d7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e344fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6b66ad
e344fcd
 
 
 
 
 
df23939
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6b66ad
 
 
 
e344fcd
 
a6b66ad
 
 
 
 
 
 
 
 
e344fcd
 
a6b66ad
e344fcd
 
 
 
a6b66ad
 
 
 
 
 
 
 
 
 
 
 
e344fcd
 
a6b66ad
 
e344fcd
 
 
 
 
7c7b0c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdd95fd
7c7b0c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdd95fd
 
 
7c7b0c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62f2727
e0e9b68
 
 
e344fcd
 
 
e0e9b68
e344fcd
 
 
 
 
 
 
e0e9b68
e344fcd
 
 
 
c563f2f
e344fcd
 
 
e0e9b68
 
 
 
 
 
 
66fc039
 
 
 
 
 
 
 
 
e0e9b68
7c7b0c4
e0e9b68
 
 
 
 
 
 
 
7c7b0c4
e0e9b68
 
 
7c7b0c4
 
66fc039
7c7b0c4
 
 
 
e0e9b68
 
 
7c7b0c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0e9b68
7c7b0c4
 
 
e0e9b68
 
66fc039
e0e9b68
7c7b0c4
 
 
 
 
e0e9b68
 
7c7b0c4
 
e0e9b68
7c7b0c4
e0e9b68
 
7c7b0c4
e0e9b68
 
 
 
 
7c7b0c4
e0e9b68
7c7b0c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdd95fd
 
7c7b0c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f037349
7c7b0c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdd95fd
 
7c7b0c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e344fcd
62f2727
 
e344fcd
 
 
 
 
 
7c7b0c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e344fcd
7c7b0c4
e344fcd
 
c563f2f
 
62f2727
e344fcd
 
 
 
4f8c53c
 
 
 
e344fcd
 
 
 
 
 
4f8c53c
e344fcd
 
 
c563f2f
e344fcd
62f2727
e344fcd
 
c563f2f
e344fcd
 
 
 
 
 
c563f2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48a9d7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af9e2c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f1dd7e
e344fcd
 
 
 
 
c563f2f
e344fcd
 
 
 
c563f2f
 
62f2727
e344fcd
 
 
 
 
e0e9b68
 
 
 
c563f2f
 
 
e0e9b68
 
 
 
 
 
 
1f1dd7e
 
 
 
 
 
 
c563f2f
e344fcd
e0e9b68
 
e344fcd
 
 
 
 
e0e9b68
 
e344fcd
 
 
 
 
 
 
e0e9b68
 
c563f2f
e344fcd
 
 
66fc039
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62f2727
 
 
b1f11a7
e0e9b68
 
f037349
62f2727
 
c563f2f
 
62f2727
e0e9b68
 
 
 
62f2727
 
 
e0e9b68
 
 
 
62f2727
f037349
 
 
 
 
 
 
 
 
 
 
48a9d7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f037349
 
66fc039
e344fcd
62f2727
f037349
66fc039
48a9d7d
 
62f2727
e344fcd
 
 
 
 
 
c563f2f
 
48a9d7d
 
 
 
 
 
c563f2f
62f2727
48a9d7d
 
 
 
 
 
 
 
 
 
62f2727
 
 
f037349
 
48a9d7d
f037349
62f2727
 
 
 
 
b1f11a7
f037349
b1f11a7
62f2727
 
 
e0e9b68
48a9d7d
62f2727
 
 
 
48a9d7d
 
 
62f2727
 
 
 
 
c563f2f
62f2727
e0e9b68
 
 
1f1dd7e
62f2727
1f1dd7e
 
 
62f2727
e0e9b68
 
 
62f2727
 
 
 
e0e9b68
 
 
62f2727
 
 
 
 
e0e9b68
 
62f2727
 
 
 
 
e0e9b68
 
 
62f2727
 
 
 
 
e0e9b68
 
62f2727
c563f2f
 
 
e344fcd
 
c563f2f
e0e9b68
 
c563f2f
e344fcd
 
 
af9e2c1
 
 
 
a40763c
 
 
 
 
 
 
 
 
 
 
 
1f1dd7e
 
 
 
 
a40763c
 
1f1dd7e
 
b1f11a7
 
a40763c
 
 
 
 
 
 
 
 
 
 
b1f11a7
 
 
 
 
 
a40763c
 
 
b1f11a7
 
 
 
 
a40763c
 
 
 
 
 
 
 
 
 
b1f11a7
 
 
a40763c
b1f11a7
 
a40763c
 
 
 
 
 
 
 
b1f11a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af9e2c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f1dd7e
 
af9e2c1
 
 
 
 
 
 
 
 
1f1dd7e
 
 
af9e2c1
 
 
 
 
 
 
1f1dd7e
af9e2c1
 
 
 
e344fcd
4f8c53c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e344fcd
4f8c53c
 
 
e344fcd
4f8c53c
e344fcd
4f8c53c
e344fcd
 
 
4f8c53c
 
 
 
 
 
 
 
e344fcd
 
4f8c53c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e344fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f8c53c
 
 
 
 
4f508d7
4f8c53c
 
 
 
 
 
 
 
 
 
 
 
 
a6b66ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2602b43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37cea3a
4f8c53c
e344fcd
2602b43
4f8c53c
a6b66ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2602b43
e344fcd
 
a6b66ad
 
e344fcd
 
 
a6b66ad
 
 
 
 
 
 
e344fcd
62f2727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6b66ad
 
62f2727
e0e9b68
 
a6b66ad
e0e9b68
 
 
 
a6b66ad
e344fcd
2602b43
e0e9b68
 
 
e344fcd
 
62f2727
 
e0e9b68
af9e2c1
e0e9b68
 
 
 
 
 
 
 
 
 
a6b66ad
 
 
2602b43
a6b66ad
 
 
 
 
 
 
 
e0e9b68
 
 
 
 
 
 
 
a6b66ad
 
e0e9b68
 
 
 
e344fcd
62f2727
 
e0e9b68
 
 
e344fcd
e0e9b68
 
e344fcd
 
 
 
 
 
4f508d7
 
 
e344fcd
 
4f508d7
 
 
 
e344fcd
 
4f508d7
e344fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c563f2f
e344fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdd95fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1f11a7
 
bdd95fd
b1f11a7
bdd95fd
 
 
 
 
 
e344fcd
 
24b611a
 
 
 
 
 
 
 
e344fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0e9b68
 
 
 
 
 
 
 
e344fcd
 
 
 
 
 
 
 
 
 
 
 
 
4f8c53c
 
 
 
 
 
 
 
 
 
 
 
 
 
e344fcd
 
 
 
e0e9b68
 
e344fcd
 
62f2727
 
 
 
 
 
e344fcd
 
 
 
 
 
 
 
 
 
 
 
 
e0e9b68
 
e344fcd
 
 
 
 
66fc039
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e344fcd
 
 
 
 
 
 
 
 
 
 
 
 
4f508d7
e344fcd
66fc039
 
e344fcd
4f508d7
 
 
 
e344fcd
4f508d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e344fcd
 
 
66fc039
e344fcd
66fc039
e344fcd
 
66fc039
 
 
 
 
 
 
 
 
e344fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c563f2f
 
e344fcd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
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
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
#!/usr/bin/env python3
"""
Hugging Face Gradio App for RDF Validation with MCP Server and Anthropic AI

This app serves both as a web interface and can expose MCP server functionality.
Deploy this on Hugging Face Spaces with your Anthropic API key.
"""

import gradio as gr
import os
import json
import sys
import asyncio
import logging
import re
import hashlib
import threading
import time
from collections import OrderedDict
from typing import Any, Dict, List, Optional

# Add current directory to path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

# Import our validation logic
try:
    from validator import validate_rdf
    VALIDATOR_AVAILABLE = True
    # Test that the function is callable
    if not callable(validate_rdf):
        print("⚠️ Warning: validate_rdf is not callable")
        VALIDATOR_AVAILABLE = False
    else:
        print("βœ… Validator module loaded successfully")
except ImportError as e:
    VALIDATOR_AVAILABLE = False
    print(f"⚠️ Warning: validator.py not found or has import errors: {e}")
    print("Some features may be limited.")
except Exception as e:
    VALIDATOR_AVAILABLE = False
    print(f"⚠️ Warning: Error loading validator: {e}")

# Optional: Check if OpenAI and requests are available
try:
    from openai import OpenAI
    OPENAI_AVAILABLE = True
except ImportError:
    OPENAI_AVAILABLE = False
    print("πŸ’‘ Install 'openai' package for AI-powered corrections: pip install openai")

try:
    import requests
    HF_INFERENCE_AVAILABLE = True
except ImportError:
    HF_INFERENCE_AVAILABLE = False
    print("πŸ’‘ Install 'requests' package for AI-powered corrections: pip install requests")

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Configuration - Your specific Hugging Face Inference Endpoint (hardcoded)
HF_API_KEY = os.getenv('HF_API_KEY', '')  # Hugging Face API key from Secret
HF_ENDPOINT_URL = "https://evxgv66ksxjlfrts.us-east-1.aws.endpoints.huggingface.cloud/v1/"
HF_MODEL = "lmstudio-community/Llama-3.3-70B-Instruct-GGUF"  # Correct model name for your endpoint

# AI Correction Configuration
MAX_CORRECTION_ATTEMPTS = 2  # Reduced for speed (rapid fix handles most cases)
ENABLE_VALIDATION_LOOP = True  # Enable validation loop by default

# MCP4BibFrame Documentation API Configuration
MCP4BIBFRAME_DOCS_URL = "https://jimfhahn-mcp4bibframe-docs.hf.space/api/mcp"
MCP4BIBFRAME_DOCS_ENABLED = True  # Set to False to disable doc integration

# Cache BibFrame documentation responses to avoid repeated network calls
BIBFRAME_DOCS_CACHE: Dict[str, tuple[Any, float]] = {}
BIBFRAME_DOCS_CACHE_TTL = 3600  # seconds

# Cache successful correction outputs to accelerate repeated error patterns
FIX_CACHE: OrderedDict[str, str] = OrderedDict()
FIX_CACHE_MAX_SIZE = 100


def _make_fix_cache_key(validation_results: str, rdf_content: str, template: str) -> str:
    """Generate a deterministic cache key for correction attempts."""
    hasher = hashlib.sha256()
    hasher.update(template.strip().encode("utf-8"))
    hasher.update(b"\x1f")
    hasher.update(validation_results.strip().encode("utf-8", errors="ignore"))
    hasher.update(b"\x1f")
    hasher.update(rdf_content.strip().encode("utf-8", errors="ignore"))
    return hasher.hexdigest()


def _get_cached_correction(cache_key: str, steps_log: Optional[List[str]] = None) -> Optional[str]:
    """Retrieve a cached correction, updating its recency ordering."""
    cached = FIX_CACHE.get(cache_key)
    if cached is not None:
        FIX_CACHE.move_to_end(cache_key)
        if steps_log is not None:
            steps_log.append("Using cached correction for repeated validation errors")
    return cached


def _store_correction_in_cache(cache_key: str, corrected_rdf: str, steps_log: Optional[List[str]] = None) -> None:
    """Store a correction in the cache and evict the oldest entry if needed."""
    if not corrected_rdf:
        return
    FIX_CACHE[cache_key] = corrected_rdf
    FIX_CACHE.move_to_end(cache_key)
    if len(FIX_CACHE) > FIX_CACHE_MAX_SIZE:
        removed_key, _ = FIX_CACHE.popitem(last=False)
        if steps_log is not None:
            steps_log.append("Cache full; evicted oldest correction entry")
    elif steps_log is not None:
        steps_log.append("Cached correction for future reuse")

# Cache successful correction outputs to accelerate repeated error patterns
FIX_CACHE: OrderedDict[str, str] = OrderedDict()
FIX_CACHE_MAX_SIZE = 100


def rapid_fix_missing_properties(rdf_content: str, validation_results: str, template: str, steps_log: Optional[List[str]] = None) -> Optional[str]:
    """Ultra-fast fix for simple missing property errors - no AI needed."""
    import re
    
    # Quick pattern match for missing properties
    missing = re.findall(r"Less than \d+ values on.*->bf:(\w+)", validation_results)
    if not missing:
        if steps_log:
            steps_log.append("❌ Rapid fix: No missing properties detected in validation results")
        return None
    
    if steps_log:
        steps_log.append(f"πŸ” Rapid fix detected {len(missing)} missing properties: {', '.join(set(missing))}")
    
    # Pre-compiled property templates (no API calls)
    INSTANT_FIXES = {
        "title": '<bf:title><bf:Title><bf:mainTitle>Untitled</bf:mainTitle></bf:Title></bf:title>',
        "language": '<bf:language><bf:Language rdf:about="http://id.loc.gov/vocabulary/languages/eng"><rdfs:label>English</rdfs:label><bf:code>eng</bf:code></bf:Language></bf:language>',
        "content": '<bf:content><bf:Content rdf:about="http://id.loc.gov/vocabulary/contentTypes/txt"><rdfs:label>text</rdfs:label><bf:code>txt</bf:code></bf:Content></bf:content>',
        "adminMetadata": '''<bf:adminMetadata>
    <bf:AdminMetadata>
        <bf:status>
            <bf:Status rdf:about="http://id.loc.gov/vocabulary/mstatus/n">
                <rdfs:label>new</rdfs:label>
                <bf:code>n</bf:code>
            </bf:Status>
        </bf:status>
        <bf:date rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2024-01-01</bf:date>
        <bf:agent>
            <bf:Agent rdf:about="http://id.loc.gov/vocabulary/organizations/dlc">
                <rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Organization"/>
                <rdfs:label>Library of Congress</rdfs:label>
            </bf:Agent>
        </bf:agent>
        <bf:assigner>
            <bf:Agent rdf:about="http://id.loc.gov/vocabulary/organizations/dlc">
                <rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Organization"/>
                <rdfs:label>Library of Congress</rdfs:label>
            </bf:Agent>
        </bf:assigner>
    </bf:AdminMetadata>
</bf:adminMetadata>''',
        "assigner": '''<bf:assigner>
    <bf:Agent rdf:about="http://id.loc.gov/vocabulary/organizations/dlc">
        <rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Organization"/>
        <rdfs:label>Library of Congress</rdfs:label>
    </bf:Agent>
</bf:assigner>'''
    }
    
    # Find insertion point
    work_match = re.search(r'(<bf:Work[^>]*>)(.*?)(</bf:Work>)', rdf_content, re.DOTALL)
    instance_match = re.search(r'(<bf:Instance[^>]*>)(.*?)(</bf:Instance>)', rdf_content, re.DOTALL)
    
    if not work_match and not instance_match:
        if steps_log:
            steps_log.append("❌ Rapid fix: No bf:Work or bf:Instance found in RDF")
        return None
    
    match = work_match or instance_match
    target_type = "Work" if work_match else "Instance"
    opening_tag = match.group(1)
    content = match.group(2)
    closing_tag = match.group(3)
    
    if steps_log:
        steps_log.append(f"πŸ“ Rapid fix target: bf:{target_type}")
        has_admin = "<bf:adminMetadata>" in content or "<bf:AdminMetadata>" in content
        steps_log.append(f"πŸ” Current state: AdminMetadata {'EXISTS' if has_admin else 'MISSING'}")
    
    # Build fixes
    fixes = []
    assigner_fixed = False
    
    for prop in missing[:10]:  # Limit to 10 properties
        prop_lower = prop.lower()
        
        # Special handling for assigner within AdminMetadata
        if prop_lower == "assigner":
            if steps_log:
                steps_log.append("πŸ”§ Processing missing 'assigner' property...")
            # Look for existing AdminMetadata blocks that need assigner
            admin_pattern = re.compile(r'(<bf:AdminMetadata[^>]*>)(.*?)(</bf:AdminMetadata>)', re.DOTALL)
            
            def add_assigner(match):
                nonlocal assigner_fixed
                admin_open = match.group(1)
                admin_content = match.group(2)
                admin_close = match.group(3)
                
                # Skip if already has assigner
                if '<bf:assigner' in admin_content:
                    return match.group(0)
                
                # Extract agent URI if present to reuse for assigner
                agent_uri = None
                agent_match = re.search(r'<bf:agent\s+rdf:resource="([^"]+)"', admin_content)
                if not agent_match:
                    agent_match = re.search(r'<bf:agent[^>]*>\s*<[^>]+\s+rdf:about="([^"]+)"', admin_content)
                if agent_match:
                    agent_uri = agent_match.group(1)
                
                # Build assigner element
                if agent_uri:
                    assigner_element = f'        <bf:assigner rdf:resource="{agent_uri}"/>'
                else:
                    # Use default Library of Congress
                    assigner_element = '''        <bf:assigner>
            <bf:Agent rdf:about="http://id.loc.gov/vocabulary/organizations/dlc">
                <rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Organization"/>
                <rdfs:label>Library of Congress</rdfs:label>
            </bf:Agent>
        </bf:assigner>'''
                
                assigner_fixed = True
                if steps_log:
                    steps_log.append(f"   βœ… Injected assigner into existing AdminMetadata (agent URI: {agent_uri or 'default'})")
                # Insert before closing tag
                return admin_open + admin_content + '\n' + assigner_element + '\n    ' + admin_close
            
            original_content = content
            content = admin_pattern.sub(add_assigner, content)
            
            if assigner_fixed and steps_log:
                steps_log.append("   βœ… Assigner successfully added to existing AdminMetadata")
            elif steps_log and content == original_content:
                steps_log.append("   ℹ️  No AdminMetadata found to inject assigner (will add with full block if adminMetadata is missing)")
            
        elif prop in INSTANT_FIXES and f"<bf:{prop}" not in content:
            fixes.append(INSTANT_FIXES[prop])
            if steps_log:
                steps_log.append(f"   βœ… Will add missing '{prop}' property")
        elif prop in INSTANT_FIXES:
            if steps_log:
                steps_log.append(f"   ℹ️  Property '{prop}' already exists, skipping")
        elif steps_log:
            steps_log.append(f"   ⚠️  No template for '{prop}', skipping")
    
    if not fixes and not assigner_fixed:
        if steps_log:
            steps_log.append("❌ Rapid fix: No properties could be fixed")
        return None
    
    # Insert all at once
    if fixes:
        if steps_log:
            steps_log.append(f"πŸ”¨ Adding {len(fixes)} missing properties to {target_type}")
        fixed_content = opening_tag + content + '\n    ' + '\n    '.join(fixes) + '\n' + closing_tag
    else:
        if steps_log:
            steps_log.append(f"πŸ”¨ Modified content (assigner injection only)")
        fixed_content = opening_tag + content + closing_tag
    
    # Replace in original RDF
    result = rdf_content.replace(match.group(0), fixed_content)
    
    if steps_log:
        steps_log.append(f"βœ… Rapid fix complete: Added {len(fixes)} properties, assigner_injected={assigner_fixed}")
    
    return result


def get_ai_correction_minimal(errors: str, rdf: str, max_tokens: int = 800) -> str:
    """Ultra-minimal prompt for faster AI response."""
    
    if not OPENAI_AVAILABLE or not os.getenv('HF_API_KEY'):
        return rdf
    
    try:
        client = get_openai_client()
        if not client:
            return rdf
        
        # Extract just the critical errors
        error_lines = []
        for line in errors.split('\n'):
            if any(term in line for term in ['Less than', 'missing', 'required', '->bf:', 'adminMetadata', 'assigner']):
                error_lines.append(line.strip()[:100])
                if len(error_lines) >= 5:
                    break
        
        if not error_lines:
            return rdf
        
        # Ultra-concise prompt
        prompt = f"""Fix these BibFrame errors:

{chr(10).join(error_lines[:3])}

Add only what's missing to this RDF:
{rdf[:800]}...{rdf[-200:] if len(rdf) > 1000 else ''}

Return complete valid RDF/XML only."""
        
        response = client.chat.completions.create(
            model=HF_MODEL,
            messages=[
                {"role": "system", "content": "Fix RDF. Output only valid RDF/XML. No explanations."},
                {"role": "user", "content": prompt}
            ],
            max_tokens=max_tokens,
            temperature=0,
            timeout=20  # Much shorter timeout
        )
        
        result = response.choices[0].message.content
        result = extract_rdf_from_response(result)
        result = fix_common_rdf_errors(result)
        
        return result
        
    except Exception:
        return rdf


def test_validator_functionality():
    """Test if the validator is actually working"""
    if not VALIDATOR_AVAILABLE:
        print("❌ Validator not available for testing")
        return False
    
    try:
        # Test with minimally valid RDF/XML that matches SHACL targets but is missing required properties
        # This ensures SHACL finds focus nodes (bf:Text Work) and reports violations
        test_rdf = '''<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:bf="http://id.loc.gov/ontologies/bibframe/">
  <bf:Work rdf:about="http://example.org/work/1">
    <rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Text"/>
    <!-- Intentionally missing title, language, content, adminMetadata to trigger SHACL violations -->
  </bf:Work>
</rdf:RDF>'''
        conforms, results = validate_rdf(test_rdf.encode('utf-8'), 'monograph')
        
        # This should fail validation due to missing required properties
        if conforms:
            print("⚠️ WARNING: Validator returned 'conforms=True' for invalid RDF. Validator may not be working correctly!")
            return False
        else:
            preview = (results or '').strip()
            preview = preview[:200] + ('…' if len(preview) > 200 else '')
            print(f"βœ… Validator test passed. Got expected SHACL violations. Preview: {preview if preview else 'No results text returned'}")
            return True
            
    except Exception as e:
        print(f"❌ Validator test failed with error: {e}")
        return False

# Run the test on startup
if VALIDATOR_AVAILABLE:
    test_validator_functionality()

def query_bibframe_docs(tool_name: str, params: dict, timeout: int = 10) -> Optional[dict]:
    """
    Query the MCP4BibFrame documentation API using the MCP protocol.
    
    Args:
        tool_name (str): Name of the tool to invoke
        params (dict): Parameters for the tool
        timeout (int): Request timeout in seconds
        
    Returns:
        Optional[dict]: Response data or None if failed
    """
    if not MCP4BIBFRAME_DOCS_ENABLED:
        return None
        
    try:
        # Construct MCP request
        mcp_request = {
            "jsonrpc": "2.0",
            "method": "tools/call",
            "params": {
                "name": tool_name,
                "arguments": params
            },
            "id": 1
        }
        
        logger.info(f"Querying BibFrame docs: {tool_name} with {params}")
        
        # Make SSE request to MCP endpoint
        response = requests.post(
            MCP4BIBFRAME_DOCS_URL,
            json=mcp_request,
            timeout=timeout,
            headers={"Accept": "text/event-stream"}
        )
        
        if response.status_code == 200:
            # Parse SSE response
            for line in response.text.split('\n'):
                if line.startswith('data: '):
                    try:
                        data = json.loads(line[6:])
                        if 'result' in data:
                            return data['result']
                    except json.JSONDecodeError:
                        continue
        else:
            logger.warning(f"BibFrame docs API returned status {response.status_code}")
            
    except requests.exceptions.Timeout:
        logger.warning("Timeout querying BibFrame documentation")
    except Exception as e:
        logger.error(f"Error querying BibFrame documentation: {str(e)}")
    
    return None


def query_bibframe_docs_cached(tool_name: str, params: dict, timeout: int = 10) -> Optional[dict]:
    """Cached wrapper around ``query_bibframe_docs`` to avoid repeated HTTP calls."""
    if not MCP4BIBFRAME_DOCS_ENABLED:
        return None

    try:
        cache_key = f"{tool_name}:{json.dumps(params, sort_keys=True)}"
    except TypeError:
        cache_key = f"{tool_name}:{str(params)}"

    cached = BIBFRAME_DOCS_CACHE.get(cache_key)
    if cached:
        payload, timestamp = cached
        if time.time() - timestamp < BIBFRAME_DOCS_CACHE_TTL:
            logger.debug(f"Using cached BibFrame docs response for {cache_key}")
            return payload

    response = query_bibframe_docs(tool_name, params, timeout)
    if response is not None:
        BIBFRAME_DOCS_CACHE[cache_key] = (response, time.time())

    return response

def extract_bibframe_terms_from_errors(validation_results: str) -> dict:
    """
    Extract BibFrame properties and classes mentioned in validation errors.
    
    Args:
        validation_results (str): Validation error text
        
    Returns:
        dict: Dictionary with 'properties' and 'classes' lists
    """
    import re
    
    terms = {
        'properties': set(),
        'classes': set()
    }
    
    # Common patterns in validation results
    # Properties often appear as bf:propertyName or ->bf:propertyName
    property_patterns = [
        r'bf:(\w+)',
        r'->bf:(\w+)',
        r'property (\w+)',
        r'missing (\w+)',
        r'requires? (\w+)'
    ]
    
    # Classes often appear as bf:ClassName or "a ClassName"
    class_patterns = [
        r'bf:([A-Z]\w+)',
        r'type ([A-Z]\w+)',
        r'class ([A-Z]\w+)',
        r'<bf:([A-Z]\w+)',
        r'a ([A-Z]\w+)'
    ]
    
    text = validation_results.lower()
    
    # Extract properties
    for pattern in property_patterns:
        matches = re.findall(pattern, validation_results, re.IGNORECASE)
        for match in matches:
            if match and len(match) > 2:  # Skip very short matches
                terms['properties'].add(match.lower())
    
    # Extract classes
    for pattern in class_patterns:
        matches = re.findall(pattern, validation_results)
        for match in matches:
            if match and len(match) > 2:
                terms['classes'].add(match)
    
    # Convert sets to lists
    terms['properties'] = list(terms['properties'])[:5]  # Limit to top 5
    terms['classes'] = list(terms['classes'])[:3]  # Limit to top 3
    
    return terms

def fetch_bibframe_guidance(validation_results: str, rdf_content: str) -> str:
    """
    Fetch relevant BibFrame guidance from the documentation API based on errors.
    
    Args:
        validation_results (str): Validation error messages
        rdf_content (str): Original RDF content
        
    Returns:
        str: Formatted guidance text for inclusion in prompts
    """
    if not MCP4BIBFRAME_DOCS_ENABLED:
        return ""
    
    guidance_parts = []
    
    try:
        # Extract terms from validation errors
        terms = extract_bibframe_terms_from_errors(validation_results)
        logger.info(f"Extracted terms - properties: {terms['properties']}, classes: {terms['classes']}")
        
        # Query information for key properties
        for prop in terms['properties'][:3]:  # Limit queries
            prop_uri = _resolve_bibframe_uri(prop)
            result = query_bibframe_docs_cached("get_property_info", {"property_uri": prop_uri})
            if result and isinstance(result, dict):
                guidance_parts.append(f"\n**{result.get('label', prop)}** ({prop}):")
                if 'definition' in result:
                    guidance_parts.append(f"- Definition: {result['definition']}")
                if 'domain' in result:
                    guidance_parts.append(f"- Used in: {', '.join(result['domain'])}")
                if 'range' in result:
                    guidance_parts.append(f"- Values: {', '.join(result['range'])}")
                if 'examples' in result and result['examples']:
                    guidance_parts.append(f"- Example: {result['examples'][0]}")
        
        # Query information for key classes
        for cls in terms['classes'][:2]:  # Limit queries
            cls_uri = _resolve_bibframe_uri(cls)
            result = query_bibframe_docs_cached("get_class_info", {"class_uri": cls_uri})
            if result and isinstance(result, dict):
                guidance_parts.append(f"\n**{result.get('label', cls)}** class:")
                if 'definition' in result:
                    guidance_parts.append(f"- Definition: {result['definition']}")
                if 'applicable_properties' in result:
                    props = [p.get('label', p.get('property', '')) for p in result['applicable_properties'][:5]]
                    guidance_parts.append(f"- Key properties: {', '.join(props)}")
        
        # If we found AdminMetadata issues, get specific usage guidance
        if any(term in validation_results.lower() for term in ['adminmetadata', 'assigner', '->bf:assigner']):
            result = query_bibframe_docs_cached("get_property_usage", {
                "property_name": "assigner",
                "class_name": "AdminMetadata"
            })
            if result and isinstance(result, dict):
                guidance_parts.append("\n**AdminMetadata/assigner usage:**")
                if 'usage' in result:
                    guidance_parts.append(f"- {result['usage']}")
                if 'examples' in result and result['examples']:
                    guidance_parts.append(f"- Pattern: {result['examples'][0]}")
        
    except Exception as e:
        logger.error(f"Error fetching BibFrame guidance: {str(e)}")
    
    if guidance_parts:
        return "\n".join(guidance_parts)
    return ""

# OpenAI client configuration for the endpoint
def get_openai_client():
    """Get configured OpenAI client for HF Inference Endpoint"""
    if not HF_API_KEY:
        print("❌ No HF_API_KEY available for OpenAI client")
        return None
    
    print(f"πŸ”— Creating OpenAI client with:")
    print(f"   base_url: {HF_ENDPOINT_URL}")
    print(f"   api_key: {'***' + HF_API_KEY[-4:] if len(HF_API_KEY) > 4 else 'HIDDEN'}")
    
    return OpenAI(
        base_url=HF_ENDPOINT_URL,
        api_key=HF_API_KEY,
        timeout=120.0  # Increase timeout for cold starts
    )

# Sample RDF data for examples (based on real Library of Congress BibFrame)
SAMPLE_VALID_RDF = '''<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:bf="http://id.loc.gov/ontologies/bibframe/"
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
         xmlns:bflc="http://id.loc.gov/ontologies/bflc/"
         xmlns:madsrdf="http://www.loc.gov/mads/rdf/v1#">
    
    <bf:Work rdf:about="http://example.org/work/1">
        <rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Text"/>
        <rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Monograph"/>
        
        <bf:title>
            <bf:Title>
                <bf:mainTitle>The knitter's handy book of patterns</bf:mainTitle>
                <bf:subtitle>basic designs in multiple sizes & gauges</bf:subtitle>
            </bf:Title>
        </bf:title>
        
        <bf:contribution>
            <bf:PrimaryContribution>
                <bf:agent>
                    <bf:Agent rdf:about="http://id.loc.gov/rwo/agents/n2001017606">
                        <rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Person"/>
                        <rdfs:label>Budd, Ann, 1956-</rdfs:label>
                    </bf:Agent>
                </bf:agent>
                <bf:role>
                    <bf:Role rdf:about="http://id.loc.gov/vocabulary/relators/aut">
                        <rdfs:label>author</rdfs:label>
                        <bf:code>aut</bf:code>
                    </bf:Role>
                </bf:role>
            </bf:PrimaryContribution>
        </bf:contribution>
        
        <bf:language>
            <bf:Language rdf:about="http://id.loc.gov/vocabulary/languages/eng">
                <rdfs:label xml:lang="en">English</rdfs:label>
                <bf:code rdf:datatype="http://www.w3.org/2001/XMLSchema#string">eng</bf:code>
            </bf:Language>
        </bf:language>
        
        <bf:content>
            <bf:Content rdf:about="http://id.loc.gov/vocabulary/contentTypes/txt">
                <rdfs:label>text</rdfs:label>
                <bf:code>txt</bf:code>
            </bf:Content>
        </bf:content>
        
        <bf:classification>
            <bf:ClassificationLcc>
                <bf:classificationPortion>TT820</bf:classificationPortion>
                <bf:itemPortion>.B877 2002</bf:itemPortion>
                <bf:assigner>
                    <bf:Organization rdf:about="http://id.loc.gov/vocabulary/organizations/dlc">
                        <rdfs:label>United States, Library of Congress</rdfs:label>
                    </bf:Organization>
                </bf:assigner>
            </bf:ClassificationLcc>
        </bf:classification>
        
        <bf:adminMetadata>
            <bf:AdminMetadata>
                <bf:status>
                    <bf:Status rdf:about="http://id.loc.gov/vocabulary/mstatus/n">
                        <rdfs:label>new</rdfs:label>
                        <bf:code>n</bf:code>
                    </bf:Status>
                </bf:status>
                <bf:date rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2001-12-12</bf:date>
                <bf:agent>
                    <bf:Agent rdf:about="http://id.loc.gov/vocabulary/organizations/dlc">
                        <rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Organization"/>
                        <rdfs:label>United States, Library of Congress</rdfs:label>
                    </bf:Agent>
                </bf:agent>
            </bf:AdminMetadata>
        </bf:adminMetadata>
    </bf:Work>
    
</rdf:RDF>'''

SAMPLE_INVALID_RDF = '''<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:bf="http://id.loc.gov/ontologies/bibframe/">
    <!-- Well-formed RDF/XML, but missing required properties to trigger SHACL violations -->
    <bf:Work rdf:about="http://example.org/work/invalid-1">
        <!-- Ensure target class is hit so SHACL runs -->
        <rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Text"/>
        <!-- Missing proper title structure, language, content, adminMetadata -->
        <bf:title>Incomplete Title</bf:title>
    </bf:Work>
</rdf:RDF>'''

# BibFrame Few-Shot Examples (based on real Library of Congress records)
BIBFRAME_CORRECTION_EXAMPLES = {
    "title_structure": {
        "pattern": r"bf:title",
        "wrong": """<bf:title>Simple Title String</bf:title>""",
        "correct": """<bf:title>
    <bf:Title>
        <bf:mainTitle>The knitter's handy book of patterns</bf:mainTitle>
        <bf:subtitle>basic designs in multiple sizes & gauges</bf:subtitle>
    </bf:Title>
</bf:title>"""
    },
    "adminmetadata": {
        "pattern": r"bf:adminMetadata|->bf:assigner",
        "wrong": """<bf:adminMetadata>
    <bf:AdminMetadata>
        <bf:agent rdf:resource="http://example.org/org"/>
        <bf:status>new</bf:status>
    </bf:AdminMetadata>
</bf:adminMetadata>""",
        "correct": """<bf:adminMetadata>
    <bf:AdminMetadata>
        <bf:status>
            <bf:Status rdf:about="http://id.loc.gov/vocabulary/mstatus/n">
                <rdfs:label>new</rdfs:label>
                <bf:code>n</bf:code>
            </bf:Status>
        </bf:status>
        <bf:date rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2001-12-12</bf:date>
        <bf:agent>
            <bf:Agent rdf:about="http://id.loc.gov/vocabulary/organizations/dlc">
                <rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Organization"/>
                <rdfs:label>United States, Library of Congress</rdfs:label>
            </bf:Agent>
        </bf:agent>
    </bf:AdminMetadata>
</bf:adminMetadata>"""
    },
    "contribution": {
        "pattern": r"bf:contribution",
        "wrong": """<bf:contribution>Author Name</bf:contribution>""",
        "correct": """<bf:contribution>
    <bf:PrimaryContribution>
        <bf:agent>
            <bf:Agent rdf:about="http://id.loc.gov/rwo/agents/n2001017606">
                <rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Person"/>
                <rdfs:label>Budd, Ann, 1956-</rdfs:label>
            </bf:Agent>
        </bf:agent>
        <bf:role>
            <bf:Role rdf:about="http://id.loc.gov/vocabulary/relators/ctb">
                <rdfs:label>contributor</rdfs:label>
                <bf:code>ctb</bf:code>
            </bf:Role>
        </bf:role>
    </bf:PrimaryContribution>
</bf:contribution>"""
    },
    "language": {
        "pattern": r"bf:language",
        "wrong": """<bf:language>English</bf:language>""",
        "correct": """<bf:language>
    <bf:Language rdf:about="http://id.loc.gov/vocabulary/languages/eng">
        <rdfs:label xml:lang="en">English</rdfs:label>
        <bf:code rdf:datatype="http://www.w3.org/2001/XMLSchema#string">eng</bf:code>
    </bf:Language>
</bf:language>"""
    },
    "content": {
        "pattern": r"bf:content",
        "wrong": """<bf:content>Text</bf:content>""",
        "correct": """<bf:content>
    <bf:Content rdf:about="http://id.loc.gov/vocabulary/contentTypes/txt">
        <rdfs:label>text</rdfs:label>
        <bf:code>txt</bf:code>
    </bf:Content>
</bf:content>"""
    },
    "classification": {
        "pattern": r"bf:classification",
        "wrong": """<bf:classification>TT820 .B877 2002</bf:classification>""",
        "correct": """<bf:classification>
    <bf:ClassificationLcc>
        <bf:classificationPortion>TT820</bf:classificationPortion>
        <bf:itemPortion>.B877 2002</bf:itemPortion>
        <bf:assigner>
            <bf:Organization rdf:about="http://id.loc.gov/vocabulary/organizations/dlc">
                <rdfs:label>United States, Library of Congress</rdfs:label>
                <bf:code rdf:datatype="http://id.loc.gov/datatypes/orgs/code">DLC</bf:code>
            </bf:Organization>
        </bf:assigner>
        <bf:status>
            <bf:Status rdf:about="http://id.loc.gov/vocabulary/mstatus/uba">
                <rdfs:label>used by assigner</rdfs:label>
                <bf:code>uba</bf:code>
            </bf:Status>
        </bf:status>
    </bf:ClassificationLcc>
</bf:classification>"""
    },
    "subject": {
        "pattern": r"bf:subject",
        "wrong": """<bf:subject>Knitting--Patterns</bf:subject>""",
        "correct": """<bf:subject>
    <bf:Topic rdf:about="http://id.loc.gov/authorities/subjects/sh85072708">
        <rdfs:label xml:lang="en">Knitting--Patterns</rdfs:label>
        <madsrdf:componentList rdf:parseType="Collection">
            <madsrdf:Authority>
                <madsrdf:authoritativeLabel xml:lang="en">Knitting</madsrdf:authoritativeLabel>
                <madsrdf:elementList>
                    <madsrdf:TopicElement>
                        <madsrdf:elementValue xml:lang="en">Knitting</madsrdf:elementValue>
                    </madsrdf:TopicElement>
                </madsrdf:elementList>
            </madsrdf:Authority>
        </madsrdf:componentList>
    </bf:Topic>
</bf:subject>"""
    }
}

# MCP Server Tools (can be used independently)
def validate_rdf_tool(rdf_content: str, template: str = "monograph") -> dict:
    """
    Validate RDF/XML content against SHACL templates.
    
    This tool validates RDF/XML data against predefined SHACL shapes to ensure
    compliance with metadata standards like BIBFRAME. Returns detailed validation
    results with conformance status and specific violation information.
    
    Args:
        rdf_content (str): The RDF/XML content to validate
        template (str): Validation template to use ('monograph' or 'custom')
    
    Returns:
        dict: Validation results with conformance status and detailed feedback
    """
    if not rdf_content:
        return {"error": "No RDF/XML content provided", "conforms": False}
    
    if not VALIDATOR_AVAILABLE:
        logger.error("Validator module not available")
        return {
            "error": "Validator not available - ensure validator.py is present",
            "conforms": False
        }
    
    try:
        # Fast syntax check before SHACL to give clearer errors on XML/prefix issues
        try:
            try:
                import rdflib  # type: ignore
            except ImportError:
                rdflib = None  # type: ignore
            if rdflib:
                g = rdflib.Graph()  # type: ignore
                # Parse as RDF/XML; raise on syntax errors like unbound prefixes
                g.parse(data=rdf_content, format="application/rdf+xml")  # type: ignore
            else:
                logger.info("rdflib not installed; skipping pre-parse RDF/XML syntax check")
        except Exception as parse_err:
            logger.error(f"RDF/XML parse error before validation: {parse_err}")
            return {
                "error": f"RDF/XML parse error: {parse_err}",
                "conforms": False
            }
        
        # Log what we're validating
        logger.info(f"Validating RDF with template '{template}', content length: {len(rdf_content)}")
        
        # Call the validator
        conforms, results_text = validate_rdf(rdf_content.encode('utf-8'), template)
        
        # Debug logging
        logger.info(f"Validation result - conforms: {conforms}, results length: {len(results_text) if results_text else 0}")
        
        # If no results text but claims to conform, something might be wrong
        if conforms and (not results_text or len(results_text.strip()) == 0):
            results_text = "Validation passed with no specific feedback."
        elif not conforms and (not results_text or len(results_text.strip()) == 0):
            results_text = "Validation failed but no specific errors were returned. Check the RDF syntax and structure."
            
        return {
            "conforms": conforms,
            "results": results_text if results_text else "",
            "template": template,
            "status": "βœ… Valid RDF" if conforms else "❌ Invalid RDF"
        }
        
    except ImportError as e:
        logger.error(f"Import error in validator: {str(e)}")
        return {
            "error": f"Validator import error: {str(e)}. Check that all dependencies are installed.",
            "conforms": False
        }
    except AttributeError as e:
        logger.error(f"Validator function not found: {str(e)}")
        return {
            "error": f"Validator function error: {str(e)}. Check validator.py implementation.",
            "conforms": False
        }
    except Exception as e:
        logger.error(f"Validation error: {str(e)}")
        import traceback
        logger.error(f"Full traceback: {traceback.format_exc()}")
        return {
            "error": f"Validation failed: {str(e)}",
            "conforms": False
        }

def filter_validation_results_by_class(validation_results: str, rdf_content: str) -> dict:
    """
    Filter validation results by RDF class (Work, Instance, etc.)
    
    Args:
        validation_results (str): Full validation results
        rdf_content (str): Original RDF content
        
    Returns:
        dict: Validation results organized by class
    """
    import re
    
    # Parse validation results to extract class information
    class_results = {
        'Work': [],
        'Instance': [],
        'Title': [],
        'Contribution': [],
        'AdminMetadata': [],
        'Other': []
    }
    
    lines = validation_results.split('\n')
    current_section = []
    current_class = 'Other'
    
    for line in lines:
        # Detect which class this error relates to
        if 'bf:Work' in line or '/work/' in line:
            current_class = 'Work'
        elif 'bf:Instance' in line or '/instance/' in line:
            current_class = 'Instance'
        elif 'bf:Title' in line:
            current_class = 'Title'
        elif 'bf:Contribution' in line:
            current_class = 'Contribution'
        elif 'bf:AdminMetadata' in line or 'AdminMetadata' in line or '->bf:assigner' in line:
            # Many admin violations show assigner path; map to AdminMetadata
            current_class = 'AdminMetadata'
        
        # Collect lines for current violation
        if 'Constraint Violation' in line:
            if current_section:
                class_results[current_class].extend(current_section)
            current_section = [line]
        elif line.strip():
            current_section.append(line)
    
    # Add last section
    if current_section:
        class_results[current_class].extend(current_section)
    
    # Remove empty classes
    return {k: '\n'.join(v) for k, v in class_results.items() if v}

def get_ai_suggestions(validation_results: str, rdf_content: str, include_warnings: bool = False) -> str:
    """Generate AI-powered, plain-language suggestions based on validation results.

    Avoids RDF/SHACL jargon and focuses on actionable fixes.
    """
    if not OPENAI_AVAILABLE:
        return generate_manual_suggestions(validation_results)

    current_api_key = os.getenv('HF_API_KEY', '')
    if not current_api_key:
        return f"""
πŸ”‘ **AI suggestions disabled**: Please set your Hugging Face API key as a Secret in your Space settings.

{generate_manual_suggestions(validation_results)}
"""

    try:
        client = get_openai_client()
        if not client:
            return f"""
πŸ”‘ **AI suggestions disabled**: HF_API_KEY not configured.

{generate_manual_suggestions(validation_results)}
"""

        severity_instruction = (
            "Focus only on violations (errors) and ignore any warnings."
            if not include_warnings else
            "Address both violations and warnings."
        )

        # Get BibFrame documentation for context
        bibframe_guidance = fetch_bibframe_guidance(validation_results, rdf_content)
        doc_section = ""
        if bibframe_guidance:
            doc_section = f"""
Reference information from BibFrame ontology:
{bibframe_guidance}
"""

        # Group errors by class to focus the prompt
        class_results = filter_validation_results_by_class(validation_results, rdf_content)
        if class_results:
            primary_class = max(class_results.keys(), key=lambda k: len(class_results[k]))
            focused_results = class_results[primary_class]
        else:
            primary_class = "Record"
            focused_results = validation_results

        simplified_summary = parse_shacl_results_for_ai(focused_results)
        relevant_rdf = extract_relevant_rdf_section(rdf_content, primary_class)

        prompt = f"""
You are a helpful metadata librarian. Write in plain language (no RDF/SHACL jargon). Analyze the validation errors for the {primary_class} and provide concise, actionable fixes.

{severity_instruction}
{doc_section}

Validation Errors for {primary_class}:
{focused_results[:1500]}

Validation Summary (plain language):
{simplified_summary}

Relevant RDF Section:
{relevant_rdf[:800]}

Instructions:
1. ONE sentence: What's wrong with this {primary_class}?
2. List errors (max 3 words each)
3. Show exact XML fixes

Format:
**Issue:** [One sentence about the {primary_class} problem]

**Errors:**
β€’ Error 1
β€’ Error 2

**Fix:**
```xml
[Complete corrected {primary_class} section]
```

Be ultra-concise. Show the fix, not explanations."""

        chat_completion = client.chat.completions.create(
            model=HF_MODEL,
            messages=[
                {
                    "role": "system",
                    "content": "You are a friendly librarian helping fix catalog records. Never use technical RDF or SHACL terminology. Use the BibFrame documentation provided to ensure accuracy."
                },
                {
                    "role": "user",
                    "content": prompt
                }
            ],
            max_tokens=800,
            temperature=0.5,
            top_p=0.9
        )

        generated_text = chat_completion.choices[0].message.content
        generated_text = clean_technical_jargon(generated_text)

        other_classes = [k for k in class_results.keys() if k != primary_class]
        class_note = (
            f"\n\nπŸ“Œ **Note:** Focused on {primary_class} errors. " +
            (f"Also found issues in: {', '.join(other_classes)}" if other_classes else "")
        )

        return f"πŸ€– **AI-Powered Suggestions ({('Violations + Warnings' if include_warnings else 'Violations Only')}):**\n\n{generated_text}{class_note}"

    except Exception as e:
        logger.error(f"OpenAI/HF Inference Endpoint error: {str(e)}")
        return f"""
❌ **AI suggestions error**: {str(e)}

{generate_manual_suggestions(validation_results)}
"""

def extract_relevant_rdf_section(rdf_content: str, class_name: str) -> str:
    """
    Extract only the relevant RDF section for a specific class
    
    Args:
        rdf_content (str): Full RDF content
        class_name (str): Class name to extract (Work, Instance, etc.)
        
    Returns:
        str: Relevant RDF section
    """
    import re
    
    # Map class names to RDF patterns
    patterns = {
        'Work': r'<bf:Work.*?</bf:Work>',
        'Instance': r'<bf:Instance.*?</bf:Instance>',
        'Title': r'<bf:Title.*?</bf:Title>',
        'Contribution': r'<bf:Contribution.*?</bf:Contribution>',
        'AdminMetadata': r'<bf:AdminMetadata.*?</bf:AdminMetadata>'
    }
    
    pattern = patterns.get(class_name)
    if not pattern:
        return rdf_content[:1000]  # Fallback to first 1000 chars
    
    # Extract matching section
    match = re.search(pattern, rdf_content, re.DOTALL)
    if match:
        section = match.group(0)
        # Also include namespace declarations
        namespaces = re.findall(r'xmlns:\w+="[^"]*"', rdf_content[:500])
        if namespaces:
            return f"<!-- Namespaces: {' '.join(namespaces[:3])} -->\n{section}"
        return section
    
    return rdf_content[:1000]  # Fallback

## [Removed duplicate get_ai_correction definition – unified below]

def merge_corrected_sections(original_rdf: str, corrected_sections: dict) -> str:
    """
    Merge corrected class sections back into the original RDF
    
    Args:
        original_rdf (str): Original RDF content
        corrected_sections (dict): Corrected sections by class
        
    Returns:
        str: Merged RDF with corrections
    """
    import re
    
    result = original_rdf
    
    # Replace each corrected section
    for class_name, corrected_section in corrected_sections.items():
        patterns = {
            'Work': r'<bf:Work.*?</bf:Work>',
            'Instance': r'<bf:Instance.*?</bf:Instance>',
            'Title': r'<bf:Title.*?</bf:Title>',
            'Contribution': r'<bf:Contribution.*?</bf:Contribution>',
            'AdminMetadata': r'<bf:AdminMetadata.*?</bf:AdminMetadata>'
        }
        
        pattern = patterns.get(class_name)
        if pattern:
            result = re.sub(pattern, corrected_section, result, count=1, flags=re.DOTALL)
    
    return result

# Sample RDF data for examples
# MCP Server Tools (can be used independently)
# Note: This section exists earlier in the file, we're removing the duplicates
    """
    Generate AI-powered fix suggestions for invalid RDF/XML.
    
    This tool analyzes validation results and provides actionable suggestions
    for fixing RDF/XML validation errors using AI or rule-based analysis.
    
    Args:
        validation_results (str): The validation error messages
        rdf_content (str): The original RDF/XML content that failed validation
        include_warnings (bool): Whether to include warnings in suggestions
    
    Returns:
        str: Detailed suggestions for fixing the RDF validation issues
    """
    
    if not OPENAI_AVAILABLE:
        return generate_manual_suggestions(validation_results)
    
    # Get API key dynamically at runtime
    current_api_key = os.getenv('HF_API_KEY', '')
    if not current_api_key:
        return f"""
πŸ”‘ **AI suggestions disabled**: Please set your Hugging Face API key as a Secret in your Space settings.

{generate_manual_suggestions(validation_results)}
"""
    
    try:
        # Use OpenAI client with your Hugging Face Inference Endpoint
        client = get_openai_client()
        if not client:
            return f"""
πŸ”‘ **AI suggestions disabled**: HF_API_KEY not configured.

{generate_manual_suggestions(validation_results)}
"""
        
        severity_instruction = "Focus only on violations (errors) and ignore any warnings." if not include_warnings else "Address both violations and warnings."
        
        prompt = f"""You are an expert in RDF/XML and SHACL validation. Analyze the validation errors and provide CONCISE, ACTIONABLE fix suggestions.

{severity_instruction}

Validation Results:
{validation_results}

Original RDF (first 1000 chars):
{rdf_content[:1000]}...

Instructions:
1. Start with a ONE-SENTENCE summary of the main issue
2. List the specific errors in bullet points (max 5 words per error)
3. Provide the exact fix for each error with code snippets
4. Keep explanations minimal - focus on solutions

Format:
**Main Issue:** [One sentence]

**Errors Found:**
β€’ Error 1 name
β€’ Error 2 name

**Fixes:**
1. **Error 1**: 
   ```xml
   [exact code to add/fix]
   ```
2. **Error 2**:
   ```xml
   [exact code to add/fix]
   ```

Be direct and solution-focused. No lengthy explanations."""
        
        # Make API call using OpenAI client
        print(f"πŸ”„ Making API call to: {HF_ENDPOINT_URL}")
        print(f"πŸ”„ Using model: {HF_MODEL}")
        print(f"πŸ”„ Include warnings: {include_warnings}")
        
        chat_completion = client.chat.completions.create(
            model=HF_MODEL,
            messages=[
                {
                    "role": "system",
                    "content": "You are a friendly librarian helping fix catalog records. Never use technical RDF or SHACL terminology."
                },
                {
                    "role": "user",
                    "content": prompt
                }
            ],
            max_tokens=1500,
            temperature=0.6,
            top_p=0.9
        )
        
        print("βœ… API call successful")
        generated_text = chat_completion.choices[0].message.content
        return f"πŸ€– **AI-Powered Suggestions ({('Violations + Warnings' if include_warnings else 'Violations Only')}):**\n\n{generated_text}"
        
    except Exception as e:
        logger.error(f"OpenAI/HF Inference Endpoint error: {str(e)}")
        return f"""
❌ **AI suggestions error**: {str(e)}

{generate_manual_suggestions(validation_results)}
"""

def extract_rdf_from_response(response: str) -> str:
    """
    Extract RDF/XML content from AI response, handling code blocks.
    
    Args:
        response (str): AI response that may contain RDF wrapped in code blocks
    
    Returns:
        str: Extracted RDF/XML content
    """
    response = response.strip()
    
    # Handle ```xml code blocks
    if "```xml" in response:
        try:
            return response.split("```xml")[1].split("```")[0].strip()
        except IndexError:
            pass
    
    # Handle generic ``` code blocks
    if "```" in response and response.count("```") >= 2:
        try:
            return response.split("```")[1].split("```")[0].strip()
        except IndexError:
            pass
    
    # If no code blocks found, return the response as-is
    return response

def fix_common_rdf_errors(rdf_xml: str) -> str:
    """
    Fix common RDF/XML errors that AI models generate.
    
    Args:
        rdf_xml (str): RDF/XML that may contain common errors
        
    Returns:
        str: Fixed RDF/XML
    """
    import re
    
    # Remove any rdf:parseType attributes (common AI mistake)
    rdf_xml = re.sub(r'\s+rdf:parseType="[^"]*"', '', rdf_xml)
    
    # Fix bf:title if it's just a string (should be nested structure)
    rdf_xml = re.sub(
        r'<bf:title>([^<]+)</bf:title>',
        r'<bf:title><bf:Title><bf:mainTitle>\1</bf:mainTitle></bf:Title></bf:title>',
        rdf_xml
    )
    
    # Fix bf:language if it's a string instead of URI
    language_map = {
        'English': 'http://id.loc.gov/vocabulary/languages/eng',
        'eng': 'http://id.loc.gov/vocabulary/languages/eng',
        'Spanish': 'http://id.loc.gov/vocabulary/languages/spa',
        'French': 'http://id.loc.gov/vocabulary/languages/fre',
    }
    for lang_text, lang_uri in language_map.items():
        rdf_xml = re.sub(
            f'<bf:language>{lang_text}</bf:language>',
            f'<bf:language rdf:resource="{lang_uri}"/>',
            rdf_xml,
            flags=re.IGNORECASE
        )
    
    # Fix bf:content if it's a string
    content_map = {
        'Text': 'http://id.loc.gov/vocabulary/contentTypes/txt',
        'text': 'http://id.loc.gov/vocabulary/contentTypes/txt',
    }
    for content_text, content_uri in content_map.items():
        rdf_xml = re.sub(
            f'<bf:content>{content_text}</bf:content>',
            f'<bf:content rdf:resource="{content_uri}"/>',
            rdf_xml,
            flags=re.IGNORECASE
        )
    
    return rdf_xml


def extract_error_focus_points(validation_results: str) -> Dict[str, List[str]]:
    """Identify the specific focus nodes and properties mentioned in validation errors."""
    import re

    focus = {
        "properties": [],
        "focus_nodes": [],
        "missing_properties": [],
        "classes": [],
    }

    if not validation_results:
        return focus

    property_set = set()
    missing_set = set()
    node_set = set()

    for match in re.finditer(r"Focus Node:\s*(?:<)?([^\s>]+)(?:>)?", validation_results):
        node_set.add(match.group(1))

    for match in re.finditer(r"Result Path:\s*(?:http://[^/]+/)?([A-Za-z]+)", validation_results):
        property_set.add(match.group(1))

    for match in re.finditer(r"Less than \d+ values on .*->bf:([A-Za-z]+)", validation_results):
        missing_set.add(match.group(1))

    focus["properties"] = sorted(property_set)
    focus["focus_nodes"] = sorted(node_set)
    focus["missing_properties"] = sorted(missing_set)
    return focus


def _resolve_bibframe_uri(name: str) -> str:
    if not name:
        return name
    if name.startswith("http://") or name.startswith("https://"):
        return name
    if ":" in name:
        prefix, local = name.split(":", 1)
        if prefix == "bf":
            return f"http://id.loc.gov/ontologies/bibframe/{local}"
    return f"http://id.loc.gov/ontologies/bibframe/{name}"


def get_targeted_bibframe_guidance(properties: List[str], classes: List[str]) -> Dict[str, dict]:
    """Fetch BibFrame documentation for only the specified properties/classes."""
    guidance: Dict[str, dict] = {}

    if not MCP4BIBFRAME_DOCS_ENABLED:
        return guidance

    for prop in properties[:5]:
        prop_uri = _resolve_bibframe_uri(prop)
        result = query_bibframe_docs_cached("get_property_info", {"property_uri": prop_uri}, timeout=5)
        if result:
            guidance[prop] = result

    for cls in classes[:5]:
        cls_uri = _resolve_bibframe_uri(cls)
        result = query_bibframe_docs_cached("get_class_info", {"class_uri": cls_uri}, timeout=5)
        if result:
            guidance[cls] = result

    return guidance


def generate_property_specific_fix(property_name: str, guidance: Optional[dict] = None) -> str:
    """Generate a BibFrame-compliant snippet for a specific missing property."""
    guidance = guidance or {}
    prop = property_name.lower() if property_name else ""

    if prop == "title":
        return """<bf:title>
    <bf:Title>
        <bf:mainTitle>PLACEHOLDER_TITLE</bf:mainTitle>
    </bf:Title>
</bf:title>"""

    if prop == "language":
        return """<bf:language>
    <bf:Language rdf:about="http://id.loc.gov/vocabulary/languages/eng">
        <rdfs:label xml:lang="en">English</rdfs:label>
        <bf:code rdf:datatype="http://www.w3.org/2001/XMLSchema#string">eng</bf:code>
    </bf:Language>
</bf:language>"""

    if prop == "content":
        return """<bf:content>
    <bf:Content rdf:about="http://id.loc.gov/vocabulary/contentTypes/txt">
        <rdfs:label>text</rdfs:label>
        <bf:code>txt</bf:code>
    </bf:Content>
</bf:content>"""

    if prop == "contribution":
        return """<bf:contribution>
    <bf:PrimaryContribution>
        <bf:agent>
            <bf:Agent>
                <rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Person"/>
                <rdfs:label>Author Name</rdfs:label>
            </bf:Agent>
        </bf:agent>
        <bf:role>
            <bf:Role rdf:about="http://id.loc.gov/vocabulary/relators/aut">
                <rdfs:label>author</rdfs:label>
                <bf:code>aut</bf:code>
            </bf:Role>
        </bf:role>
    </bf:PrimaryContribution>
</bf:contribution>"""

    if prop == "classification":
        return """<bf:classification>
    <bf:ClassificationLcc>
        <bf:classificationPortion>TT820</bf:classificationPortion>
        <bf:itemPortion>.B877 2002</bf:itemPortion>
        <bf:assigner>
            <bf:Organization rdf:about="http://id.loc.gov/vocabulary/organizations/dlc">
                <rdfs:label>United States, Library of Congress</rdfs:label>
            </bf:Organization>
        </bf:assigner>
    </bf:ClassificationLcc>
</bf:classification>"""

    if prop == "adminmetadata":
        return """<bf:adminMetadata>
    <bf:AdminMetadata>
        <bf:status>
            <bf:Status rdf:about="http://id.loc.gov/vocabulary/mstatus/n">
                <rdfs:label>new</rdfs:label>
                <bf:code>n</bf:code>
            </bf:Status>
        </bf:status>
        <bf:date rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2024-01-01</bf:date>
        <bf:agent>
            <bf:Agent rdf:about="http://id.loc.gov/vocabulary/organizations/dlc">
                <rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Organization"/>
                <rdfs:label>United States, Library of Congress</rdfs:label>
            </bf:Agent>
        </bf:agent>
    </bf:AdminMetadata>
</bf:adminMetadata>"""

    # Fallback: simple literal placeholder
    return f"<bf:{property_name}>PLACEHOLDER_VALUE</bf:{property_name}>"

def get_ai_correction(validation_results: str, rdf_content: str, template: str = 'monograph', max_attempts: int = None, include_warnings: bool = False, enable_validation_loop: bool | None = None, cache_key: Optional[str] = None, steps_log: Optional[List[str]] = None) -> str:
    """
    Generate AI-powered corrected RDF/XML based on validation errors.
    
    This tool takes invalid RDF/XML and validation results, then generates
    a corrected version that addresses all identified validation issues.
    The generated correction is validated before being returned to the user.
    
    Args:
        validation_results (str): The validation error messages
        rdf_content (str): The original invalid RDF/XML content
        template (str): The validation template to use
        max_attempts (int): Maximum number of attempts to generate valid RDF (uses MAX_CORRECTION_ATTEMPTS if None)
        include_warnings (bool): Whether to fix warnings in addition to violations
    
    Returns:
        str: Corrected RDF/XML that should pass validation
    """
    
    # Determine whether to iterate based on parameter or global default
    iterate_enabled = ENABLE_VALIDATION_LOOP if enable_validation_loop is None else enable_validation_loop
    if steps_log is not None:
        steps_log.append(f"Planning correction: iterate_enabled={iterate_enabled}, include_warnings={include_warnings}")
    # Use configuration default if not specified
    if max_attempts is None:
        max_attempts = MAX_CORRECTION_ATTEMPTS
    if steps_log is not None:
        steps_log.append(f"Max attempts set to {max_attempts}")
    # If iteration disabled, force single attempt
    if not iterate_enabled:
        max_attempts = 1
        if steps_log is not None:
            steps_log.append("Iteration disabled; forcing single attempt")

    if cache_key is None and validation_results and rdf_content:
        cache_key = _make_fix_cache_key(validation_results, rdf_content, template)
    if cache_key:
        cached_result = _get_cached_correction(cache_key, steps_log)
        if cached_result is not None:
            return cached_result
    
    if not OPENAI_AVAILABLE:
        if steps_log is not None:
            steps_log.append("OPENAI client not available; falling back to manual hints")
        return generate_manual_correction_hints(validation_results, rdf_content)
    
    # Get API key dynamically at runtime
    current_api_key = os.getenv('HF_API_KEY', '')
    if not current_api_key:
        if steps_log is not None:
            steps_log.append("HF_API_KEY not set; cannot call model; returning manual hints")
        return f"""<!-- AI correction disabled: Set HF_API_KEY as a Secret in your Space settings -->

{generate_manual_correction_hints(validation_results, rdf_content)}"""
    
    try:
        client = get_openai_client()
        if not client:
            if steps_log is not None:
                steps_log.append("Failed to initialize OpenAI client; returning manual hints")
            return f"""<!-- AI correction disabled: HF_API_KEY not configured -->

{generate_manual_correction_hints(validation_results, rdf_content)}"""
        
        # Fetch BibFrame documentation guidance
        if steps_log is not None:
            steps_log.append("Fetching BibFrame documentation guidance...")
        
        bibframe_guidance = fetch_bibframe_guidance(validation_results, rdf_content)
        
        if bibframe_guidance:
            if steps_log is not None:
                steps_log.append(f"Retrieved BibFrame guidance ({len(bibframe_guidance)} chars)")
            guidance_section = f"""
BIBFRAME DOCUMENTATION (from official ontology):
{bibframe_guidance}

Apply the above BibFrame definitions and patterns when correcting the RDF/XML.
"""
        else:
            guidance_section = ""
            if steps_log is not None:
                steps_log.append("No specific BibFrame guidance retrieved")
        
        # Add timeout protection
        import time
        start_time = time.time()
        timeout = 45  # Reduced to 45 second total timeout for speed
        if steps_log is not None:
            steps_log.append(f"Timeout budget: {timeout}s total")

        severity_instruction = "Fix only the violations (errors) and ignore any warnings." if not include_warnings else "Fix both violations and warnings."
        
        # Try multiple attempts to generate valid RDF
        for attempt in range(max_attempts):
            # Check timeout
            elapsed = time.time() - start_time
            if elapsed > timeout:
                if steps_log is not None:
                    steps_log.append(f"Timeout reached after {int(elapsed)}s; stopping attempts")
                print(f"⏰ Timeout reached after {timeout} seconds")
                break
                
            attempt_no = attempt + 1
            if steps_log is not None:
                steps_log.append(f"Attempt {attempt_no}/{max_attempts}: requesting model correction")
            print(f"πŸ”„ Correction attempt {attempt_no}/{max_attempts}")
            
            # Targeted AdminMetadata guidance inferred from results text
            needs_assigner = ("->bf:assigner" in validation_results) or (" bf:assigner" in validation_results)
            admin_guidance = ""
            if needs_assigner:
                admin_guidance = """
IMPORTANT: For each <bf:AdminMetadata>, ensure it has a direct child <bf:assigner>.
Rules:
- If <bf:agent rdf:resource=\"...\"/> exists, add <bf:assigner rdf:resource=\"...\"/> with the SAME URI.
- Else if <bf:descriptionModifier rdf:resource=\"...\"/> exists, add <bf:assigner rdf:resource=\"...\"/> with the SAME URI.
- Else if a <bf:identifiedBy> block contains <bf:assigner rdf:resource=\"...\"/>, copy that URI to a TOP-LEVEL <bf:assigner>.
Keep all existing content; only add missing <bf:assigner> where required.
"""

            # Build few-shot examples based on the errors found
            examples_to_include = []
            validation_lower = validation_results.lower()
            
            # Check each example pattern against validation results
            for name, example in BIBFRAME_CORRECTION_EXAMPLES.items():
                pattern = example.get("pattern", name)
                if re.search(pattern, validation_results, re.IGNORECASE):
                    examples_to_include.append((name, example))
                    if steps_log is not None:
                        steps_log.append(f"Including {name} example based on pattern match")
            
            few_shot_section = ""
            if examples_to_include:
                few_shot_section = "\n\nCORRECT BIBFRAME PATTERNS (from Library of Congress records):\n"
                few_shot_section += "NEVER use simple strings - always use nested structures as shown below:\n\n"
                for name, example in examples_to_include:
                    few_shot_section += f"{name.upper()}:\n"
                    few_shot_section += f"❌ WRONG:\n```xml\n{example['wrong']}\n```\n"
                    few_shot_section += f"βœ… CORRECT:\n```xml\n{example['correct']}\n```\n\n"

            # Add critical rules based on real patterns
            critical_rules = """
CRITICAL RDF/XML RULES (from real BibFrame):
1. NEVER use rdf:parseType except for "Collection" on madsrdf:componentList
2. Properties like bf:title, bf:language, bf:content MUST have nested typed resources
3. Use rdf:about for resource URIs, not rdf:resource on the property element
4. bf:adminMetadata can appear multiple times in one record
5. Status, Role, Language etc. are OBJECTS with rdf:about URIs, not literals
6. Date values use rdf:datatype for typing (e.g., xsd:date, xsd:dateTime)
7. Every bf:AdminMetadata needs BOTH bf:agent AND bf:assigner if validation requires it
"""

            prompt = f"""You are an expert in RDF/XML and BibFrame cataloging. Fix the following RDF/XML based on the validation errors and official BibFrame documentation.

{severity_instruction}
{admin_guidance}
{guidance_section}
{critical_rules}
{few_shot_section}

Validation Errors:
{validation_results}

Original RDF/XML:
{rdf_content}

{f"Previous attempt {attempt} still had validation errors. Please fix ALL issues this time." if attempt > 0 else ""}

INSTRUCTIONS:
1. Return ONLY valid RDF/XML - no explanations
2. Follow the EXACT patterns shown in the examples above
3. Use proper nested structures - NO simple string values for complex properties
4. Keep ALL namespace declarations
5. Fix ALL validation errors"""
            
            try:
                # Update system prompt to be even more explicit
                system_prompt = """You are an RDF/XML expert following Library of Congress BibFrame patterns.
Output ONLY valid RDF/XML following these rules:
- Start with <?xml version="1.0" encoding="UTF-8"?>
- NO markdown, NO explanations
- Use EXACT structure patterns from the examples
- Complex properties need nested typed resources
- rdf:parseType ONLY for Collection on madsrdf:componentList
- Status/Role/Language are OBJECTS with URIs, not strings"""

                chat_completion = client.chat.completions.create(
                    model=HF_MODEL,
                    messages=[
                        {
                            "role": "system",
                            "content": system_prompt
                        },
                        {
                            "role": "user", 
                            "content": prompt
                        }
                    ],
                    max_tokens=1500,
                    temperature=0.0,
                    timeout=20  # Reduced to 20 second timeout per API call for speed
                )
                
                corrected_rdf = chat_completion.choices[0].message.content.strip()
                if steps_log is not None:
                    steps_log.append(f"Attempt {attempt_no}: model responded; extracting and fixing common errors")
                
                # Extract RDF content if it's wrapped in code blocks
                corrected_rdf = extract_rdf_from_response(corrected_rdf)
                
                # Fix common AI mistakes
                corrected_rdf = fix_common_rdf_errors(corrected_rdf)
                
                # Only validate if we have the validator and haven't hit timeout
                if VALIDATOR_AVAILABLE and (time.time() - start_time < timeout - 10):
                    try:
                        # Quick validation check
                        conforms, new_results = validate_rdf(corrected_rdf.encode('utf-8'), template)
                        
                        if conforms:
                            if steps_log is not None:
                                steps_log.append(f"Attempt {attempt_no}: correction PASSED validation")
                            print(f"βœ… Correction validated successfully on attempt {attempt_no}")
                            result_text = f"""<!-- AI-generated correction validated successfully -->
{corrected_rdf}"""
                            if cache_key:
                                _store_correction_in_cache(cache_key, result_text, steps_log)
                            return result_text
                        else:
                            if steps_log is not None:
                                steps_log.append(f"Attempt {attempt_no}: still invalid; will retry with updated errors")
                            print(f"❌ Correction attempt {attempt_no} still has validation errors")
                            # Update validation_results for next attempt
                            validation_results = new_results
                            
                    except Exception as e:
                        if steps_log is not None:
                            steps_log.append(f"Attempt {attempt_no}: error during validation: {str(e)} β€” returning correction anyway")
                        print(f"⚠️ Error validating correction attempt {attempt_no}: {str(e)}")
                        # If validation fails, return the correction anyway
                        return f"""<!-- AI-generated correction (validation check failed) -->
{corrected_rdf}"""
                else:
                    # If validator not available or timeout approaching, return the correction
                    if steps_log is not None:
                        steps_log.append("Skipping validation check (validator unavailable or timeout)")
                    print("⚠️ Returning correction without validation")
                    return f"""<!-- AI-generated correction (validation skipped) -->
{corrected_rdf}"""
                    
            except Exception as api_error:
                if steps_log is not None:
                    steps_log.append(f"Attempt {attempt_no}: API error: {str(api_error)}")
                print(f"❌ API error on attempt {attempt_no}: {str(api_error)}")
                if attempt == max_attempts - 1:  # Last attempt
                    raise api_error
                continue
        
        # All attempts failed or timed out
        if steps_log is not None:
            steps_log.append("All attempts failed or timed out; returning manual hints")
        return f"""<!-- AI correction failed after {max_attempts} attempts or timeout -->
<!-- Please correct manually using the validation results as a guide -->

{generate_manual_correction_hints(validation_results, rdf_content)}"""
        
    except Exception as e:
        logger.error(f"LLM API error: {str(e)}")
        if steps_log is not None:
            steps_log.append(f"Fatal error invoking model: {str(e)}")
        return f"""<!-- Error generating AI correction: {str(e)} -->

{generate_manual_correction_hints(validation_results, rdf_content)}"""


def get_ai_correction_targeted(validation_results: str, rdf_content: str, template: str = 'monograph', max_attempts: int = None, include_warnings: bool = False, enable_validation_loop: bool | None = None, steps_log: Optional[List[str]] = None) -> str:
    """Fast path that attempts structured quick fixes before invoking the full AI loop."""

    if steps_log:
        steps_log.append("\n" + "=" * 70)
        steps_log.append("πŸ“Š INITIAL VALIDATION ERRORS:")
        steps_log.append("=" * 70)
        # Show summary of validation errors
        error_lines = [line.strip() for line in validation_results.split('\n') if 'Less than' in line or 'Message:' in line or 'Module:' in line]
        for line in error_lines[:15]:  # Show first 15 error lines
            steps_log.append(f"   {line}")
        if len(error_lines) > 15:
            steps_log.append(f"   ... and {len(error_lines) - 15} more errors")
        steps_log.append("")

    cache_key: Optional[str] = None
    if validation_results and rdf_content:
        cache_key = _make_fix_cache_key(validation_results, rdf_content, template)
        cached = _get_cached_correction(cache_key, steps_log)
        if cached is not None:
            if steps_log:
                steps_log.append("πŸ’Ύ Cache hit! Returning previously successful correction")
            return cached

    # Try rapid fix FIRST - this should handle most cases in < 5 seconds
    if steps_log:
        steps_log.append("=" * 60)
        steps_log.append("πŸš€ STARTING RAPID FIX")
        steps_log.append("=" * 60)
    
    quick_fix = rapid_fix_missing_properties(rdf_content, validation_results, template, steps_log)
    
    if quick_fix:
        if steps_log:
            steps_log.append("=" * 60)
            steps_log.append("πŸ” RE-VALIDATING AFTER RAPID FIX")
            steps_log.append("=" * 60)
    
    if quick_fix and VALIDATOR_AVAILABLE:
        try:
            conforms, new_results = validate_rdf(quick_fix.encode('utf-8'), template)
            if conforms:
                if steps_log:
                    steps_log.append("=" * 60)
                    steps_log.append("βœ…βœ…βœ… RAPID FIX SUCCESSFUL - VALIDATION PASSED!")
                    steps_log.append("=" * 60)
                if cache_key:
                    _store_correction_in_cache(cache_key, quick_fix, steps_log)
                return quick_fix
            else:
                # Update for next attempt
                if steps_log:
                    steps_log.append("=" * 60)
                    steps_log.append("⚠️ RAPID FIX INCOMPLETE - Still has errors:")
                    steps_log.append("=" * 60)
                    # Show first few errors
                    error_lines = new_results.split('\n')[:10] if new_results else []
                    for line in error_lines:
                        if 'Less than' in line or 'Message:' in line:
                            steps_log.append(f"   {line.strip()}")
                
                validation_results = new_results or validation_results
                rdf_content = quick_fix
                if steps_log:
                    steps_log.append("πŸ“‹ Continuing to minimal AI correction...")
        except Exception as e:
            if steps_log:
                steps_log.append("=" * 60)
                steps_log.append(f"❌ RAPID FIX VALIDATION ERROR: {e}")
                steps_log.append("=" * 60)
                steps_log.append("πŸ“‹ Continuing to minimal AI correction...")
    elif quick_fix and steps_log:
        steps_log.append("⚠️ Validator not available, cannot re-validate rapid fix")
    elif steps_log:
        steps_log.append("ℹ️ Rapid fix returned None, moving to AI correction")

    # If rapid fix didn't fully work, try minimal AI correction
    if OPENAI_AVAILABLE and os.getenv('HF_API_KEY'):
        if steps_log:
            steps_log.append("Attempting minimal AI correction...")
        
        corrected = get_ai_correction_minimal(validation_results, rdf_content, max_tokens=1000)
        
        if corrected and corrected != rdf_content and VALIDATOR_AVAILABLE:
            try:
                conforms, new_results = validate_rdf(corrected.encode('utf-8'), template)
                if conforms:
                    if steps_log:
                        steps_log.append("βœ… Minimal AI correction successful!")
                    if cache_key:
                        _store_correction_in_cache(cache_key, corrected, steps_log)
                    return corrected
                else:
                    validation_results = new_results or validation_results
                    rdf_content = corrected
                    if steps_log:
                        steps_log.append("Minimal AI correction partial; falling back to full AI...")
            except Exception as e:
                if steps_log:
                    steps_log.append(f"Minimal AI validation error: {e}; falling back...")

    focus_points = extract_error_focus_points(validation_results)
    missing_props = focus_points.get("missing_properties", [])

    if steps_log is not None:
        steps_log.append(f"Targeted fix: detected {len(missing_props)} missing properties")
        if missing_props:
            preview = ", ".join(missing_props[:5])
            if len(missing_props) > 5:
                preview += ", ..."
            steps_log.append(f"Missing list: {preview}")

    working_rdf = rdf_content
    quick_fix_attempted = False

    if missing_props and len(missing_props) <= 5:
        guidance = get_targeted_bibframe_guidance(missing_props, focus_points.get("classes", []))
        if steps_log is not None:
            steps_log.append(f"Retrieved guidance entries: {len(guidance)}")

        import re

        def _inject_snippets(match: re.Match) -> str:
            nonlocal quick_fix_attempted
            opening, inner, closing = match.groups()
            new_bits = []
            for prop in missing_props:
                if f"<bf:{prop}" not in inner:
                    snippet = generate_property_specific_fix(prop, guidance.get(prop))
                    new_bits.append(snippet)
            if not new_bits:
                return match.group(0)
            quick_fix_attempted = True
            if steps_log is not None:
                steps_log.append(f"Injected {len(new_bits)} snippets into {match.group(1).split()[0][1:]}")
            combined = opening + inner
            if not inner.endswith("\n"):
                combined += "\n"
            combined += "    " + "\n    ".join(new_bits) + "\n" + closing
            return combined

        work_pattern = re.compile(r"(<bf:Work[^>]*>)([\s\S]*?)(</bf:Work>)")
        instance_pattern = re.compile(r"(<bf:Instance[^>]*>)([\s\S]*?)(</bf:Instance>)")

        if work_pattern.search(working_rdf):
            working_rdf = work_pattern.sub(_inject_snippets, working_rdf, count=1)
        elif instance_pattern.search(working_rdf):
            working_rdf = instance_pattern.sub(_inject_snippets, working_rdf, count=1)

        if quick_fix_attempted and VALIDATOR_AVAILABLE:
            try:
                conforms, new_results = validate_rdf(working_rdf.encode('utf-8'), template)
                if conforms:
                    if steps_log is not None:
                        steps_log.append("Quick fix succeeded; validation now passes")
                    if cache_key:
                        _store_correction_in_cache(cache_key, working_rdf, steps_log)
                    return working_rdf
                else:
                    if steps_log is not None:
                        steps_log.append("Quick fix incomplete; falling back to AI loop")
                    validation_results = new_results or validation_results
            except Exception as quick_err:
                if steps_log is not None:
                    steps_log.append(f"Quick fix validation error: {quick_err}; using AI fallback")

    if validation_results and working_rdf:
        cache_key = _make_fix_cache_key(validation_results, working_rdf, template)

    return get_ai_correction(
        validation_results,
        working_rdf,
        template,
        max_attempts=max_attempts,
        include_warnings=include_warnings,
        enable_validation_loop=enable_validation_loop,
        cache_key=cache_key,
        steps_log=steps_log,
    )


def generate_manual_suggestions(validation_results: str) -> str:
    """Generate generic, pattern-based suggestions when AI is not available.

    Note: Avoid hardcoding SHACL rules or specific property requirements; rely only on
    patterns present in the validation output text.
    """
    vr_lower = validation_results.lower() if validation_results else ""
    suggestions: List[str] = []

    # Missing/required
    if ("mincount" in vr_lower) or ("missing" in vr_lower) or ("required" in vr_lower):
        suggestions.append("β€’ Some required fields are missing. Add the missing information where indicated.")

    # Too many values
    if ("maxcount" in vr_lower) or ("too many" in vr_lower) or ("more than allowed" in vr_lower):
        suggestions.append("β€’ Some fields have too many values. Keep only the main/one value as required.")

    # Datatype/format issues
    if ("datatype" in vr_lower) or ("type mismatch" in vr_lower) or ("expected" in vr_lower and "datatype" in vr_lower):
        suggestions.append("β€’ Some values are in the wrong format. Use the expected format (e.g., dates like YYYY-MM-DD).")

    # URI/identifier issues
    if ("iri" in vr_lower) or ("uri" in vr_lower) or ("identifier" in vr_lower and "invalid" in vr_lower):
        suggestions.append("β€’ Some identifiers look malformed. Use complete, valid web addresses or proper identifiers.")

    # Namespace/prefix issues
    if ("namespace" in vr_lower) or ("prefix" in vr_lower):
        suggestions.append("β€’ Define all XML namespace prefixes at the top and use them consistently.")

    # XML syntax/structure
    if ("xml" in vr_lower) or ("syntax" in vr_lower) or ("well-formed" in vr_lower):
        suggestions.append("β€’ Fix XML structure issues (unclosed tags, invalid characters, or nesting problems).")

    # Fallback
    if not suggestions:
        suggestions.append("β€’ Review the validation details and update the record where issues are highlighted.")
        suggestions.append("β€’ Follow the selected template; add missing fields and correct formats as needed.")

    suggestions_text = "\n".join(suggestions)

    return f"""
πŸ“‹ **What needs fixing:**

{suggestions_text}

πŸ’‘ **Quick tips:**
β€’ Include required fields when noted
β€’ Keep single-value fields to one value
β€’ Use the expected formats (e.g., for dates)
β€’ Declare and use XML namespace prefixes consistently
β€’ Ensure the XML is well‑formed

Need help? Load an example and compare the structure.
"""

def clean_technical_jargon(text: str) -> str:
    """Replace technical RDF/SHACL terms with plain language for end users."""
    if not text:
        return text
    replacements = {
        # RDF/SHACL jargon
        "URIRef": "identifier",
        "URI": "identifier",
        "IRI": "identifier",
        "Literal": "text value",
        "triple": "field entry",
        "graph": "dataset",
        "node": "record",
        "subject": "record",
        "predicate": "field type",
        "object": "value",
        "SHACL": "validation",
        "constraint": "rule",
        "conformance": "compliance",
        "violation": "issue",
        "sh:": "",
        "rdf:": "",
        "rdfs:": "",
        "xsd:": "",
        # Tone softening
        "Error:": "Issue:",
        "Invalid": "Incorrect",
        "Failed": "Did not pass",
        "Missing": "Not found",
    }
    cleaned = text
    for k, v in replacements.items():
        cleaned = cleaned.replace(k, v)
    return cleaned

def parse_shacl_results_for_ai(results_text: str) -> str:
    """Simplify SHACL results into clearer sentences for AI processing.

    Pattern-based only; does not depend on any SHACL rule definitions.
    """
    if not results_text:
        return ""
    import re
    simplified: List[str] = []

    # Generic patterns
    patterns = [
        (re.compile(r"minCount", re.IGNORECASE), "A required field is missing."),
        (re.compile(r"maxCount", re.IGNORECASE), "A field has more values than allowed; only one may be permitted."),
        (re.compile(r"datatype", re.IGNORECASE), "A field has a value in the wrong format."),
        (re.compile(r"iri|uri", re.IGNORECASE), "An identifier looks malformed or incomplete."),
        (re.compile(r"namespace|prefix", re.IGNORECASE), "A namespace prefix is undefined or inconsistent."),
        (re.compile(r"xml|syntax|well-formed", re.IGNORECASE), "The XML structure has an error (e.g., unclosed tag)."),
    ]

    lines = [ln.strip() for ln in results_text.splitlines() if ln.strip()]
    for ln in lines:
        matched = False
        for regex, message in patterns:
            if regex.search(ln):
                simplified.append(message)
                matched = True
                break
        if not matched and ("Constraint Violation" in ln or "Violation" in ln):
            simplified.append("A record rule was not met.")

    # Deduplicate while preserving order
    seen = set()
    unique = []
    for s in simplified:
        if s not in seen:
            unique.append(s)
            seen.add(s)

    return "\n".join(unique) if unique else results_text

def generate_manual_correction_hints(validation_results: str, rdf_content: str) -> str:
    """Generate manual correction hints when AI is not available"""
    return f"""<!-- Manual correction hints based on validation results -->
<!-- Set HF_API_KEY as a Secret in your Space settings for AI-powered corrections -->

{rdf_content}

<!-- 
VALIDATION ISSUES FOUND:
{validation_results[:500]}...

MANUAL CORRECTION STEPS:
1. Add missing namespace declarations
2. Include required properties (rdf:type, etc.)
3. Fix XML syntax errors
4. Ensure proper URI formats
5. Validate data types
-->"""

def extract_xml_from_text(text: str) -> str:
    """Extract RDF/XML from model output that may include extra formatting.

    Looks for the first <rdf:RDF ...> ... </rdf:RDF> block. If not found,
    returns the original text unchanged.
    """
    if not text:
        return text
    import re
    # Try to capture XML block even if fenced in code blocks
    # Use DOTALL to span multiple lines
    pattern = re.compile(r"<rdf:RDF[\s\S]*?</rdf:RDF>", re.IGNORECASE)
    m = pattern.search(text)
    if m:
        return m.group(0)
    # Strip common markdown fences if present
    fenced = re.sub(r"^```[a-zA-Z]*\n|```$", "", text.strip())
    return fenced if fenced else text

def clean_xml_for_validation(xml_text: str) -> str:
    """
    Clean XML text for validation by removing comments and extra formatting.
    
    Args:
        xml_text (str): XML text that may contain comments or formatting
        
    Returns:
        str: Clean XML ready for validation
    """
    import re
    
    if not xml_text:
        return xml_text
    
    # Remove all HTML comments
    cleaned = re.sub(r'<!--.*?-->', '', xml_text, flags=re.DOTALL)
    
    # Remove any leading/trailing whitespace
    cleaned = cleaned.strip()
    
    # If the text starts with "```" code fence, extract content
    if cleaned.startswith("```"):
        try:
            # Extract content between code fences
            parts = cleaned.split("```")
            if len(parts) >= 3:
                # Second part should be the XML content
                cleaned = parts[1]
                # Remove language identifier if present (e.g., "xml")
                if cleaned.startswith("xml"):
                    cleaned = cleaned[3:]
        except:
            pass
    
    return cleaned.strip()

# --- Namespace and wrapper helpers to avoid XML parser errors ---
STANDARD_NAMESPACES = {
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "bf": "http://id.loc.gov/ontologies/bibframe/",
    "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
    "xsd": "http://www.w3.org/2001/XMLSchema#",
}

def _extract_declared_namespaces(xml_text: str) -> dict:
    import re
    decls = {}
    for prefix, uri in re.findall(r"xmlns:([A-Za-z0-9_-]+)=\"([^\"]+)\"", xml_text[:2000]):
        decls[prefix] = uri
    return decls

def _detect_used_prefixes(xml_text: str) -> set:
    import re
    used = set()
    # Tag prefixes like <bf:Work ...> and attribute prefixes like rdf:type="..."
    for m in re.finditer(r"<\s*([A-Za-z0-9_-]+):[A-Za-z0-9_-]+", xml_text):
        used.add(m.group(1))
    for m in re.finditer(r"\s([A-Za-z0-9_-]+):[A-Za-z0-9_-]+=", xml_text):
        used.add(m.group(1))
    return used

def ensure_rdf_wrapper_and_namespaces(xml_text: str, original_text: Optional[str] = None, steps_log: Optional[List[str]] = None) -> str:
    """Ensure the XML has an <rdf:RDF> wrapper and required xmlns declarations for used prefixes.

    - If wrapper exists, add any missing xmlns: declarations for standard, used prefixes.
    - If wrapper is missing, wrap the content and include standard namespaces for used prefixes.
    """
    if not xml_text or not isinstance(xml_text, str):
        return xml_text
    import re

    declared = _extract_declared_namespaces(xml_text)
    if original_text:
        # Merge any declarations present in the original input
        declared.update(_extract_declared_namespaces(original_text))

    used = _detect_used_prefixes(xml_text)
    # Always consider rdf used for wrapper
    used.add("rdf")

    # Only inject namespaces for known standards to avoid guessing
    missing = [p for p in used if p not in declared and p in STANDARD_NAMESPACES]
    added_attrs = " ".join([f"xmlns:{p}=\"{STANDARD_NAMESPACES[p]}\"" for p in missing])

    has_wrapper = bool(re.search(r"<rdf:RDF[^>]*>", xml_text))
    updated = xml_text

    if has_wrapper:
        if added_attrs:
            # Inject before the closing '>' of the first <rdf:RDF ...>
            def _inject(match):
                start_tag = match.group(0)
                if start_tag.endswith('>'):
                    return start_tag[:-1] + ' ' + added_attrs + '>'
                return start_tag + ' ' + added_attrs
            updated = re.sub(r"<rdf:RDF[^>]*>", _inject, updated, count=1)
            if steps_log is not None and missing:
                steps_log.append(f"Injected missing namespace declarations: {', '.join(missing)}")
    else:
        # Build a wrapper with standard namespaces for used prefixes we know
        attrs = [f"xmlns:rdf=\"{STANDARD_NAMESPACES['rdf']}\""]
        for p in used:
            if p == 'rdf':
                continue
            uri = declared.get(p) or STANDARD_NAMESPACES.get(p)
            if uri:
                attrs.append(f"xmlns:{p}=\"{uri}\"")
        wrapper_open = "<rdf:RDF " + " ".join(attrs) + ">\n"
        wrapper_close = "\n</rdf:RDF>"
        updated = wrapper_open + xml_text + wrapper_close
        if steps_log is not None:
            steps_log.append("Wrapped snippet in <rdf:RDF> with standard namespace declarations")

    return updated

def validate_rdf_interface(rdf_content: str, template: str, use_ai: bool = True, include_warnings: bool = False, iterate_until_valid: bool = True, max_attempts: int = 5, show_steps: bool = True):
    """Main validation function for Gradio interface"""
    if not rdf_content.strip():
        return "❌ Error", "No RDF/XML data provided", "", "", "", "", ""

    steps_log: List[str] = []
    
    # Check if validator is available
    if not VALIDATOR_AVAILABLE:
        error_msg = "Validator module is not available. Please check that validator.py is present and all dependencies are installed."
        steps_log.append(f"ERROR: {error_msg}")
        return "❌ Error", error_msg, "", "\n".join(steps_log) if show_steps else "", "", "", ""
    
    # Prepare and validate RDF
    steps_log.append(f"Preparing RDF for validation (original length: {len(rdf_content)} chars)")
    prepped_input = ensure_rdf_wrapper_and_namespaces(rdf_content, steps_log=steps_log if show_steps else None)
    steps_log.append(f"Preprocessed RDF (new length: {len(prepped_input)} chars)")
    
    # Call validation
    steps_log.append(f"Calling validator with template '{template}'")
    result = validate_rdf_tool(prepped_input, template)
    
    if "error" in result:
        steps_log.append(f"Validation error: {result['error']}")
        return f"❌ Error: {result['error']}", "", "", "\n".join(steps_log) if show_steps else "", "", "", ""
    
    status = result["status"]
    results_text = result["results"]
    conforms = result["conforms"]
    
    steps_log.append(f"Initial validation: {'PASSED' if conforms else 'FAILED'} using template '{template}'")
    
    # Log if we got unexpected empty results
    if not results_text or len(results_text.strip()) == 0:
        steps_log.append("WARNING: Validator returned empty results text")
    
    # Filter results if warnings should be excluded
    filtered_results = results_text
    if not include_warnings and "Warning" in results_text:
        # Split results into lines and filter out warnings
        lines = results_text.split('\n')
        filtered_lines = []
        skip_until_next_section = False
        
        for line in lines:
            if "Warning" in line and ("Constraint Violation" in line or "sh:Warning" in line):
                skip_until_next_section = True
            elif "Constraint Violation" in line and "Warning" not in line:
                skip_until_next_section = False
                filtered_lines.append(line)
            elif not skip_until_next_section:
                filtered_lines.append(line)
        
        filtered_results = '\n'.join(filtered_lines)
        if not include_warnings:
            steps_log.append("Filtered out warnings from results")
    
    corrected_status = ""
    corrected_results = ""
    
    if not include_warnings:
        steps_log.append("Configured to ignore warnings in AI processing")
    if iterate_until_valid:
        steps_log.append(f"Iteration enabled with max_attempts={max_attempts}")
    if conforms:
        suggestions = "βœ… No issues found! Your RDF/XML is valid according to the selected template."
        corrected_rdf = ""
        corrected_status = "β€”"
        corrected_results = ""
        steps_log.append("No correction needed; record already conforms")
    else:
        if use_ai:
            # Pass filtered results to AI functions
            suggestions = get_ai_suggestions(filtered_results, rdf_content, include_warnings)
            steps_log.append("Requested AI suggestions for concise guidance")
            corrected_rdf = get_ai_correction_targeted(
                filtered_results,
                rdf_content,
                template,
                max_attempts=max_attempts,
                include_warnings=include_warnings,
                enable_validation_loop=iterate_until_valid,
                steps_log=steps_log,
            )
            # Attempt re-validation of corrected RDF
            try:
                # Clean the corrected output for validation
                corrected_xml = clean_xml_for_validation(corrected_rdf)
                corrected_xml = extract_xml_from_text(corrected_xml)
                corrected_xml = ensure_rdf_wrapper_and_namespaces(corrected_xml, original_text=prepped_input, steps_log=steps_log)
                
                # Debug logging
                steps_log.append(f"Re-validating cleaned RDF ({len(corrected_xml)} chars)")
                if show_steps:
                    # Log first 200 chars of what we're validating
                    preview = corrected_xml[:200] + "..." if len(corrected_xml) > 200 else corrected_xml
                    steps_log.append(f"Preview: {preview}")
                
                reval = validate_rdf_tool(corrected_xml, template)
                if "error" in reval:
                    corrected_status = f"❌ Re-validation Error: {reval['error']}"
                    corrected_results = ""
                    steps_log.append(f"Re-validation failed with error: {reval['error']}")
                else:
                    corrected_status = reval.get("status", "")
                    corrected_results = reval.get("results", "")
                    conforms = reval.get('conforms', False)
                    steps_log.append(f"Re-validation: {corrected_status} - Conforms: {conforms}")
            except Exception as re_ex:
                corrected_status = f"❌ Re-validation Error: {re_ex}"
                corrected_results = ""
                steps_log.append(f"Re-validation error: {re_ex}")
        else:
            suggestions = generate_manual_suggestions(filtered_results)
            corrected_rdf = generate_manual_correction_hints(filtered_results, rdf_content)
            corrected_status = "β€”"
            corrected_results = ""
            steps_log.append("AI disabled; produced manual suggestions and hints")
    
    steps_text = "\n".join(steps_log) if show_steps else ""
    return status, results_text, suggestions, steps_text, corrected_rdf, corrected_status, corrected_results

def get_rdf_examples(example_type: str = "valid") -> str:
    """
    Retrieve example RDF/XML snippets for testing and learning.
    
    This tool provides sample RDF/XML content that can be used to test
    the validation system or learn proper RDF structure. Examples include
    valid BibFrame Work records, invalid records for testing corrections,
    and BibFrame Instance records.
    
    Args:
        example_type (str): Type of example to retrieve. Options:
            - 'valid': A complete, valid BibFrame Work record
            - 'invalid': An incomplete BibFrame Work with validation errors
            - 'bibframe': A BibFrame Instance record example
    
    Returns:
        str: Complete RDF/XML example content ready for validation testing
    """
    examples = {
        "valid": SAMPLE_VALID_RDF,
        "invalid": SAMPLE_INVALID_RDF,
        "bibframe": '''<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:bf="http://id.loc.gov/ontologies/bibframe/"
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
    
    <bf:Instance rdf:about="http://example.org/instance/1">
        <rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Print"/>
        <bf:instanceOf rdf:resource="http://example.org/work/1"/>
        <bf:title>
            <bf:Title>
                <bf:mainTitle>Example Book Title</bf:mainTitle>
            </bf:Title>
        </bf:title>
        <bf:provisionActivity>
            <bf:Publication>
                <bf:date>2024</bf:date>
                <bf:place>
                    <bf:Place>
                        <rdfs:label>New York</rdfs:label>
                    </bf:Place>
                </bf:place>
            </bf:Publication>
        </bf:provisionActivity>
    </bf:Instance>
    
</rdf:RDF>'''
    }
    
    return examples.get(example_type, examples["valid"])

# Create Gradio Interface
def create_interface():
    """Create the main Gradio interface"""
    
    # Check API key status dynamically
    current_api_key = os.getenv('HF_API_KEY', '')
    api_status = "πŸ”‘ AI features enabled" if (OPENAI_AVAILABLE and current_api_key) else "⚠️ AI features disabled (set HF_API_KEY)"
    
    with gr.Blocks(
        title="RDF Validation Server with AI",
        theme=gr.themes.Soft(),
        css="""
        .status-box { 
            font-weight: bold; 
            padding: 10px; 
            border-radius: 5px; 
        }
        .header-text {
            text-align: center;
            padding: 20px;
        }
        """
    ) as demo:
        
        # Header
        debug_info = f"""
        Debug Info:
        - OPENAI_AVAILABLE: {OPENAI_AVAILABLE}
        - HF_INFERENCE_AVAILABLE: {HF_INFERENCE_AVAILABLE}
        - HF_API_KEY set: {'Yes' if current_api_key else 'No'}
        - HF_API_KEY length: {len(current_api_key) if current_api_key else 0}
        - HF_ENDPOINT_URL: {HF_ENDPOINT_URL}
        - HF_MODEL: {HF_MODEL}
        """
        
        gr.HTML(f"""
        <div class="header-text">
            <h1>πŸ” RDF Validation Server with AI</h1>
            <p>Validate RDF/XML against SHACL schemas with AI-powered suggestions and corrections</p>
            <p><strong>Status:</strong> {api_status}</p>
            <details><summary>Debug Info</summary><pre>{debug_info}</pre></details>
        </div>
        """)
        
        # Main interface
        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown("### πŸ“ Input")
                
                rdf_input = gr.Textbox(
                    label="RDF/XML Content",
                    placeholder="Paste your RDF/XML content here...",
                    lines=15,
                    show_copy_button=True
                )
                
                # Keep the main form simple and tuck options into an accordion
                with gr.Accordion("Advanced options", open=False):
                    with gr.Row():
                        template_dropdown = gr.Dropdown(
                            label="Validation Template",
                            choices=["monograph", "custom"],
                            value="monograph",
                            info="Select the SHACL template to validate against"
                        )
                        use_ai_checkbox = gr.Checkbox(
                            label="Use AI Features",
                            value=True,
                            info="Enable AI-powered suggestions and corrections"
                        )
                        include_warnings_checkbox = gr.Checkbox(
                            label="Include Warnings",
                            value=False,
                            info="Include warnings in AI corrections (violations only by default)"
                        )
                    with gr.Row():
                        iterate_checkbox = gr.Checkbox(
                            label="Iterate until valid",
                            value=True,
                            info="Try multiple correction attempts until validation passes or attempts run out"
                        )
                        max_attempts_slider = gr.Slider(
                            label="Max attempts",
                            minimum=1,
                            maximum=3,
                            value=2,
                            step=1,
                            info="Maximum number of correction attempts (2 recommended for speed)"
                        )
                        show_steps_checkbox = gr.Checkbox(
                            label="Show steps",
                            value=False,
                            info="Display step-by-step process (turn on when you want transparency)"
                        )
                
                validate_btn = gr.Button("πŸ” Validate RDF", variant="primary", size="lg")
                
                # Examples and controls
                gr.Markdown("### πŸ“š Examples & Tools")
                
                with gr.Row():
                    example1_btn = gr.Button("βœ… Valid RDF Example", variant="secondary")
                    example2_btn = gr.Button("❌ Invalid RDF Example", variant="secondary")
                    clear_btn = gr.Button("πŸ—‘οΈ Clear All", variant="stop")
        
        # Results section
        with gr.Row():
            with gr.Column():
                gr.Markdown("### πŸ“Š Results")
                
                status_output = gr.Textbox(
                    label="Validation Status",
                    interactive=False,
                    lines=1,
                    elem_classes=["status-box"]
                )
                
                results_output = gr.Textbox(
                    label="Detailed Validation Results",
                    interactive=False,
                    lines=8,
                    show_copy_button=True
                )
                
                suggestions_output = gr.Textbox(
                    label="πŸ’‘ Fix Suggestions",
                    interactive=False,
                    lines=8,
                    show_copy_button=True
                )

                steps_output = gr.Textbox(
                    label="🧭 Correction Steps",
                    interactive=False,
                    lines=10,
                    show_copy_button=True,
                    placeholder="Step-by-step log of how the system derived the corrected XML"
                )
        
        # Corrected RDF section
        with gr.Row():
            with gr.Column():
                gr.Markdown("### πŸ› οΈ AI-Generated Corrections")
                
                corrected_output = gr.Textbox(
                    label="Corrected RDF/XML",
                    interactive=False,
                    lines=15,
                    show_copy_button=True,
                    placeholder="Corrected RDF will appear here after validation..."
                )

                with gr.Row():
                    corrected_status_output = gr.Textbox(
                        label="Re-validation Status (Corrected RDF)",
                        interactive=False,
                        lines=1,
                        elem_classes=["status-box"]
                    )
                    corrected_results_output = gr.Textbox(
                        label="Re-validation Details",
                        interactive=False,
                        lines=6,
                        show_copy_button=True
                    )
        
        # Event handlers
        validate_btn.click(
            fn=validate_rdf_interface,
            inputs=[rdf_input, template_dropdown, use_ai_checkbox, include_warnings_checkbox, iterate_checkbox, max_attempts_slider, show_steps_checkbox],
            outputs=[status_output, results_output, suggestions_output, steps_output, corrected_output, corrected_status_output, corrected_results_output]
        )
        
        # Remove auto-validation to prevent processing loops
        # rdf_input.change(
        #     fn=validate_rdf_interface,
        #     inputs=[rdf_input, template_dropdown, use_ai_checkbox],
        #     outputs=[status_output, results_output, suggestions_output, corrected_output]
        # )
        
        # Example buttons
        example1_btn.click(
            lambda: get_rdf_examples("valid"),
            outputs=[rdf_input]
        )
        
        example2_btn.click(
            lambda: get_rdf_examples("invalid"),
            outputs=[rdf_input]
        )
        
        clear_btn.click(
            lambda: ("", "", "", "", "", "", "", ""),
            outputs=[rdf_input, status_output, results_output, suggestions_output, steps_output, corrected_output, corrected_status_output, corrected_results_output]
        )
        
        # Footer with instructions
        gr.Markdown("""
        ---
        ### οΏ½ **Documentation & Resources:**
        
        **[πŸ“– MCP4BibFrame Documentation](https://huggingface.co/spaces/jimfhahn/mcp4bibframe-docs)** - Complete BibFrame ontology reference with examples
        
        This validator integrates with the **MCP4BibFrame Documentation API** to provide authoritative BibFrame ontology information during AI-powered corrections.
        
        ### πŸš€ **Quick Start:**
        
        1. **Paste your RDF/XML** in the input box above
        2. **Click "Validate RDF"** to check for errors
        3. **Review AI suggestions** for plain-language fixes (enhanced with BibFrame documentation)
        4. **Copy the corrected RDF** from the output
        
        ---
        ### οΏ½πŸš€ **Deployment Instructions for Hugging Face Spaces:**
        
        1. **Create a new Space** on [Hugging Face](https://huggingface.co/spaces)
        2. **Set up your Hugging Face Inference Endpoint** and get the endpoint URL
        3. **Set your tokens** in Space settings (use Secrets for security):
           - Go to Settings β†’ Repository secrets  
           - Add: `HF_API_KEY` = `your_huggingface_api_key_here`
           - Endpoint is now hardcoded to your specific Inference Endpoint
        4. **Upload these files** to your Space repository
        5. **Install requirements**: The Space will auto-install from `requirements.txt`
        
        ### πŸ”§ **MCP Server Mode:**
        This app functions as both a web interface AND an MCP server for Claude Desktop and other MCP clients.
        
        **Available MCP Tools:**
        - `validate_rdf_tool`: Validate RDF/XML against SHACL shapes
        - `get_ai_suggestions`: Get AI-powered fix suggestions (with BibFrame docs)
        - `get_ai_correction`: Generate corrected RDF/XML (with BibFrame docs)
        - `get_rdf_examples`: Retrieve example RDF snippets
        - `validate_rdf_interface`: Complete validation with AI suggestions and corrections (primary tool)
        
        **MCP Configuration (Streamable HTTP):** 
        Add this configuration to your MCP client (Claude Desktop, etc.):
        
        ```json
        {
          "mcpServers": {
            "rdf-validator": {
              "url": "https://jimfhahn-mcp4rdf.hf.space/gradio_api/mcp/"
            }
          }
        }
        ```
        
        **Alternative SSE Configuration:**
        ```json
        {
          "mcpServers": {
            "rdf-validator": {
              "url": "https://jimfhahn-mcp4rdf.hf.space/gradio_api/mcp/sse"
            }
          }
        }
        ```
        
        ### πŸ’‘ **Features:**
        - βœ… Real-time RDF/XML validation against SHACL schemas
        - πŸ€– AI-powered error suggestions and corrections (enhanced with BibFrame ontology docs)
        - πŸ“š Built-in examples and templates
        - πŸ”— Integrated with [MCP4BibFrame Documentation API](https://huggingface.co/spaces/jimfhahn/mcp4bibframe-docs)
        - πŸ“‹ Copy results with one click
        
        **BibFrame Documentation Integration:** 
        AI corrections now use authoritative BibFrame ontology information from the MCP4BibFrame Documentation API to ensure accuracy and compliance with official specifications.
        
        ### πŸ”— **Related Resources:**
        - [MCP4BibFrame Documentation](https://huggingface.co/spaces/jimfhahn/mcp4bibframe-docs) - BibFrame ontology reference
        - [BIG DCTAP Documentation](https://bf-interop.github.io/DCTap/)
        - [BIBFRAME Ontology](http://id.loc.gov/ontologies/bibframe.html)
        - [SHACL Specification](https://www.w3.org/TR/shacl/)
        
        **Note:** AI features require a valid Hugging Face API key (HF_API_KEY) set as a Secret. Manual suggestions are provided as fallback.
        """)
    
    return demo

# Launch configuration
if __name__ == "__main__":
    demo = create_interface()
    
    # Configuration for different environments
    port = int(os.getenv('PORT', 7860))  # Hugging Face uses PORT env variable
    
    demo.launch(
        server_name="0.0.0.0",      # Important for external hosting
        server_port=port,           # Use environment PORT or default to 7860
        share=False,                # Don't create gradio.live links in production
        show_error=True,            # Show errors in the interface
        show_api=True,              # Enable API endpoints
        allowed_paths=["."],        # Allow serving files from current directory
        mcp_server=True             # Enable MCP server functionality (Gradio 5.28+)
    )