File size: 115,666 Bytes
4a8abfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eabfed0
 
 
 
 
 
284efb4
 
 
eabfed0
 
 
 
 
 
 
4a8abfd
 
 
 
 
 
 
 
 
284efb4
4a8abfd
 
 
 
 
 
284efb4
 
4a8abfd
 
284efb4
 
4a8abfd
 
284efb4
 
4a8abfd
 
 
284efb4
 
4a8abfd
 
284efb4
 
4a8abfd
 
284efb4
 
4a8abfd
 
 
 
284efb4
 
 
4a8abfd
 
 
 
284efb4
 
4a8abfd
284efb4
 
4a8abfd
 
284efb4
 
4a8abfd
 
 
 
284efb4
 
 
 
 
 
 
 
 
 
 
 
4a8abfd
 
284efb4
 
4a8abfd
 
284efb4
 
4a8abfd
 
284efb4
 
4a8abfd
 
284efb4
4a8abfd
284efb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a8abfd
 
 
 
 
 
 
 
 
eabfed0
 
 
 
 
 
 
 
 
 
 
284efb4
eabfed0
 
 
 
 
 
 
 
 
 
 
 
 
 
284efb4
 
eabfed0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284efb4
eabfed0
 
 
 
284efb4
 
eabfed0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284efb4
eabfed0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
788251f
 
4a8abfd
 
 
788251f
 
 
 
 
 
 
 
 
 
 
4a8abfd
788251f
4a8abfd
 
788251f
4a8abfd
788251f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a8abfd
788251f
4a8abfd
 
 
 
788251f
 
 
 
 
 
 
 
 
 
 
4a8abfd
788251f
4a8abfd
 
 
 
788251f
 
 
 
 
 
4a8abfd
788251f
4a8abfd
 
 
 
788251f
 
 
 
 
 
 
 
 
 
 
4a8abfd
788251f
4a8abfd
 
 
 
788251f
 
 
 
 
 
4a8abfd
788251f
4a8abfd
 
 
 
788251f
 
 
 
 
 
 
 
 
 
 
4a8abfd
788251f
4a8abfd
 
 
 
788251f
 
 
 
 
 
4a8abfd
788251f
4a8abfd
 
 
 
788251f
 
 
 
 
 
 
 
 
4a8abfd
788251f
4a8abfd
 
 
 
788251f
 
 
 
 
 
 
 
 
 
 
 
 
 
4a8abfd
788251f
4a8abfd
 
 
 
7186a85
4a8abfd
 
 
7186a85
 
 
 
 
 
 
 
4a8abfd
 
 
 
 
 
 
7186a85
4a8abfd
 
 
 
 
 
7186a85
 
 
4a8abfd
 
 
 
7186a85
 
 
 
 
 
 
4a8abfd
 
 
 
 
7186a85
 
 
 
 
 
 
4a8abfd
7186a85
4a8abfd
 
7186a85
 
 
 
 
 
 
 
 
 
4a8abfd
 
 
7186a85
 
 
 
 
 
 
 
 
 
 
4a8abfd
 
 
7186a85
4a8abfd
 
 
 
 
 
 
 
 
7186a85
4a8abfd
 
 
7186a85
 
 
 
 
 
4a8abfd
 
 
7186a85
 
 
 
 
 
 
4a8abfd
 
 
 
 
 
 
 
 
 
7186a85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a8abfd
 
 
7186a85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a8abfd
 
 
7186a85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
788251f
 
4a8abfd
 
 
 
788251f
4a8abfd
788251f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a8abfd
788251f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a8abfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7186a85
 
 
4a8abfd
 
 
 
 
 
 
7186a85
 
 
 
 
 
 
 
 
 
 
 
 
eabfed0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7186a85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a8abfd
 
7186a85
 
 
4a8abfd
7186a85
 
 
4a8abfd
7186a85
 
 
eabfed0
 
7186a85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a8abfd
 
 
eabfed0
 
 
 
 
7186a85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eabfed0
 
 
7186a85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eabfed0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a8abfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
788251f
4a8abfd
788251f
4a8abfd
788251f
 
4a8abfd
 
 
 
284efb4
4a8abfd
 
284efb4
 
 
 
 
4a8abfd
 
284efb4
 
 
4a8abfd
 
 
 
284efb4
 
 
 
 
 
4a8abfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eabfed0
 
 
 
 
 
 
 
 
 
4a8abfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284efb4
 
 
4a8abfd
 
 
 
 
 
 
 
 
 
 
 
 
284efb4
 
 
4a8abfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284efb4
 
 
4a8abfd
 
 
 
 
 
 
 
 
7186a85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eabfed0
284efb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eabfed0
4a8abfd
 
 
284efb4
 
 
eabfed0
4a8abfd
 
 
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
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
import streamlit as st
import requests
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import json
import time
from datetime import datetime, timedelta
import asyncio
import aiohttp
from typing import Dict, Any, List, Optional, Set
import sqlite3
import hashlib
from concurrent.futures import ThreadPoolExecutor, as_completed
import xml.etree.ElementTree as ET
import re
import os
import pickle
from urllib.parse import urljoin, urlparse
import threading
from pathlib import Path
import numpy as np
from sklearn.ensemble import IsolationForest
from sklearn.metrics.pairwise import cosine_similarity
import warnings
warnings.filterwarnings('ignore')

# Global ML availability flag
ML_AVAILABLE = False

# AI/ML Imports for enhanced functionality
try:
    from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
    from sentence_transformers import SentenceTransformer
    ML_AVAILABLE = True
except ImportError:
    ML_AVAILABLE = False

# Enhanced Page Configuration
st.set_page_config(
    page_title="Ultimate Data Harvester",
    page_icon="πŸš€",
    layout="wide",
    initial_sidebar_state="collapsed"
)

# Enhanced CSS with modern, professional styling
st.markdown("""
<style>
    .main > div {
        padding-top: 1rem;
    }
    .stApp {
        background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        color: #2c3e50;
    }
    .metric-card {
        background: rgba(255, 255, 255, 0.95);
        border-radius: 12px;
        padding: 1.5rem;
        margin: 0.5rem 0;
        border: 1px solid rgba(52, 73, 94, 0.1);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        transition: all 0.3s ease;
    }
    .metric-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
    }
    .api-card {
        background: rgba(255, 255, 255, 0.9);
        border-radius: 10px;
        padding: 1.2rem;
        margin: 0.5rem;
        border: 1px solid rgba(52, 73, 94, 0.15);
        transition: all 0.3s ease;
        position: relative;
        overflow: hidden;
    }
    .api-card:hover {
        transform: translateY(-3px);
        box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
        border-color: #3498db;
    }
    .title-container {
        text-align: center;
        padding: 2rem 0;
        background: rgba(255, 255, 255, 0.9);
        border-radius: 15px;
        margin-bottom: 2rem;
        border: 1px solid rgba(52, 73, 94, 0.1);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    }
    .status-indicator {
        width: 10px;
        height: 10px;
        border-radius: 50%;
        display: inline-block;
        margin-right: 8px;
    }
    .status-active { background-color: #27ae60; }
    .status-discovering { background-color: #f39c12; }
    .status-error { background-color: #e74c3c; }
    .status-paused { background-color: #95a5a6; }
    
    .ai-panel {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 10px;
        padding: 1rem;
        margin: 1rem 0;
        color: white;
        border: none;
    }
    .discovery-progress {
        background: rgba(255, 255, 255, 0.95);
        border-radius: 8px;
        padding: 1rem;
        margin: 1rem 0;
        border: 1px solid rgba(52, 73, 94, 0.1);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    }
    .endpoint-item {
        background: rgba(255, 255, 255, 0.8);
        border-radius: 6px;
        padding: 0.5rem;
        margin: 0.3rem 0;
        border-left: 3px solid #3498db;
        font-size: 0.9rem;
        color: #34495e;
    }
    
    /* Custom button styling */
    .stButton > button {
        background: linear-gradient(135deg, #3498db, #2980b9);
        color: white;
        border: none;
        border-radius: 8px;
        padding: 0.5rem 1rem;
        font-weight: 500;
        transition: all 0.3s ease;
    }
    .stButton > button:hover {
        background: linear-gradient(135deg, #2980b9, #1f4e79);
        transform: translateY(-1px);
        box-shadow: 0 4px 12px rgba(52, 152, 219, 0.3);
    }
    
    /* Tab styling */
    .stTabs [data-baseweb="tab-list"] {
        gap: 8px;
    }
    .stTabs [data-baseweb="tab"] {
        background-color: rgba(255, 255, 255, 0.7);
        border-radius: 8px;
        color: #2c3e50;
        font-weight: 500;
    }
    .stTabs [aria-selected="true"] {
        background-color: #3498db;
        color: white;
    }
    
    /* Metrics styling */
    [data-testid="metric-container"] {
        background: rgba(255, 255, 255, 0.9);
        border: 1px solid rgba(52, 73, 94, 0.1);
        padding: 1rem;
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    }
    
    /* Sidebar styling */
    .css-1d391kg {
        background: linear-gradient(135deg, #ecf0f1 0%, #bdc3c7 100%);
    }
</style>
""", unsafe_allow_html=True)

# Enhanced Database Configuration
DB_PATH = "ultimate_data_harvester.db"
SESSION_PATH = "harvester_session.pkl"
ENDPOINTS_CACHE = "discovered_endpoints.json"

# AI Enhancement Classes
class AIDataQualityAssessor:
    """AI-powered data quality assessment using transformers"""
    
    def __init__(self):
        self.quality_model = None
        self.embeddings_model = None
        self._initialize_models()
    
    def _initialize_models(self):
        """Initialize AI models for quality assessment"""
        global ML_AVAILABLE
        if ML_AVAILABLE:
            try:
                # Initialize quality classifier
                self.quality_model = pipeline(
                    "text-classification",
                    model="distilbert-base-uncased-finetuned-sst-2-english",
                    return_all_scores=True
                )
                
                # Initialize embeddings model for similarity
                self.embeddings_model = SentenceTransformer('all-MiniLM-L6-v2')
                
            except Exception as e:
                ML_AVAILABLE = False
                self.quality_model = None
                self.embeddings_model = None
    
    def assess_data_quality(self, data: Any, api_name: str) -> Dict:
        """Comprehensive AI-powered data quality assessment"""
        if not ML_AVAILABLE or not self.quality_model:
            return self._basic_quality_assessment(data, api_name)
        
        try:
            # Convert data to text for analysis
            text_data = self._data_to_text(data)
            
            # AI quality scoring
            ai_scores = self.quality_model(text_data[:512])  # Limit to 512 chars
            quality_score = max([score['score'] for score in ai_scores[0]])
            
            # Basic quality metrics
            completeness = self._check_completeness(data)
            consistency = self._check_consistency(data, api_name)
            structure_quality = self._assess_structure(data)
            
            # Anomaly detection
            anomalies = self._detect_anomalies(data)
            
            return {
                "ai_quality_score": round(quality_score, 3),
                "completeness_score": completeness,
                "consistency_score": consistency,
                "structure_score": structure_quality,
                "anomaly_count": len(anomalies),
                "anomalies": anomalies[:5],  # Top 5 anomalies
                "overall_grade": self._calculate_overall_grade(
                    quality_score, completeness, consistency, structure_quality
                ),
                "recommendations": self._generate_quality_recommendations(
                    quality_score, completeness, consistency, anomalies
                )
            }
        
        except Exception as e:
            return self._basic_quality_assessment(data, api_name)
    
    def _data_to_text(self, data: Any) -> str:
        """Convert any data format to text for AI analysis"""
        if isinstance(data, str):
            return data
        elif isinstance(data, dict):
            return json.dumps(data, ensure_ascii=False)[:1000]
        elif isinstance(data, list):
            return str(data)[:1000]
        else:
            return str(data)[:1000]
    
    def _check_completeness(self, data: Any) -> float:
        """Check data completeness"""
        if isinstance(data, dict):
            total_fields = len(data)
            complete_fields = sum(1 for v in data.values() if v is not None and v != "")
            return complete_fields / total_fields if total_fields > 0 else 0.0
        elif isinstance(data, list):
            if not data:
                return 0.0
            if isinstance(data[0], dict):
                return np.mean([self._check_completeness(item) for item in data])
            return 1.0
        return 1.0 if data is not None else 0.0
    
    def _check_consistency(self, data: Any, api_name: str) -> float:
        """Check data consistency based on API expectations"""
        consistency_score = 1.0
        
        if isinstance(data, list):
            if len(data) > 1:
                # Check if all items have similar structure
                first_item = data[0] if data else {}
                if isinstance(first_item, dict):
                    first_keys = set(first_item.keys())
                    consistency_scores = []
                    for item in data[1:6]:  # Check first 5 items
                        if isinstance(item, dict):
                            item_keys = set(item.keys())
                            similarity = len(first_keys & item_keys) / len(first_keys | item_keys)
                            consistency_scores.append(similarity)
                    
                    if consistency_scores:
                        consistency_score = np.mean(consistency_scores)
        
        return consistency_score
    
    def _assess_structure(self, data: Any) -> float:
        """Assess data structure quality"""
        if isinstance(data, dict):
            # Check for nested structure, proper keys, etc.
            score = 0.8  # Base score for dictionary
            if len(data) > 0:
                score += 0.1
            if any(isinstance(v, (dict, list)) for v in data.values()):
                score += 0.1  # Bonus for nested structure
            return min(score, 1.0)
        elif isinstance(data, list):
            return 0.9 if data else 0.5
        else:
            return 0.6  # Basic data
    
    def _detect_anomalies(self, data: Any) -> List[str]:
        """Detect data anomalies"""
        anomalies = []
        
        if isinstance(data, dict):
            # Check for suspicious values
            for key, value in data.items():
                if value is None:
                    anomalies.append(f"Null value in field: {key}")
                elif isinstance(value, str) and len(value) > 1000:
                    anomalies.append(f"Unusually long string in field: {key}")
                elif isinstance(value, (int, float)) and abs(value) > 1e10:
                    anomalies.append(f"Extreme numeric value in field: {key}")
        
        elif isinstance(data, list):
            if len(data) > 10000:
                anomalies.append(f"Very large dataset: {len(data)} items")
            
            # Check for inconsistent types
            if data:
                first_type = type(data[0])
                if not all(isinstance(item, first_type) for item in data[:10]):
                    anomalies.append("Inconsistent data types in list")
        
        return anomalies
    
    def _calculate_overall_grade(self, ai_score: float, completeness: float, 
                               consistency: float, structure: float) -> str:
        """Calculate overall data quality grade"""
        overall_score = (ai_score + completeness + consistency + structure) / 4
        
        if overall_score >= 0.9:
            return "A+ (Excellent)"
        elif overall_score >= 0.8:
            return "A (Very Good)"
        elif overall_score >= 0.7:
            return "B (Good)"
        elif overall_score >= 0.6:
            return "C (Fair)"
        else:
            return "D (Poor)"
    
    def _generate_quality_recommendations(self, ai_score: float, completeness: float, 
                                        consistency: float, anomalies: List[str]) -> List[str]:
        """Generate AI-powered recommendations for data quality improvement"""
        recommendations = []
        
        if ai_score < 0.7:
            recommendations.append("πŸ“ Consider data validation and cleaning")
        
        if completeness < 0.8:
            recommendations.append("πŸ” Investigate missing data fields")
        
        if consistency < 0.8:
            recommendations.append("βš™οΈ Standardize data format across records")
        
        if len(anomalies) > 3:
            recommendations.append("🚨 Multiple anomalies detected - requires investigation")
        
        if not recommendations:
            recommendations.append("βœ… Data quality is good - no immediate action needed")
        
        return recommendations
    
    def _basic_quality_assessment(self, data: Any, api_name: str) -> Dict:
        """Basic quality assessment without AI"""
        return {
            "ai_quality_score": 0.0,
            "completeness_score": self._check_completeness(data),
            "consistency_score": 0.8,  # Default
            "structure_score": self._assess_structure(data),
            "anomaly_count": 0,
            "anomalies": [],
            "overall_grade": "C (Basic Assessment)",
            "recommendations": ["Install ML libraries for advanced AI assessment"]
        }

class SemanticDataAnalyzer:
    """Semantic analysis and similarity detection"""
    
    def __init__(self):
        self.embeddings_model = None
        self.stored_embeddings = {}
        self._initialize_model()
    
    def _initialize_model(self):
        """Initialize sentence transformer model"""
        global ML_AVAILABLE
        if ML_AVAILABLE:
            try:
                self.embeddings_model = SentenceTransformer('all-MiniLM-L6-v2')
            except Exception as e:
                ML_AVAILABLE = False
                self.embeddings_model = None
    
    def find_similar_datasets(self, new_data: Any, api_name: str, threshold: float = 0.85) -> List[Dict]:
        """Find semantically similar datasets"""
        if not self.embeddings_model:
            return []
        
        try:
            # Convert data to text and create embedding
            text_data = self._data_to_text(new_data)
            new_embedding = self.embeddings_model.encode([text_data])
            
            # Compare with stored embeddings
            similar_datasets = []
            for stored_key, stored_embedding in self.stored_embeddings.items():
                similarity = cosine_similarity(new_embedding, [stored_embedding])[0][0]
                if similarity > threshold:
                    similar_datasets.append({
                        "dataset": stored_key,
                        "similarity": float(similarity),
                        "api_name": stored_key.split("_")[0] if "_" in stored_key else "unknown"
                    })
            
            # Store new embedding
            embedding_key = f"{api_name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
            self.stored_embeddings[embedding_key] = new_embedding[0]
            
            return sorted(similar_datasets, key=lambda x: x['similarity'], reverse=True)
        
        except Exception as e:
            return []
    
    def _data_to_text(self, data: Any) -> str:
        """Convert data to text for embedding"""
        if isinstance(data, str):
            return data[:500]
        elif isinstance(data, dict):
            # Extract key information
            text_parts = []
            for key, value in list(data.items())[:10]:  # First 10 keys
                text_parts.append(f"{key}: {str(value)[:100]}")
            return " | ".join(text_parts)
        elif isinstance(data, list) and data:
            return str(data[0])[:500]
        else:
            return str(data)[:500]

class APIHealthMonitor:
    """Intelligent API health monitoring with anomaly detection"""
    
    def __init__(self):
        self.anomaly_detector = IsolationForest(contamination=0.1, random_state=42)
        self.health_history = {}
        self.is_trained = False
    
    def monitor_api_health(self, api_name: str, response_time: float, 
                          success_rate: float, data_size: int) -> Dict:
        """Comprehensive API health assessment"""
        current_metrics = {
            "response_time": response_time,
            "success_rate": success_rate,
            "data_size": data_size,
            "timestamp": time.time()
        }
        
        # Store health history
        if api_name not in self.health_history:
            self.health_history[api_name] = []
        
        self.health_history[api_name].append(current_metrics)
        
        # Keep only last 50 measurements
        if len(self.health_history[api_name]) > 50:
            self.health_history[api_name] = self.health_history[api_name][-50:]
        
        # Calculate health score
        health_score = self._calculate_health_score(current_metrics)
        
        # Detect anomalies if we have enough data
        anomaly_score = 0.0
        if len(self.health_history[api_name]) >= 10:
            anomaly_score = self._detect_performance_anomaly(api_name, current_metrics)
        
        # Generate recommendations
        recommendations = self._generate_health_recommendations(
            current_metrics, health_score, anomaly_score
        )
        
        return {
            "health_score": health_score,
            "status": self._get_health_status(health_score),
            "anomaly_score": anomaly_score,
            "is_anomaly": anomaly_score < -0.5,
            "recommendations": recommendations,
            "trend": self._calculate_trend(api_name),
            "metrics": current_metrics
        }
    
    def _calculate_health_score(self, metrics: Dict) -> float:
        """Calculate overall health score (0-1)"""
        # Response time score (lower is better)
        time_score = max(0, 1 - (metrics["response_time"] / 10000))  # 10s max
        
        # Success rate score
        success_score = metrics["success_rate"]
        
        # Data size score (normalized)
        size_score = min(1.0, metrics["data_size"] / 1000000)  # 1MB reference
        
        # Weighted average
        health_score = (time_score * 0.4 + success_score * 0.5 + size_score * 0.1)
        return max(0, min(1, health_score))
    
    def _detect_performance_anomaly(self, api_name: str, current_metrics: Dict) -> float:
        """Detect performance anomalies using isolation forest"""
        try:
            history = self.health_history[api_name]
            
            # Prepare training data
            training_data = []
            for h in history[:-1]:  # Exclude current measurement
                training_data.append([
                    h["response_time"],
                    h["success_rate"],
                    h["data_size"]
                ])
            
            if len(training_data) >= 5:
                # Train anomaly detector
                self.anomaly_detector.fit(training_data)
                
                # Check current metrics
                current_data = [[
                    current_metrics["response_time"],
                    current_metrics["success_rate"],
                    current_metrics["data_size"]
                ]]
                
                anomaly_score = self.anomaly_detector.decision_function(current_data)[0]
                return float(anomaly_score)
        
        except Exception as e:
            pass  # Silent fail for anomaly detection
        
        return 0.0
    
    def _get_health_status(self, health_score: float) -> str:
        """Get health status based on score"""
        if health_score >= 0.9:
            return "🟒 Excellent"
        elif health_score >= 0.7:
            return "🟑 Good"
        elif health_score >= 0.5:
            return "🟠 Fair"
        else:
            return "πŸ”΄ Poor"
    
    def _generate_health_recommendations(self, metrics: Dict, health_score: float, 
                                       anomaly_score: float) -> List[str]:
        """Generate health improvement recommendations"""
        recommendations = []
        
        if metrics["response_time"] > 5000:
            recommendations.append("⏱️ High response time detected - consider caching")
        
        if metrics["success_rate"] < 0.9:
            recommendations.append("❌ Low success rate - check API status")
        
        if anomaly_score < -0.5:
            recommendations.append("🚨 Performance anomaly detected - investigate")
        
        if health_score < 0.6:
            recommendations.append("⚠️ Overall poor health - consider alternatives")
        
        if not recommendations:
            recommendations.append("βœ… API performing well")
        
        return recommendations
    
    def _calculate_trend(self, api_name: str) -> str:
        """Calculate performance trend"""
        if api_name not in self.health_history or len(self.health_history[api_name]) < 5:
            return "πŸ“Š Insufficient data"
        
        recent_scores = []
        for metrics in self.health_history[api_name][-5:]:
            score = self._calculate_health_score(metrics)
            recent_scores.append(score)
        
        if len(recent_scores) >= 3:
            trend = np.polyfit(range(len(recent_scores)), recent_scores, 1)[0]
            
            if trend > 0.02:
                return "πŸ“ˆ Improving"
            elif trend < -0.02:
                return "πŸ“‰ Declining"
            else:
                return "➑️ Stable"
        
        return "πŸ“Š Monitoring"

# Initialize AI components
if ML_AVAILABLE:
    ai_quality_assessor = AIDataQualityAssessor()
    semantic_analyzer = SemanticDataAnalyzer()
    health_monitor = APIHealthMonitor()
else:
    ai_quality_assessor = None
    semantic_analyzer = None
    health_monitor = None

# Simplified API Configuration - Real working endpoints
SIMPLIFIED_API_CONFIG = {
    "Skolverket": {
        "name": "πŸ‡ΈπŸ‡ͺ Skolverket",
        "description": "Swedish National Agency for Education",
        "endpoints": [
            {
                "url": "https://api.skolverket.se/planned-educations/v3",
                "headers": {"Accept": "application/vnd.skolverket.plannededucations.api.v3.hal+json"},
                "method": "GET"
            },
            {
                "url": "https://api.skolverket.se/skolenhetsregister/v2/skolenhet",
                "headers": {"Accept": "application/json"},
                "method": "GET"
            }
        ],
        "rate_limit": None
    },
    "SCB": {
        "name": "πŸ‡ΈπŸ‡ͺ Statistics Sweden", 
        "description": "Swedish National Statistics Office",
        "endpoints": [
            {
                "url": "https://api.scb.se/OV0104/v1/doris/sv/ssd/START/BE/BE0101/BE0101A/BefolkningNy",
                "headers": {"Content-Type": "application/json"},
                "method": "POST",
                "data": {
                    "query": [
                        {"code": "Region", "selection": {"filter": "item", "values": ["00"]}},
                        {"code": "Civilstand", "selection": {"filter": "item", "values": ["TOT"]}},
                        {"code": "Alder", "selection": {"filter": "item", "values": ["tot"]}},
                        {"code": "Kon", "selection": {"filter": "item", "values": ["1", "2"]}},
                        {"code": "ContentsCode", "selection": {"filter": "item", "values": ["BE0101N1"]}},
                        {"code": "Tid", "selection": {"filter": "item", "values": ["2023"]}}
                    ],
                    "response": {"format": "json"}
                }
            }
        ],
        "rate_limit": {"requests": 10, "per_seconds": 10}
    },
    "Kolada": {
        "name": "πŸ‡ΈπŸ‡ͺ Kolada",
        "description": "Municipal Key Performance Indicators",
        "endpoints": [
            {
                "url": "https://api.kolada.se/v2/municipality",
                "headers": {"Accept": "application/json"},
                "method": "GET"
            },
            {
                "url": "https://api.kolada.se/v2/kpi",
                "headers": {"Accept": "application/json"},
                "method": "GET"
            }
        ],
        "rate_limit": None
    },
    "Eurostat": {
        "name": "πŸ‡ͺπŸ‡Ί Eurostat",
        "description": "European Union Statistics",
        "endpoints": [
            {
                "url": "https://ec.europa.eu/eurostat/api/dissemination/statistics/1.0/data/demo_pjan?format=JSON&lang=en&geo=EU27_2020&age=TOTAL&sex=T&time=2023",
                "headers": {"Accept": "application/json"},
                "method": "GET"
            }
        ],
        "rate_limit": None
    },
    "WHO": {
        "name": "🌍 WHO",
        "description": "World Health Organization",
        "endpoints": [
            {
                "url": "https://ghoapi.azureedge.net/api/WHOSIS_000001",
                "headers": {"Accept": "application/json"},
                "method": "GET"
            },
            {
                "url": "https://ghoapi.azureedge.net/api/Dimension",
                "headers": {"Accept": "application/json"},
                "method": "GET"
            }
        ],
        "rate_limit": None
    },
    "OECD": {
        "name": "🌍 OECD",
        "description": "Organisation for Economic Co-operation and Development",
        "endpoints": [
            {
                "url": "https://sdmx.oecd.org/public/rest/data/OECD.SDD.NAD,DSD_NAMAIN1@DF_QNA,1.0/AUS.B1GQ.C.Q?format=jsondata",
                "headers": {"Accept": "application/vnd.sdmx.data+json;version=1.0.0"},
                "method": "GET"
            }
        ],
        "rate_limit": None
    },
    "WorldBank": {
        "name": "🌍 World Bank",
        "description": "International Financial Institution",
        "endpoints": [
            {
                "url": "https://api.worldbank.org/v2/country?format=json&per_page=50",
                "headers": {"Accept": "application/json"},
                "method": "GET"
            },
            {
                "url": "https://api.worldbank.org/v2/indicator/SP.POP.TOTL?format=json&date=2023&per_page=50",
                "headers": {"Accept": "application/json"},
                "method": "GET"
            }
        ],
        "rate_limit": None
    },
    "Riksbanken": {
        "name": "πŸ‡ΈπŸ‡ͺ Riksbanken",
        "description": "Swedish Central Bank",
        "endpoints": [
            {
                "url": "https://api.riksbank.se/swea/v1/Observations/SEKEURPMI/2023-01-01/2023-12-31",
                "headers": {"Accept": "application/json"},
                "method": "GET"
            }
        ],
        "rate_limit": {"requests": 5, "per_seconds": 60}
    },
    "Swecris": {
        "name": "πŸ‡ΈπŸ‡ͺ Swecris",
        "description": "Swedish Research Council Database",
        "endpoints": [
            {
                "url": "https://swecris-api.vr.se/v1/projects?size=50",
                "headers": {
                    "Accept": "application/json",
                    "Authorization": "Bearer VRSwecrisAPI2025-1"
                },
                "method": "GET"
            }
        ],
        "rate_limit": None
    },
    "CSN": {
        "name": "πŸ‡ΈπŸ‡ͺ CSN",
        "description": "Swedish Board of Student Finance",
        "endpoints": [
            {
                "url": "https://statistik.csn.se/PXWeb/api/v1/sv/CSNstat/StudiebidragGymnasieskola/SS0101B1.px",
                "headers": {"Content-Type": "application/json"},
                "method": "POST",
                "data": {
                    "query": [
                        {"code": "Region", "selection": {"filter": "item", "values": ["00"]}},
                        {"code": "ContentsCode", "selection": {"filter": "item", "values": ["SS0101B1"]}},
                        {"code": "Tid", "selection": {"filter": "item", "values": ["2023"]}}
                    ],
                    "response": {"format": "json"}
                }
            }
        ],
        "rate_limit": None
    }
}

def init_enhanced_database():
    """Initialize optimized SQLite database with comprehensive schema and performance enhancements"""
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    
    # Enable WAL mode for better concurrency and performance
    cursor.execute('PRAGMA journal_mode=WAL')
    cursor.execute('PRAGMA synchronous=NORMAL')
    cursor.execute('PRAGMA cache_size=10000')
    cursor.execute('PRAGMA temp_store=MEMORY')
    cursor.execute('PRAGMA mmap_size=268435456')  # 256MB
    
    # Enhanced endpoints table with better indexing
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS discovered_endpoints (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            api_name TEXT NOT NULL,
            endpoint_path TEXT NOT NULL,
            full_url TEXT NOT NULL,
            discovery_method TEXT,
            depth_level INTEGER DEFAULT 0,
            parent_endpoint TEXT,
            endpoint_type TEXT,
            last_checked TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            is_active BOOLEAN DEFAULT 1,
            response_format TEXT,
            parameters_schema TEXT,
            estimated_records INTEGER DEFAULT 0,
            last_fetch_status TEXT,
            creation_date DATE DEFAULT (date('now')),
            UNIQUE(api_name, endpoint_path)
        )
    ''')
    
    # Create indexes for endpoints table
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_endpoints_api_name ON discovered_endpoints(api_name)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_endpoints_active ON discovered_endpoints(is_active)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_endpoints_last_checked ON discovered_endpoints(last_checked)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_endpoints_depth ON discovered_endpoints(depth_level)')
    
    # Optimized data storage table with compression and partitioning support
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS harvested_data (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            api_name TEXT NOT NULL,
            endpoint_path TEXT NOT NULL,
            data_hash TEXT UNIQUE NOT NULL,
            raw_data_compressed BLOB,
            processed_data_compressed BLOB,
            raw_data_size INTEGER,
            processed_data_size INTEGER,
            record_count INTEGER DEFAULT 0,
            data_size_bytes INTEGER DEFAULT 0,
            fetch_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            fetch_duration_ms INTEGER DEFAULT 0,
            status TEXT DEFAULT 'success',
            error_message TEXT,
            session_id TEXT,
            quality_score REAL DEFAULT 0.0,
            health_score REAL DEFAULT 0.0,
            similar_datasets TEXT DEFAULT '[]',
            data_format TEXT,
            api_version TEXT,
            fetch_date DATE DEFAULT (date('now')),
            last_accessed TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            access_count INTEGER DEFAULT 1,
            CHECK (status IN ('success', 'error', 'partial', 'timeout'))
        )
    ''')
    
    # Create comprehensive indexes for data table
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_data_api_name ON harvested_data(api_name)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_data_fetch_date ON harvested_data(fetch_date)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_data_status ON harvested_data(status)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_data_session ON harvested_data(session_id)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_data_hash ON harvested_data(data_hash)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_data_quality ON harvested_data(quality_score)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_data_records ON harvested_data(record_count)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_data_size ON harvested_data(data_size_bytes)')
    
    # Enhanced session management table
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS harvest_sessions (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            session_id TEXT UNIQUE NOT NULL,
            session_name TEXT,
            started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            last_activity TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            completed_at TIMESTAMP,
            total_endpoints INTEGER DEFAULT 0,
            processed_endpoints INTEGER DEFAULT 0,
            successful_fetches INTEGER DEFAULT 0,
            failed_fetches INTEGER DEFAULT 0,
            total_records INTEGER DEFAULT 0,
            total_data_size INTEGER DEFAULT 0,
            session_status TEXT DEFAULT 'active',
            current_api TEXT,
            current_endpoint TEXT,
            session_config TEXT,
            error_count INTEGER DEFAULT 0,
            avg_fetch_time REAL DEFAULT 0.0,
            session_type TEXT DEFAULT 'manual',
            priority INTEGER DEFAULT 1,
            CHECK (session_status IN ('active', 'paused', 'completed', 'failed', 'cancelled'))
        )
    ''')
    
    # Create indexes for sessions table
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_sessions_status ON harvest_sessions(session_status)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_sessions_started ON harvest_sessions(started_at)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_sessions_activity ON harvest_sessions(last_activity)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_sessions_priority ON harvest_sessions(priority)')
    
    # Enhanced discovery progress table
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS discovery_progress (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            api_name TEXT NOT NULL,
            discovery_session TEXT,
            started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            completed_at TIMESTAMP,
            endpoints_found INTEGER DEFAULT 0,
            depth_reached INTEGER DEFAULT 0,
            discovery_status TEXT DEFAULT 'running',
            discovery_config TEXT,
            errors_encountered INTEGER DEFAULT 0,
            success_rate REAL DEFAULT 0.0,
            estimated_total INTEGER DEFAULT 0,
            CHECK (discovery_status IN ('running', 'completed', 'failed', 'paused'))
        )
    ''')
    
    # Create indexes for discovery table
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_discovery_api ON discovery_progress(api_name)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_discovery_status ON discovery_progress(discovery_status)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_discovery_started ON discovery_progress(started_at)')
    
    # Data quality and metadata table
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS data_quality_metrics (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            data_id INTEGER REFERENCES harvested_data(id),
            api_name TEXT NOT NULL,
            completeness_score REAL DEFAULT 0.0,
            consistency_score REAL DEFAULT 0.0,
            accuracy_score REAL DEFAULT 0.0,
            timeliness_score REAL DEFAULT 0.0,
            overall_quality REAL DEFAULT 0.0,
            anomalies_detected INTEGER DEFAULT 0,
            anomaly_details TEXT,
            validation_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            validation_rules_version TEXT DEFAULT '1.0'
        )
    ''')
    
    # Create quality metrics indexes
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_quality_api ON data_quality_metrics(api_name)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_quality_overall ON data_quality_metrics(overall_quality)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_quality_timestamp ON data_quality_metrics(validation_timestamp)')
    
    # API performance tracking table
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS api_performance_log (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            api_name TEXT NOT NULL,
            endpoint_path TEXT NOT NULL,
            response_time_ms INTEGER,
            response_size_bytes INTEGER,
            http_status_code INTEGER,
            success BOOLEAN,
            error_type TEXT,
            timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            date_only DATE DEFAULT (date('now'))
        )
    ''')
    
    # Create performance indexes
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_perf_api_date ON api_performance_log(api_name, date_only)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_perf_success ON api_performance_log(success)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_perf_response_time ON api_performance_log(response_time_ms)')
    
    # Data archival management table
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS data_archive_log (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            original_data_id INTEGER,
            archive_path TEXT,
            archive_format TEXT DEFAULT 'gzip',
            archived_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            original_size INTEGER,
            compressed_size INTEGER,
            compression_ratio REAL,
            checksum TEXT,
            retention_date DATE,
            archive_status TEXT DEFAULT 'active'
        )
    ''')
    
    # Create views for common queries
    cursor.execute('''
        CREATE VIEW IF NOT EXISTS v_api_summary AS
        SELECT 
            api_name,
            COUNT(*) as total_fetches,
            COUNT(CASE WHEN status = 'success' THEN 1 END) as successful_fetches,
            SUM(record_count) as total_records,
            SUM(data_size_bytes) as total_data_size,
            AVG(fetch_duration_ms) as avg_fetch_time,
            AVG(quality_score) as avg_quality_score,
            MAX(fetch_timestamp) as last_fetch,
            MIN(fetch_timestamp) as first_fetch
        FROM harvested_data
        GROUP BY api_name
    ''')
    
    cursor.execute('''
        CREATE VIEW IF NOT EXISTS v_session_summary AS
        SELECT 
            session_id,
            session_name,
            session_status,
            started_at,
            completed_at,
            total_endpoints,
            processed_endpoints,
            successful_fetches,
            failed_fetches,
            total_records,
            total_data_size,
            CASE 
                WHEN total_endpoints > 0 THEN 
                    ROUND((processed_endpoints * 100.0) / total_endpoints, 2)
                ELSE 0 
            END as completion_percentage,
            CASE 
                WHEN processed_endpoints > 0 THEN 
                    ROUND((successful_fetches * 100.0) / processed_endpoints, 2)
                ELSE 0 
            END as success_percentage
        FROM harvest_sessions
    ''')
    
    # Enable automatic statistics collection
    cursor.execute('PRAGMA optimize')
    
    conn.commit()
    conn.close()

# Database optimization and maintenance functions
def optimize_database():
    """Perform database optimization and maintenance"""
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    
    try:
        # Update statistics
        cursor.execute('ANALYZE')
        
        # Vacuum if necessary (reclaim space)
        cursor.execute('PRAGMA auto_vacuum=INCREMENTAL')
        cursor.execute('PRAGMA incremental_vacuum')
        
        # Optimize query planner
        cursor.execute('PRAGMA optimize')
        
        conn.commit()
        return True
    except Exception as e:
        return False
    finally:
        conn.close()

def get_database_stats():
    """Get comprehensive database statistics"""
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    
    try:
        stats = {}
        
        # Basic table counts
        tables = ['discovered_endpoints', 'harvested_data', 'harvest_sessions', 'discovery_progress']
        for table in tables:
            cursor.execute(f'SELECT COUNT(*) FROM {table}')
            stats[f'{table}_count'] = cursor.fetchone()[0]
        
        # Database size
        cursor.execute('PRAGMA page_count')
        page_count = cursor.fetchone()[0]
        cursor.execute('PRAGMA page_size')
        page_size = cursor.fetchone()[0]
        stats['database_size_mb'] = round((page_count * page_size) / (1024 * 1024), 2)
        
        # Data quality stats
        cursor.execute('SELECT AVG(quality_score), AVG(health_score) FROM harvested_data WHERE status = "success"')
        quality_stats = cursor.fetchone()
        stats['avg_quality_score'] = round(quality_stats[0] or 0, 3)
        stats['avg_health_score'] = round(quality_stats[1] or 0, 3)
        
        # Recent activity
        cursor.execute('''
            SELECT COUNT(*) FROM harvested_data 
            WHERE fetch_timestamp > datetime('now', '-24 hours')
        ''')
        stats['recent_fetches_24h'] = cursor.fetchone()[0]
        
        return stats
    
    finally:
        conn.close()

def compress_old_data(days_old=30):
    """Compress data older than specified days"""
    import gzip
    import json
    
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    
    try:
        # Find old data to compress
        cursor.execute('''
            SELECT id, raw_data, processed_data 
            FROM harvested_data 
            WHERE fetch_timestamp < datetime('now', '-{} days')
            AND raw_data_compressed IS NULL
        '''.format(days_old))
        
        old_records = cursor.fetchall()
        compressed_count = 0
        
        for record_id, raw_data, processed_data in old_records:
            try:
                # Compress raw data
                raw_compressed = None
                if raw_data:
                    raw_compressed = gzip.compress(raw_data.encode('utf-8'))
                
                # Compress processed data
                processed_compressed = None
                if processed_data:
                    processed_compressed = gzip.compress(processed_data.encode('utf-8'))
                
                # Update record with compressed data
                cursor.execute('''
                    UPDATE harvested_data 
                    SET raw_data_compressed = ?, 
                        processed_data_compressed = ?,
                        raw_data = NULL,
                        processed_data = NULL,
                        raw_data_size = ?,
                        processed_data_size = ?
                    WHERE id = ?
                ''', (
                    raw_compressed, 
                    processed_compressed,
                    len(raw_data) if raw_data else 0,
                    len(processed_data) if processed_data else 0,
                    record_id
                ))
                
                compressed_count += 1
                
            except Exception as e:
                continue  # Skip problematic records
        
        conn.commit()
        return compressed_count
    
    finally:
        conn.close()

def backup_database(backup_path=None):
    """Create a backup of the database"""
    import shutil
    from datetime import datetime
    
    if backup_path is None:
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        backup_path = f"backup_harvester_{timestamp}.db"
    
    try:
        shutil.copy2(DB_PATH, backup_path)
        return backup_path
    except Exception as e:
        return None

class SimplifiedDataHarvester:
    """Simplified data harvester - one function to fetch from all APIs"""
    
    def __init__(self):
        self.session = requests.Session()
        self.session.headers.update({
            'User-Agent': 'Simplified-Data-Harvester/1.0 (Research & Analysis)'
        })
        self.results = {}
        self.errors = {}
    
    def fetch_all_apis(self, progress_callback=None) -> Dict:
        """One function to fetch data from all APIs automatically"""
        session_id = f"simplified_{int(time.time())}"
        total_apis = len(SIMPLIFIED_API_CONFIG)
        completed = 0
        
        if progress_callback:
            progress_callback(f"πŸš€ Starting comprehensive data collection from {total_apis} APIs...")
        
        for api_name, config in SIMPLIFIED_API_CONFIG.items():
            if progress_callback:
                progress_callback(f"πŸ”„ Fetching from {config['name']}...")
            
            try:
                api_results = self._fetch_api_data(api_name, config, session_id)
                self.results[api_name] = api_results
                completed += 1
                
                if progress_callback:
                    progress = (completed / total_apis) * 100
                    progress_callback(f"βœ… {config['name']} completed ({progress:.1f}%)")
                
                # Apply rate limiting if specified
                if config.get('rate_limit'):
                    rate_limit = config['rate_limit']
                    sleep_time = rate_limit['per_seconds'] / rate_limit['requests']
                    time.sleep(sleep_time)
                else:
                    time.sleep(0.5)  # Default delay between APIs
                    
            except Exception as e:
                self.errors[api_name] = str(e)
                if progress_callback:
                    progress_callback(f"❌ {config['name']} failed: {str(e)[:50]}...")
                completed += 1
        
        if progress_callback:
            successful = len(self.results)
            failed = len(self.errors)
            progress_callback(f"πŸŽ‰ Collection complete! βœ… {successful} successful, ❌ {failed} failed")
        
        return {
            "results": self.results,
            "errors": self.errors,
            "session_id": session_id,
            "summary": {
                "total_apis": total_apis,
                "successful": len(self.results),
                "failed": len(self.errors),
                "success_rate": (len(self.results) / total_apis) * 100
            }
        }
    
    def _fetch_api_data(self, api_name: str, config: Dict, session_id: str) -> Dict:
        """Fetch data from all endpoints for a specific API"""
        api_results = {
            "api_name": api_name,
            "endpoints": [],
            "total_records": 0,
            "total_size": 0
        }
        
        for i, endpoint in enumerate(config['endpoints']):
            try:
                start_time = time.time()
                
                # Make request
                if endpoint.get('method', 'GET').upper() == 'POST':
                    response = self.session.post(
                        endpoint['url'],
                        headers=endpoint.get('headers', {}),
                        json=endpoint.get('data', {}),
                        timeout=30
                    )
                else:
                    response = self.session.get(
                        endpoint['url'],
                        headers=endpoint.get('headers', {}),
                        timeout=30
                    )
                
                response.raise_for_status()
                
                # Process response
                data = self._process_response(response, api_name)
                fetch_duration = int((time.time() - start_time) * 1000)
                
                # Extract meaningful data
                processed_data = self._extract_api_data(data, api_name)
                record_count = self._count_records(processed_data)
                data_size = len(response.content)
                
                # Save to database
                endpoint_path = f"endpoint_{i+1}"
                self._save_data_to_db(
                    api_name, endpoint_path, processed_data, session_id,
                    fetch_duration, record_count, data_size, "success"
                )
                
                endpoint_result = {
                    "endpoint_url": endpoint['url'],
                    "status": "success",
                    "records": record_count,
                    "size_bytes": data_size,
                    "duration_ms": fetch_duration,
                    "data_preview": self._create_data_preview(processed_data)
                }
                
                api_results["endpoints"].append(endpoint_result)
                api_results["total_records"] += record_count
                api_results["total_size"] += data_size
                
            except Exception as e:
                endpoint_result = {
                    "endpoint_url": endpoint['url'],
                    "status": "error",
                    "error": str(e),
                    "records": 0,
                    "size_bytes": 0,
                    "duration_ms": 0
                }
                api_results["endpoints"].append(endpoint_result)
        
        return api_results
    
    def _process_response(self, response, api_name: str):
        """Process API response based on content type"""
        content_type = response.headers.get('content-type', '').lower()
        
        if 'json' in content_type:
            return response.json()
        elif 'xml' in content_type:
            return self._xml_to_dict(response.text)
        else:
            try:
                return response.json()  # Try JSON first
            except:
                return {"raw_content": response.text}
    
    def _xml_to_dict(self, xml_text: str) -> Dict:
        """Convert XML to dictionary"""
        try:
            import xml.etree.ElementTree as ET
            root = ET.fromstring(xml_text)
            return self._element_to_dict(root)
        except:
            return {"raw_xml": xml_text}
    
    def _element_to_dict(self, element) -> Dict:
        """Convert XML element to dictionary"""
        result = {}
        
        if element.attrib:
            result.update(element.attrib)
        
        if element.text and element.text.strip():
            if len(element) == 0:
                return element.text.strip()
            result['text'] = element.text.strip()
        
        for child in element:
            child_data = self._element_to_dict(child)
            if child.tag in result:
                if not isinstance(result[child.tag], list):
                    result[child.tag] = [result[child.tag]]
                result[child.tag].append(child_data)
            else:
                result[child.tag] = child_data
        
        return result
    
    def _extract_api_data(self, data: Any, api_name: str) -> Any:
        """Extract meaningful data from API response based on API type"""
        if api_name == "Skolverket":
            if isinstance(data, dict):
                if "_embedded" in data:
                    return data["_embedded"]
                elif "skolenheter" in data:
                    return data["skolenheter"]
            return data
        
        elif api_name == "SCB":
            if isinstance(data, dict):
                return data.get("data", data.get("variables", data))
        
        elif api_name == "Kolada":
            if isinstance(data, dict):
                return data.get("values", data)
        
        elif api_name == "Eurostat":
            if isinstance(data, dict):
                return data.get("value", data.get("data", data))
        
        elif api_name == "WHO":
            if isinstance(data, dict):
                return data.get("value", data.get("fact", data))
        
        elif api_name == "OECD":
            if isinstance(data, dict):
                if "data" in data:
                    return data["data"]
                return data
        
        elif api_name == "WorldBank":
            if isinstance(data, list) and len(data) > 1:
                return data[1] if data[1] else data[0]
            return data
        
        elif api_name == "Riksbanken":
            if isinstance(data, dict):
                return data.get("observations", data.get("data", data))
        
        elif api_name == "Swecris":
            if isinstance(data, dict):
                return data.get("items", data.get("projects", data))
        
        elif api_name == "CSN":
            if isinstance(data, dict):
                return data.get("data", data.get("variables", data))
        
        return data
    
    def _count_records(self, data: Any) -> int:
        """Count records in the data"""
        if isinstance(data, list):
            return len(data)
        elif isinstance(data, dict):
            # Try to find arrays that represent records
            for key, value in data.items():
                if isinstance(value, list) and len(value) > 0:
                    return len(value)
            return 1
        else:
            return 1 if data else 0
    
    def _create_data_preview(self, data: Any) -> Dict:
        """Create a preview of the data for display"""
        preview = {
            "type": type(data).__name__,
            "sample": None
        }
        
        if isinstance(data, list):
            preview["length"] = len(data)
            preview["sample"] = data[:3] if len(data) > 3 else data
        elif isinstance(data, dict):
            preview["keys"] = list(data.keys())[:10]
            if data:
                first_key = list(data.keys())[0]
                preview["sample"] = {first_key: data[first_key]}
        else:
            preview["sample"] = str(data)[:200]
        
        return preview
    
    def _save_data_to_db(self, api_name: str, endpoint_path: str, data: Any,
                        session_id: str, fetch_duration: int, record_count: int,
                        data_size: int, status: str, error_message: str = None):
        """Save data to database with optimization"""
        import gzip
        
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        try:
            # Create data hash for deduplication
            data_str = json.dumps(data, sort_keys=True, default=str)
            data_hash = hashlib.sha256(data_str.encode()).hexdigest()
            
            # Check if data exists
            cursor.execute('SELECT id FROM harvested_data WHERE data_hash = ?', (data_hash,))
            if cursor.fetchone():
                return  # Skip duplicate
            
            # Compress if large
            raw_data_compressed = None
            raw_data = None
            
            if data_size > 1024:
                try:
                    raw_data_compressed = gzip.compress(data_str.encode('utf-8'))
                except:
                    raw_data = data_str
            else:
                raw_data = data_str
            
            # Insert data
            cursor.execute('''
                INSERT INTO harvested_data 
                (api_name, endpoint_path, data_hash, raw_data, raw_data_compressed,
                 record_count, data_size_bytes, fetch_duration_ms, status, 
                 error_message, session_id, data_format)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            ''', (
                api_name, endpoint_path, data_hash, raw_data, raw_data_compressed,
                record_count, data_size, fetch_duration, status, error_message, 
                session_id, self._detect_data_format(data)
            ))
            
            conn.commit()
            
        except Exception as e:
            # Fallback to basic schema
            try:
                cursor.execute('''
                    INSERT OR REPLACE INTO harvested_data 
                    (api_name, endpoint_path, data_hash, raw_data, record_count, 
                     data_size_bytes, fetch_duration_ms, status, session_id)
                    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
                ''', (
                    api_name, endpoint_path, data_hash, data_str[:10000],  # Limit size
                    record_count, data_size, fetch_duration, status, session_id
                ))
                conn.commit()
            except:
                pass  # Silent fail
                
        finally:
            conn.close()
    
    def _detect_data_format(self, data: Any) -> str:
        """Detect data format"""
        if isinstance(data, dict):
            if "_embedded" in data or "_links" in data:
                return "HAL+JSON"
            elif "query" in data or "variables" in data:
                return "PX-Web"
            else:
                return "JSON"
        elif isinstance(data, list):
            return "JSON-Array"
        else:
            return "Unknown"
        if not config:
            return []
        
        discovered = []
        session_id = f"discovery_{api_name}_{int(time.time())}"
        
        if progress_callback:
            progress_callback(f"πŸ” Starting deep discovery for {api_name}...")
        
        # Apply rate limiting
        self._apply_rate_limit(config)
        
        # Explore each known root recursively
        for root_path in config["known_roots"]:
            if progress_callback:
                progress_callback(f"πŸ”Ž Exploring root: {root_path}")
            
            root_endpoints = self._explore_endpoint_recursively(
                api_name, config, root_path, 0, config["explore_depth"], progress_callback
            )
            discovered.extend(root_endpoints)
        
        # Try to discover through API documentation pages
        doc_endpoints = self._discover_from_documentation(api_name, config, progress_callback)
        discovered.extend(doc_endpoints)
        
        # Save discovery results
        self._save_discovery_results(api_name, session_id, discovered)
        
        if progress_callback:
            progress_callback(f"βœ… Discovery complete: {len(discovered)} endpoints found for {api_name}")
        
        return discovered
    
    def _explore_endpoint_recursively(self, api_name: str, config: Dict, path: str, 
                                    current_depth: int, max_depth: int, progress_callback=None) -> List[Dict]:
        """Recursively explore API endpoints"""
        if current_depth >= max_depth:
            return []
        
        discovered = []
        full_url = config["base_url"] + path
        
        try:
            # Apply authentication if needed
            headers = self._get_auth_headers(config)
            
            response = self.session.get(full_url, headers=headers, timeout=15)
            
            if response.status_code == 200:
                # Parse response to find more endpoints
                endpoints = self._extract_endpoints_from_response(
                    api_name, config, response, path, current_depth
                )
                
                for endpoint in endpoints:
                    discovered.append(endpoint)
                    
                    # Recursively explore found endpoints
                    if current_depth < max_depth - 1:
                        sub_endpoints = self._explore_endpoint_recursively(
                            api_name, config, endpoint["path"], 
                            current_depth + 1, max_depth, progress_callback
                        )
                        discovered.extend(sub_endpoints)
                
                if progress_callback and discovered:
                    progress_callback(f"πŸ“‘ Found {len(discovered)} endpoints at depth {current_depth}")
            
        except Exception as e:
            if progress_callback:
                progress_callback(f"⚠️ Error exploring {path}: {str(e)[:100]}")
        
        self._apply_rate_limit(config)
        return discovered
    
    def _extract_endpoints_from_response(self, api_name: str, config: Dict, response: requests.Response, 
                                       parent_path: str, depth: int) -> List[Dict]:
        """Extract endpoint information from API response"""
        endpoints = []
        
        try:
            # Try JSON parsing first
            if 'application/json' in response.headers.get('Content-Type', ''):
                data = response.json()
                endpoints.extend(self._extract_from_json(api_name, config, data, parent_path, depth))
            
            # Parse HTML for documentation links
            elif 'text/html' in response.headers.get('Content-Type', ''):
                endpoints.extend(self._extract_from_html(api_name, config, response.text, parent_path, depth))
            
            # Parse XML responses
            elif 'xml' in response.headers.get('Content-Type', ''):
                endpoints.extend(self._extract_from_xml(api_name, config, response.text, parent_path, depth))
        
        except Exception as e:
            pass  # Continue with other extraction methods
        
        return endpoints
    
    def _extract_from_json(self, api_name: str, config: Dict, data: Any, parent_path: str, depth: int) -> List[Dict]:
        """Extract endpoints from JSON response"""
        endpoints = []
        
        if isinstance(data, dict):
            # Look for common API documentation patterns
            if '_links' in data:  # HAL format
                for link_key, link_data in data['_links'].items():
                    if isinstance(link_data, dict) and 'href' in link_data:
                        endpoint_path = self._normalize_path(link_data['href'])
                        endpoints.append(self._create_endpoint_info(
                            api_name, endpoint_path, 'HAL_link', parent_path, depth + 1
                        ))
            
            if 'paths' in data:  # OpenAPI/Swagger
                for path in data['paths'].keys():
                    endpoint_path = self._normalize_path(path)
                    endpoints.append(self._create_endpoint_info(
                        api_name, endpoint_path, 'OpenAPI', parent_path, depth + 1
                    ))
            
            # Look for URL patterns in values
            for key, value in data.items() if isinstance(data, dict) else []:
                if isinstance(value, str) and self._is_api_path(value, config):
                    endpoint_path = self._normalize_path(value)
                    endpoints.append(self._create_endpoint_info(
                        api_name, endpoint_path, 'JSON_value', parent_path, depth + 1
                    ))
        
        elif isinstance(data, list):
            for item in data:
                endpoints.extend(self._extract_from_json(api_name, config, item, parent_path, depth))
        
        return endpoints
    
    def _extract_from_html(self, api_name: str, config: Dict, html: str, parent_path: str, depth: int) -> List[Dict]:
        """Extract endpoints from HTML documentation"""
        endpoints = []
        
        # Look for API endpoint patterns in HTML
        patterns = [
            r'href=["\']([^"\']*(?:api|/v\d+)[^"\']*)["\']',
            r'url["\']?\s*[:=]\s*["\']([^"\']*(?:api|/v\d+)[^"\']*)["\']',
            r'endpoint["\']?\s*[:=]\s*["\']([^"\']*)["\']'
        ]
        
        for pattern in patterns:
            matches = re.finditer(pattern, html, re.IGNORECASE)
            for match in matches:
                potential_path = match.group(1)
                if self._is_api_path(potential_path, config):
                    endpoint_path = self._normalize_path(potential_path)
                    endpoints.append(self._create_endpoint_info(
                        api_name, endpoint_path, 'HTML_link', parent_path, depth + 1
                    ))
        
        return endpoints
    
    def _extract_from_xml(self, api_name: str, config: Dict, xml_text: str, parent_path: str, depth: int) -> List[Dict]:
        """Extract endpoints from XML response"""
        endpoints = []
        
        try:
            root = ET.fromstring(xml_text)
            
            # Look for URL attributes and text content
            for elem in root.iter():
                # Check attributes
                for attr_value in elem.attrib.values():
                    if self._is_api_path(attr_value, config):
                        endpoint_path = self._normalize_path(attr_value)
                        endpoints.append(self._create_endpoint_info(
                            api_name, endpoint_path, 'XML_attr', parent_path, depth + 1
                        ))
                
                # Check text content
                if elem.text and self._is_api_path(elem.text, config):
                    endpoint_path = self._normalize_path(elem.text)
                    endpoints.append(self._create_endpoint_info(
                        api_name, endpoint_path, 'XML_text', parent_path, depth + 1
                    ))
        
        except ET.ParseError:
            pass
        
        return endpoints
    
    def _discover_from_documentation(self, api_name: str, config: Dict, progress_callback=None) -> List[Dict]:
        """Discover endpoints from API documentation pages"""
        endpoints = []
        
        # Common documentation paths
        doc_paths = [
            '/docs', '/documentation', '/api-docs', '/swagger', '/openapi',
            '/help', '/reference', '/guide', '/v1/docs', '/v2/docs'
        ]
        
        for doc_path in doc_paths:
            try:
                full_url = config["base_url"] + doc_path
                headers = self._get_auth_headers(config)
                
                response = self.session.get(full_url, headers=headers, timeout=10)
                
                if response.status_code == 200:
                    doc_endpoints = self._extract_endpoints_from_response(
                        api_name, config, response, doc_path, 0
                    )
                    endpoints.extend(doc_endpoints)
                    
                    if progress_callback and doc_endpoints:
                        progress_callback(f"πŸ“š Found {len(doc_endpoints)} endpoints in documentation")
                
            except Exception:
                continue
            
            self._apply_rate_limit(config)
        
        return endpoints
    
    def _is_api_path(self, path: str, config: Dict) -> bool:
        """Check if a path looks like a valid API endpoint"""
        if not isinstance(path, str) or len(path) < 2:
            return False
        
        # Must start with / or be a relative path
        if not (path.startswith('/') or not path.startswith('http')):
            return False
        
        # Check against discovery patterns
        for pattern in config["discovery_patterns"]:
            if re.match(pattern, path):
                return True
        
        # General API path indicators
        api_indicators = ['/api/', '/v1/', '/v2/', '/v3/', '/rest/', '/data/']
        return any(indicator in path.lower() for indicator in api_indicators)
    
    def _normalize_path(self, path: str) -> str:
        """Normalize API path"""
        # Remove base URL if present
        if path.startswith('http'):
            parsed = urlparse(path)
            path = parsed.path
        
        # Ensure starts with /
        if not path.startswith('/'):
            path = '/' + path
        
        # Remove trailing slash
        if path.endswith('/') and len(path) > 1:
            path = path[:-1]
        
        return path
    
    def _create_endpoint_info(self, api_name: str, path: str, discovery_method: str, 
                            parent_path: str, depth: int) -> Dict:
        """Create endpoint information dictionary"""
        return {
            "api_name": api_name,
            "path": path,
            "full_url": DEEP_API_CONFIG[api_name]["base_url"] + path,
            "discovery_method": discovery_method,
            "parent_path": parent_path,
            "depth": depth,
            "discovered_at": datetime.now().isoformat()
        }
    
    def _get_auth_headers(self, config: Dict) -> Dict:
        """Get authentication headers for API"""
        headers = {}
        auth = config.get("auth")
        
        if auth and auth.get("type") == "Bearer":
            headers["Authorization"] = f"Bearer {auth['token']}"
        
        return headers
    
    def _apply_rate_limit(self, config: Dict):
        """Apply rate limiting for API"""
        rate_limit = config.get("rate_limit")
        if rate_limit:
            sleep_time = rate_limit["per_seconds"] / rate_limit["requests"]
            time.sleep(sleep_time)
    
    def _save_discovery_results(self, api_name: str, session_id: str, endpoints: List[Dict]):
        """Save discovery results to database"""
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        for endpoint in endpoints:
            cursor.execute('''
                INSERT OR REPLACE INTO discovered_endpoints 
                (api_name, endpoint_path, full_url, discovery_method, depth_level, 
                 parent_endpoint, last_checked, response_format)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?)
            ''', (
                endpoint["api_name"],
                endpoint["path"],
                endpoint["full_url"],
                endpoint["discovery_method"],
                endpoint["depth"],
                endpoint.get("parent_path", ""),
                datetime.now(),
                "unknown"
            ))
        
        # Update discovery progress
        cursor.execute('''
            INSERT INTO discovery_progress 
            (api_name, discovery_session, completed_at, endpoints_found, discovery_status)
            VALUES (?, ?, ?, ?, ?)
        ''', (api_name, session_id, datetime.now(), len(endpoints), "completed"))
        
        conn.commit()
        conn.close()

class SessionManager:
    """Manage harvest sessions with resumption capability"""
    
    def __init__(self):
        self.current_session = None
        
    def create_session(self, session_name: str = None) -> str:
        """Create a new harvest session"""
        session_id = f"session_{int(time.time())}"
        if not session_name:
            session_name = f"Harvest Session {datetime.now().strftime('%Y-%m-%d %H:%M')}"
        
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        cursor.execute('''
            INSERT INTO harvest_sessions 
            (session_id, session_name, session_status)
            VALUES (?, ?, ?)
        ''', (session_id, session_name, "active"))
        
        conn.commit()
        conn.close()
        
        self.current_session = session_id
        return session_id
    
    def get_last_session(self) -> Optional[Dict]:
        """Get the most recent session for resumption"""
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        cursor.execute('''
            SELECT * FROM harvest_sessions 
            WHERE session_status != 'completed'
            ORDER BY last_activity DESC 
            LIMIT 1
        ''')
        
        row = cursor.fetchone()
        conn.close()
        
        if row:
            return {
                "session_id": row[1],
                "session_name": row[2],
                "started_at": row[3],
                "last_activity": row[4],
                "total_endpoints": row[6],
                "processed_endpoints": row[7],
                "current_api": row[11],
                "current_endpoint": row[12]
            }
        
        return None
    
    def resume_session(self, session_id: str) -> Dict:
        """Resume a previous session"""
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        # Get session details
        cursor.execute('SELECT * FROM harvest_sessions WHERE session_id = ?', (session_id,))
        session = cursor.fetchone()
        
        if not session:
            conn.close()
            return {}
        
        # Get processed endpoints
        cursor.execute('''
            SELECT DISTINCT api_name, endpoint_path 
            FROM harvested_data 
            WHERE session_id = ?
        ''', (session_id,))
        
        processed = cursor.fetchall()
        processed_endpoints = {f"{row[0]}:{row[1]}" for row in processed}
        
        conn.close()
        
        self.current_session = session_id
        
        return {
            "session_id": session_id,
            "processed_endpoints": processed_endpoints,
            "total_endpoints": session[6],
            "processed_count": session[7],
            "current_api": session[11],
            "current_endpoint": session[12]
        }
    
    def update_session_progress(self, session_id: str, current_api: str, 
                              current_endpoint: str, processed_count: int):
        """Update session progress"""
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        cursor.execute('''
            UPDATE harvest_sessions 
            SET last_activity = ?, current_api = ?, current_endpoint = ?, processed_endpoints = ?
            WHERE session_id = ?
        ''', (datetime.now(), current_api, current_endpoint, processed_count, session_id))
        
        conn.commit()
        conn.close()
    
    def complete_session(self, session_id: str):
        """Mark session as completed"""
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        cursor.execute('''
            UPDATE harvest_sessions 
            SET completed_at = ?, session_status = 'completed'
            WHERE session_id = ?
        ''', (datetime.now(), session_id))
        
        conn.commit()
        conn.close()

class UltimateDataHarvester:
    """Ultimate data harvester with resumption and intelligent storage"""
    
    def __init__(self):
        self.session = requests.Session()
        self.session.headers.update({
            'User-Agent': 'Ultimate-Data-Harvester/2.0 (Comprehensive Research Purpose)'
        })
        self.discoverer = DeepEndpointDiscoverer()
        self.session_manager = SessionManager()
        init_enhanced_database()
        
    def get_all_discovered_endpoints(self, api_name: str = None) -> List[Dict]:
        """Get all discovered endpoints from database"""
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        if api_name:
            cursor.execute('''
                SELECT * FROM discovered_endpoints 
                WHERE api_name = ? AND is_active = 1
                ORDER BY api_name, depth_level, endpoint_path
            ''', (api_name,))
        else:
            cursor.execute('''
                SELECT * FROM discovered_endpoints 
                WHERE is_active = 1
                ORDER BY api_name, depth_level, endpoint_path
            ''')
        
        columns = [desc[0] for desc in cursor.description]
        endpoints = [dict(zip(columns, row)) for row in cursor.fetchall()]
        
        conn.close()
        return endpoints
    
    def harvest_with_resumption(self, selected_apis: List[str], session_id: str = None, 
                               progress_callback=None) -> Dict:
        """Harvest data with session resumption capability"""
        # Resume existing session or create new one
        if session_id:
            session_info = self.session_manager.resume_session(session_id)
            processed_endpoints = session_info.get("processed_endpoints", set())
        else:
            session_id = self.session_manager.create_session()
            processed_endpoints = set()
        
        results = {
            "session_id": session_id,
            "total_endpoints": 0,
            "processed_endpoints": 0,
            "successful_fetches": 0,
            "failed_fetches": 0,
            "total_records": 0,
            "skipped_endpoints": 0,
            "errors": []
        }
        
        # Get all endpoints for selected APIs
        all_endpoints = []
        for api_name in selected_apis:
            endpoints = self.get_all_discovered_endpoints(api_name)
            all_endpoints.extend(endpoints)
        
        results["total_endpoints"] = len(all_endpoints)
        
        if progress_callback:
            progress_callback(f"πŸš€ Starting harvest from {len(all_endpoints)} endpoints (Session: {session_id})")
        
        # Process endpoints with resumption
        for i, endpoint in enumerate(all_endpoints):
            endpoint_key = f"{endpoint['api_name']}:{endpoint['endpoint_path']}"
            
            # Skip if already processed in this session
            if endpoint_key in processed_endpoints:
                results["skipped_endpoints"] += 1
                if progress_callback:
                    progress_callback(f"⏭️ Skipping already processed: {endpoint_key}")
                continue
            
            # Update session progress
            self.session_manager.update_session_progress(
                session_id, endpoint['api_name'], endpoint['endpoint_path'], 
                results["processed_endpoints"]
            )
            
            # Fetch data from endpoint
            fetch_result = self._fetch_endpoint_data(endpoint, session_id)
            
            if fetch_result["status"] == "success":
                results["successful_fetches"] += 1
                results["total_records"] += fetch_result.get("record_count", 0)
            else:
                results["failed_fetches"] += 1
                results["errors"].append(f"{endpoint_key}: {fetch_result.get('error', 'Unknown error')}")
            
            results["processed_endpoints"] += 1
            processed_endpoints.add(endpoint_key)
            
            if progress_callback:
                progress_callback(f"πŸ“Š Processed {results['processed_endpoints']}/{results['total_endpoints']}: {endpoint_key}")
            
            # Apply rate limiting
            config = DEEP_API_CONFIG.get(endpoint['api_name'], {})
            self._apply_rate_limit(config)
        
        # Complete session
        self.session_manager.complete_session(session_id)
        
        if progress_callback:
            progress_callback(f"βœ… Harvest completed! Session: {session_id}")
        
        return results
    
    def _fetch_endpoint_data(self, endpoint: Dict, session_id: str) -> Dict:
        """Fetch data from a single endpoint with intelligent storage"""
        start_time = time.time()
        
        try:
            api_name = endpoint["api_name"]
            config = DEEP_API_CONFIG.get(api_name, {})
            
            # Setup headers with authentication
            headers = {}
            auth = config.get("auth")
            if auth and auth.get("type") == "Bearer":
                headers["Authorization"] = f"Bearer {auth['token']}"
            
            # Make request
            response = self.session.get(endpoint["full_url"], headers=headers, timeout=30)
            response.raise_for_status()
            
            # Parse response
            content_type = response.headers.get('Content-Type', '')
            if 'application/json' in content_type:
                data = response.json()
            elif 'application/xml' in content_type or 'text/xml' in content_type:
                data = self._xml_to_dict(response.text)
            else:
                data = {"raw_response": response.text}
            
            # Process and clean data
            processed_data = self._process_api_response(api_name, data)
            
            # Calculate metrics
            fetch_duration = int((time.time() - start_time) * 1000)
            record_count = len(processed_data) if isinstance(processed_data, list) else 1
            data_size = len(json.dumps(processed_data, default=str).encode('utf-8'))
            
            # Save to database with intelligent categorization
            self._save_harvested_data(
                api_name, endpoint["endpoint_path"], processed_data, 
                session_id, fetch_duration, record_count, data_size
            )
            
            return {
                "status": "success",
                "record_count": record_count,
                "data_size": data_size,
                "fetch_duration": fetch_duration
            }
            
        except Exception as e:
            fetch_duration = int((time.time() - start_time) * 1000)
            
            # Save error information
            self._save_harvested_data(
                endpoint["api_name"], endpoint["endpoint_path"], {}, 
                session_id, fetch_duration, 0, 0, "error", str(e)
            )
            
            return {
                "status": "error",
                "error": str(e),
                "fetch_duration": fetch_duration
            }
    
    def _process_api_response(self, api_name: str, data: Any) -> Any:
        """Process API response with intelligent data extraction"""
        if api_name == "Skolverket":
            if isinstance(data, dict):
                if "_embedded" in data:
                    for key, value in data["_embedded"].items():
                        if isinstance(value, list):
                            return value
                if "Skolenheter" in data:
                    return data["Skolenheter"]
            return data
        
        elif api_name == "SCB":
            if isinstance(data, dict):
                return data.get("data", data.get("variables", data))
        
        elif api_name == "Kolada":
            if isinstance(data, dict):
                return data.get("values", data)
        
        elif api_name == "Eurostat":
            if isinstance(data, dict):
                return data.get("value", data.get("data", data))
        
        elif api_name == "WHO":
            if isinstance(data, dict):
                return data.get("value", data.get("fact", data))
        
        elif api_name == "OECD":
            if isinstance(data, dict):
                if "dataSets" in data:
                    return data["dataSets"]
                return data.get("data", data)
        
        elif api_name == "WorldBank":
            if isinstance(data, list) and len(data) > 1:
                return data[1] if data[1] else data[0]
            return data
        
        elif api_name == "Riksbanken":
            if isinstance(data, dict):
                return data.get("observations", data.get("data", data))
        
        elif api_name == "Swecris":
            if isinstance(data, dict):
                return data.get("items", data.get("projects", data))
        
        elif api_name == "CSN":
            if isinstance(data, dict):
                return data.get("data", data.get("variables", data))
        
        return data
    
    def _save_harvested_data(self, api_name: str, endpoint_path: str, data: Any, 
                           session_id: str, fetch_duration: int, record_count: int, 
                           data_size: int, status: str = "success", error_message: str = None):
        """Save harvested data with optimized storage and AI-enhanced analysis"""
        import gzip
        
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        # Create data hash for deduplication
        data_str = json.dumps(data, sort_keys=True, default=str)
        data_hash = hashlib.sha256(data_str.encode()).hexdigest()
        
        # Check if this data already exists
        cursor.execute('SELECT id FROM harvested_data WHERE data_hash = ?', (data_hash,))
        if cursor.fetchone():
            # Update access count and last accessed time
            cursor.execute('''
                UPDATE harvested_data 
                SET access_count = access_count + 1, last_accessed = CURRENT_TIMESTAMP
                WHERE data_hash = ?
            ''', (data_hash,))
            conn.commit()
            conn.close()
            return
        
        # AI Quality Assessment
        quality_assessment = {}
        if ai_quality_assessor and status == "success":
            quality_assessment = ai_quality_assessor.assess_data_quality(data, api_name)
        
        # Semantic Similarity Analysis
        similar_datasets = []
        if semantic_analyzer and status == "success":
            similar_datasets = semantic_analyzer.find_similar_datasets(data, api_name)
        
        # API Health Monitoring
        health_info = {}
        if health_monitor:
            success_rate = 1.0 if status == "success" else 0.0
            health_info = health_monitor.monitor_api_health(
                api_name, fetch_duration, success_rate, data_size
            )
        
        # Determine data format
        data_format = self._detect_data_format(data)
        
        # Compress data if it's large
        raw_data_compressed = None
        processed_data_compressed = None
        raw_data = None
        processed_data = None
        
        if data_size > 1024:  # Compress if larger than 1KB
            try:
                raw_data_compressed = gzip.compress(data_str.encode('utf-8'))
                processed_data_compressed = gzip.compress(json.dumps(data, default=str).encode('utf-8'))
            except:
                # Fallback to uncompressed storage
                raw_data = data_str
                processed_data = json.dumps(data, default=str)
        else:
            raw_data = data_str
            processed_data = json.dumps(data, default=str)
        
        try:
            cursor.execute('''
                INSERT INTO harvested_data 
                (api_name, endpoint_path, data_hash, raw_data_compressed, processed_data_compressed,
                 raw_data, processed_data, raw_data_size, processed_data_size,
                 record_count, data_size_bytes, fetch_duration_ms, status, 
                 error_message, session_id, quality_score, health_score, similar_datasets,
                 data_format, access_count)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            ''', (
                api_name, endpoint_path, data_hash, raw_data_compressed, processed_data_compressed,
                raw_data, processed_data, len(data_str), len(json.dumps(data, default=str)),
                record_count, data_size, fetch_duration, status, error_message, session_id,
                quality_assessment.get('ai_quality_score', 0.0),
                health_info.get('health_score', 0.0),
                json.dumps(similar_datasets[:3], default=str),
                data_format, 1
            ))
            
            # Log API performance
            cursor.execute('''
                INSERT INTO api_performance_log 
                (api_name, endpoint_path, response_time_ms, response_size_bytes, 
                 http_status_code, success, error_type)
                VALUES (?, ?, ?, ?, ?, ?, ?)
            ''', (
                api_name, endpoint_path, fetch_duration, data_size,
                200 if status == "success" else 500,
                status == "success",
                error_message if status != "success" else None
            ))
            
            conn.commit()
            
            # Display AI insights if available
            if quality_assessment and st.session_state.get('show_ai_insights', True):
                self._display_ai_insights(api_name, quality_assessment, health_info, similar_datasets)
                
        except sqlite3.OperationalError as e:
            # Handle database schema updates
            if "no such column" in str(e):
                self._upgrade_database_schema()
                # Retry with basic data structure
                cursor.execute('''
                    INSERT OR REPLACE INTO harvested_data 
                    (api_name, endpoint_path, data_hash, raw_data, processed_data, 
                     record_count, data_size_bytes, fetch_duration_ms, status, 
                     error_message, session_id)
                    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                ''', (
                    api_name, endpoint_path, data_hash, raw_data or data_str, 
                    processed_data or json.dumps(data, default=str), record_count, data_size, 
                    fetch_duration, status, error_message, session_id
                ))
                conn.commit()
        finally:
            conn.close()
    
    def _detect_data_format(self, data: Any) -> str:
        """Detect the format of the data"""
        if isinstance(data, dict):
            if "_embedded" in data or "_links" in data:
                return "HAL+JSON"
            elif "dataSets" in data or "structure" in data:
                return "SDMX-JSON"
            else:
                return "JSON"
        elif isinstance(data, list):
            return "JSON-Array"
        elif isinstance(data, str):
            if data.strip().startswith('<'):
                return "XML"
            else:
                return "Text"
        else:
            return "Unknown"
    
    def _display_ai_insights(self, api_name: str, quality_assessment: Dict, 
                           health_info: Dict, similar_datasets: List[Dict]):
        """Display AI-powered insights in real-time"""
        if quality_assessment:
            with st.expander(f"πŸ€– AI Insights for {api_name}", expanded=False):
                col1, col2, col3 = st.columns(3)
                
                with col1:
                    st.metric("Quality Grade", quality_assessment.get('overall_grade', 'N/A'))
                    st.metric("Completeness", f"{quality_assessment.get('completeness_score', 0):.2f}")
                
                with col2:
                    if health_info:
                        st.metric("Health Status", health_info.get('status', 'Unknown'))
                        st.metric("Performance Trend", health_info.get('trend', 'N/A'))
                
                with col3:
                    st.metric("Anomalies", quality_assessment.get('anomaly_count', 0))
                    if similar_datasets:
                        st.metric("Similar Datasets", len(similar_datasets))
                
                # Recommendations
                recommendations = quality_assessment.get('recommendations', [])
                if recommendations:
                    st.write("**🎯 Recommendations:**")
                    for rec in recommendations[:3]:
                        st.write(f"β€’ {rec}")
                
                # Similar datasets
                if similar_datasets:
                    st.write("**πŸ” Similar Datasets Found:**")
                    for sim in similar_datasets[:2]:
                        st.write(f"β€’ {sim['dataset']} (similarity: {sim['similarity']:.2f})")
    
    def _upgrade_database_schema(self):
        """Upgrade database schema to include AI columns"""
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        try:
            # Add AI enhancement columns
            cursor.execute('ALTER TABLE harvested_data ADD COLUMN quality_score REAL DEFAULT 0.0')
            cursor.execute('ALTER TABLE harvested_data ADD COLUMN health_score REAL DEFAULT 0.0') 
            cursor.execute('ALTER TABLE harvested_data ADD COLUMN similar_datasets TEXT DEFAULT "[]"')
            conn.commit()
        except sqlite3.OperationalError:
            pass  # Columns already exist
        finally:
            conn.close()
    
    def _xml_to_dict(self, xml_text: str) -> Dict:
        """Convert XML to dictionary"""
        try:
            root = ET.fromstring(xml_text)
            return self._element_to_dict(root)
        except ET.ParseError:
            return {"raw_xml": xml_text}
    
    def _element_to_dict(self, element) -> Dict:
        """Convert XML element to dictionary"""
        result = {}
        
        if element.attrib:
            result.update(element.attrib)
        
        if element.text and element.text.strip():
            if len(element) == 0:
                return element.text.strip()
            result['text'] = element.text.strip()
        
        for child in element:
            child_data = self._element_to_dict(child)
            if child.tag in result:
                if not isinstance(result[child.tag], list):
                    result[child.tag] = [result[child.tag]]
                result[child.tag].append(child_data)
            else:
                result[child.tag] = child_data
        
        return result
    
    def _apply_rate_limit(self, config: Dict):
        """Apply rate limiting"""
        rate_limit = config.get("rate_limit")
        if rate_limit:
            sleep_time = rate_limit["per_seconds"] / rate_limit["requests"]
            time.sleep(sleep_time)
    
    def get_database_stats(self) -> Dict:
        """Get comprehensive database statistics"""
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        # Endpoint statistics
        cursor.execute('SELECT COUNT(*) FROM discovered_endpoints')
        total_endpoints = cursor.fetchone()[0]
        
        cursor.execute('SELECT COUNT(DISTINCT api_name) FROM discovered_endpoints')
        active_apis = cursor.fetchone()[0]
        
        # Data statistics
        cursor.execute('SELECT COUNT(*), SUM(record_count), SUM(data_size_bytes) FROM harvested_data WHERE status = "success"')
        data_stats = cursor.fetchone()
        
        # Session statistics
        cursor.execute('SELECT COUNT(*) FROM harvest_sessions')
        total_sessions = cursor.fetchone()[0]
        
        conn.close()
        
        return {
            "total_endpoints": total_endpoints,
            "active_apis": active_apis,
            "successful_fetches": data_stats[0] or 0,
            "total_records": data_stats[1] or 0,
            "total_data_size": data_stats[2] or 0,
            "total_sessions": total_sessions
        }

# Initialize simplified components
if 'harvester' not in st.session_state:
    st.session_state.harvester = SimplifiedDataHarvester()

if 'last_results' not in st.session_state:
    st.session_state.last_results = None

# Enhanced Header
st.markdown("""
<div class="title-container">
    <h1 style="font-size: 2.5rem; margin: 0; color: #2c3e50;">
        πŸš€ Ultimate Data Harvester
    </h1>
    <p style="font-size: 1.1rem; margin: 0.5rem 0 0 0; color: #34495e;">
        AI-Enhanced Deep Discovery β€’ Session Resumption β€’ Intelligent Storage
    </p>
    <p style="font-size: 0.95rem; margin: 0.3rem 0 0 0; color: #7f8c8d;">
        Comprehensive data collection from 10 international APIs with advanced analytics
    </p>
    <div style="margin-top: 1rem;">
        <span style="background: #ecf0f1; color: #2c3e50; padding: 0.3rem 0.8rem; border-radius: 15px; margin: 0 0.3rem; font-size: 0.9rem;">πŸ” Recursive Discovery</span>
        <span style="background: #ecf0f1; color: #2c3e50; padding: 0.3rem 0.8rem; border-radius: 15px; margin: 0 0.3rem; font-size: 0.9rem;">🎯 Auto-Resume</span>
        <span style="background: #ecf0f1; color: #2c3e50; padding: 0.3rem 0.8rem; border-radius: 15px; margin: 0 0.3rem; font-size: 0.9rem;">πŸ’Ύ Smart Storage</span>
    </div>
</div>
""", unsafe_allow_html=True)

# Display ML status prominently
if ML_AVAILABLE:
    st.success("πŸ€– **AI Enhanced Mode Active** - Advanced quality assessment and semantic analysis enabled")
else:
    st.info("πŸ“Š **Standard Mode** - Basic functionality available. Install transformers and sentence-transformers for AI features.")

# Session Management Section
st.markdown("### 🎯 Session Management")

col1, col2, col3 = st.columns([2, 1, 1])

with col1:
    if st.session_state.last_session_info:
        last_session = st.session_state.last_session_info
        st.markdown(f"""
        <div class="discovery-progress">
            <strong>πŸ“‹ Last Session Available</strong><br>
            <strong>Name:</strong> {last_session['session_name']}<br>
            <strong>Progress:</strong> {last_session['processed_endpoints']}/{last_session['total_endpoints']} endpoints<br>
            <strong>Last API:</strong> {last_session.get('current_api', 'N/A')}<br>
            <strong>Started:</strong> {last_session['started_at'][:19]}
        </div>
        """, unsafe_allow_html=True)
    else:
        st.info("No previous session found. Ready to start fresh!")

with col2:
    if st.button("πŸ”„ Resume Last Session", disabled=not st.session_state.last_session_info, use_container_width=True):
        st.session_state.current_session = st.session_state.last_session_info['session_id']
        st.success(f"Resumed session: {st.session_state.last_session_info['session_name']}")

with col3:
    if st.button("πŸ†• Start New Session", use_container_width=True):
        session_id = st.session_state.harvester.session_manager.create_session()
        st.session_state.current_session = session_id
        st.session_state.last_session_info = None
        st.success(f"New session created: {session_id}")

# Database Statistics
st.markdown("### πŸ“Š Database Overview")

stats = st.session_state.harvester.get_database_stats()

col1, col2, col3, col4, col5 = st.columns(5)

with col1:
    st.markdown(f"""
    <div class="metric-card">
        <div style="font-size: 0.9rem; opacity: 0.8;">🎯 Discovered Endpoints</div>
        <div style="font-size: 1.8rem; font-weight: bold;">{stats['total_endpoints']:,}</div>
    </div>
    """, unsafe_allow_html=True)

with col2:
    st.markdown(f"""
    <div class="metric-card">
        <div style="font-size: 0.9rem; opacity: 0.8;">🌍 Active APIs</div>
        <div style="font-size: 1.8rem; font-weight: bold;">{stats['active_apis']}</div>
    </div>
    """, unsafe_allow_html=True)

with col3:
    st.markdown(f"""
    <div class="metric-card">
        <div style="font-size: 0.9rem; opacity: 0.8;">βœ… Successful Fetches</div>
        <div style="font-size: 1.8rem; font-weight: bold;">{stats['successful_fetches']:,}</div>
    </div>
    """, unsafe_allow_html=True)

with col4:
    st.markdown(f"""
    <div class="metric-card">
        <div style="font-size: 0.9rem; opacity: 0.8;">πŸ“Š Total Records</div>
        <div style="font-size: 1.8rem; font-weight: bold;">{stats['total_records']:,}</div>
    </div>
    """, unsafe_allow_html=True)

with col5:
    data_size_mb = stats['total_data_size'] / 1024 / 1024 if stats['total_data_size'] else 0
    st.markdown(f"""
    <div class="metric-card">
        <div style="font-size: 0.9rem; opacity: 0.8;">πŸ’Ύ Data Size</div>
        <div style="font-size: 1.8rem; font-weight: bold;">{data_size_mb:.1f} MB</div>
    </div>
    """, unsafe_allow_html=True)

# Main Operations
st.markdown("### πŸš€ Operations")

tab1, tab2, tab3 = st.tabs(["πŸ” Deep Discovery", "πŸ“Š Data Harvesting", "πŸ“ˆ Analytics"])

with tab1:
    st.markdown("**πŸ€– AI-Enhanced Deep Discovery - Find all endpoints with intelligent analysis**")
    
    # AI Settings
    col1, col2 = st.columns(2)
    with col1:
        enable_ai_insights = st.checkbox("πŸ€– Enable AI Quality Assessment", value=True, key="enable_ai")
    with col2:
        show_similarity = st.checkbox("πŸ” Show Semantic Similarity", value=True, key="enable_similarity")
    
    st.session_state['show_ai_insights'] = enable_ai_insights
    
    # API Selection for Discovery
    selected_apis_discovery = st.multiselect(
        "Select APIs for deep endpoint discovery:",
        list(DEEP_API_CONFIG.keys()),
        default=[],
        key="discovery_apis"
    )
    
    col1, col2 = st.columns([3, 1])
    
    with col1:
        if st.button("πŸ” Start Deep Discovery", disabled=not selected_apis_discovery, use_container_width=True):
            progress_container = st.container()
            status_container = st.empty()
            
            with progress_container:
                progress_bar = st.progress(0)
                
                for i, api_name in enumerate(selected_apis_discovery):
                    st.session_state.discovery_status[api_name] = "discovering"
                    
                    def progress_callback(message):
                        status_container.text(f"πŸ” {api_name}: {message}")
                    
                    # Run deep discovery
                    discovered = st.session_state.harvester.discoverer.discover_all_endpoints(
                        api_name, progress_callback
                    )
                    
                    st.session_state.discovery_status[api_name] = "completed"
                    progress_bar.progress((i + 1) / len(selected_apis_discovery))
                    
                    # Show results
                    st.success(f"βœ… {api_name}: {len(discovered)} endpoints discovered")
                
                status_container.text("πŸŽ‰ Deep discovery completed for all selected APIs!")
    
    with col2:
        if st.button("πŸ“‹ View All Endpoints", use_container_width=True):
            endpoints = st.session_state.harvester.get_all_discovered_endpoints()
            
            if endpoints:
                df_endpoints = pd.DataFrame(endpoints)
                st.dataframe(
                    df_endpoints[['api_name', 'endpoint_path', 'discovery_method', 'depth_level', 'last_checked']], 
                    use_container_width=True
                )
            else:
                st.info("No endpoints discovered yet. Run discovery first!")

with tab2:
    st.markdown("**Harvest data from all discovered endpoints with session resumption**")
    
    # API Selection for Harvesting
    selected_apis_harvest = st.multiselect(
        "Select APIs for data harvesting:",
        list(DEEP_API_CONFIG.keys()),
        default=list(DEEP_API_CONFIG.keys()),
        key="harvest_apis"
    )
    
    col1, col2 = st.columns([2, 1])
    
    with col1:
        if st.button("πŸš€ Start Ultimate Harvest", disabled=not selected_apis_harvest, use_container_width=True):
            progress_container = st.container()
            status_container = st.empty()
            results_container = st.container()
            
            with progress_container:
                progress_bar = st.progress(0)
                
                def progress_callback(message):
                    status_container.text(message)
                
                # Start harvest with resumption
                results = st.session_state.harvester.harvest_with_resumption(
                    selected_apis_harvest, 
                    st.session_state.current_session,
                    progress_callback
                )
                
                # Update progress bar to completion
                progress_bar.progress(1.0)
                
                # Show results
                with results_container:
                    st.success("πŸŽ‰ Ultimate harvest completed!")
                    
                    col_a, col_b, col_c, col_d = st.columns(4)
                    
                    with col_a:
                        st.metric("βœ… Successful", results['successful_fetches'])
                    with col_b:
                        st.metric("❌ Failed", results['failed_fetches'])
                    with col_c:
                        st.metric("πŸ“Š Records", f"{results['total_records']:,}")
                    with col_d:
                        st.metric("⏭️ Skipped", results['skipped_endpoints'])
    
    with col2:
        # Export options
        st.markdown("**Export Data**")
        
        if st.button("πŸ“„ Export Database (JSON)", use_container_width=True):
            conn = sqlite3.connect(DB_PATH)
            
            # Export all tables
            tables = ['discovered_endpoints', 'harvested_data', 'harvest_sessions']
            export_data = {}
            
            for table in tables:
                df = pd.read_sql_query(f"SELECT * FROM {table}", conn)
                export_data[table] = df.to_dict('records')
            
            conn.close()
            
            # Create download
            export_json = json.dumps(export_data, default=str, indent=2)
            st.download_button(
                "πŸ’Ύ Download Complete Database",
                data=export_json,
                file_name=f"ultimate_harvest_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
                mime="application/json",
                use_container_width=True
            )

with tab3:
    st.markdown("**Analytics and Insights from Harvested Data**")
    
    # Get harvested data for analytics
    conn = sqlite3.connect(DB_PATH)
    
    try:
        df_data = pd.read_sql_query('''
            SELECT api_name, COUNT(*) as fetches, SUM(record_count) as total_records,
                   AVG(fetch_duration_ms) as avg_duration, SUM(data_size_bytes) as total_size
            FROM harvested_data 
            WHERE status = 'success'
            GROUP BY api_name
        ''', conn)
        
        if not df_data.empty:
            col1, col2 = st.columns(2)
            
            with col1:
                # Records by API
                fig_records = px.bar(
                    df_data, 
                    x='api_name', 
                    y='total_records',
                    title="πŸ“Š Records Harvested by API",
                    color='total_records',
                    color_continuous_scale='viridis'
                )
                fig_records.update_layout(
                    paper_bgcolor="rgba(255,255,255,0.9)",
                    plot_bgcolor="rgba(255,255,255,0.9)",
                    font_color="#2c3e50"
                )
                st.plotly_chart(fig_records, use_container_width=True)
            
            with col2:
                # Data size by API
                df_data['size_mb'] = df_data['total_size'] / 1024 / 1024
                fig_size = px.pie(
                    df_data, 
                    values='size_mb', 
                    names='api_name',
                    title="πŸ’Ύ Data Size Distribution (MB)"
                )
                fig_size.update_layout(
                    paper_bgcolor="rgba(255,255,255,0.9)",
                    plot_bgcolor="rgba(255,255,255,0.9)",
                    font_color="#2c3e50"
                )
                st.plotly_chart(fig_size, use_container_width=True)
            
            # Performance metrics
            st.markdown("**⚑ Performance Metrics**")
            
            fig_perf = px.bar(
                df_data, 
                x='api_name', 
                y='avg_duration',
                title="⏱️ Average Fetch Duration by API (ms)",
                color='avg_duration',
                color_continuous_scale='plasma'
            )
            fig_perf.update_layout(
                paper_bgcolor="rgba(255,255,255,0.9)",
                plot_bgcolor="rgba(255,255,255,0.9)",
                font_color="#2c3e50"
            )
            st.plotly_chart(fig_perf, use_container_width=True)
        
        else:
            st.info("No data available for analytics. Start harvesting first!")
    
    finally:
        conn.close()

# Database Management Section
with st.expander("πŸ—„οΈ Database Management & Statistics", expanded=False):
    st.markdown("**Database Performance & Maintenance Tools**")
    
    col1, col2, col3, col4 = st.columns(4)
    
    with col1:
        if st.button("πŸ“Š Get Database Stats", use_container_width=True):
            with st.spinner("Analyzing database..."):
                stats = get_database_stats()
                
                st.markdown("**Database Statistics:**")
                for key, value in stats.items():
                    formatted_key = key.replace('_', ' ').title()
                    if 'size_mb' in key:
                        st.metric(formatted_key, f"{value} MB")
                    elif 'score' in key:
                        st.metric(formatted_key, f"{value:.3f}")
                    else:
                        st.metric(formatted_key, value)
    
    with col2:
        if st.button("πŸ”§ Optimize Database", use_container_width=True):
            with st.spinner("Optimizing database..."):
                success = optimize_database()
                if success:
                    st.success("βœ… Database optimized successfully!")
                else:
                    st.error("❌ Database optimization failed")
    
    with col3:
        if st.button("πŸ—œοΈ Compress Old Data", use_container_width=True):
            with st.spinner("Compressing old data..."):
                compressed_count = compress_old_data(days_old=7)  # Compress data older than 7 days
                if compressed_count > 0:
                    st.success(f"βœ… Compressed {compressed_count} old records")
                else:
                    st.info("ℹ️ No old data found to compress")
    
    with col4:
        if st.button("πŸ’Ύ Create Backup", use_container_width=True):
            with st.spinner("Creating backup..."):
                backup_path = backup_database()
                if backup_path:
                    st.success(f"βœ… Backup created: {backup_path}")
                    # Offer download
                    try:
                        with open(backup_path, 'rb') as f:
                            st.download_button(
                                label="⬇️ Download Backup",
                                data=f.read(),
                                file_name=backup_path,
                                mime="application/x-sqlite3"
                            )
                    except:
                        pass
                else:
                    st.error("❌ Backup creation failed")
    
    # Enhanced database insights
    st.markdown("---")
    
    try:
        conn = sqlite3.connect(DB_PATH)
        
        # Show recent activity summary
        col1, col2 = st.columns(2)
        
        with col1:
            st.markdown("**πŸ“ˆ Recent Activity (Last 24h)**")
            df_recent = pd.read_sql_query('''
                SELECT api_name, COUNT(*) as fetches, SUM(record_count) as records
                FROM harvested_data 
                WHERE fetch_timestamp > datetime('now', '-1 day')
                GROUP BY api_name
                ORDER BY fetches DESC
            ''', conn)
            
            if not df_recent.empty:
                st.dataframe(df_recent, use_container_width=True)
            else:
                st.info("No recent activity")
        
        with col2:
            st.markdown("**🎯 Data Quality Overview**")
            df_quality = pd.read_sql_query('''
                SELECT 
                    api_name,
                    ROUND(AVG(quality_score), 3) as avg_quality,
                    ROUND(AVG(health_score), 3) as avg_health,
                    COUNT(*) as total_records
                FROM harvested_data 
                WHERE status = 'success' AND quality_score > 0
                GROUP BY api_name
                ORDER BY avg_quality DESC
            ''', conn)
            
            if not df_quality.empty:
                st.dataframe(df_quality, use_container_width=True)
            else:
                st.info("No quality data available")
        
        conn.close()
        
    except Exception as e:
        st.error(f"Database error: {e}")
    
    # Storage efficiency metrics
    st.markdown("**πŸ’Ύ Storage Efficiency**")
    
    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        # Calculate compression ratios
        cursor.execute('''
            SELECT 
                COUNT(*) as total_records,
                COUNT(CASE WHEN raw_data_compressed IS NOT NULL THEN 1 END) as compressed_records,
                SUM(data_size_bytes) as total_original_size,
                SUM(CASE WHEN raw_data_compressed IS NOT NULL THEN raw_data_size ELSE data_size_bytes END) as effective_size
            FROM harvested_data
        ''')
        
        storage_stats = cursor.fetchone()
        
        if storage_stats and storage_stats[0] > 0:
            total_records, compressed_records, original_size, effective_size = storage_stats
            
            col1, col2, col3, col4 = st.columns(4)
            
            with col1:
                st.metric("Total Records", total_records)
            
            with col2:
                st.metric("Compressed Records", compressed_records)
            
            with col3:
                compression_ratio = 0
                if original_size and effective_size:
                    compression_ratio = (1 - effective_size / original_size) * 100
                st.metric("Compression Ratio", f"{compression_ratio:.1f}%")
            
            with col4:
                space_saved = (original_size - effective_size) if original_size and effective_size else 0
                space_saved_mb = space_saved / (1024 * 1024)
                st.metric("Space Saved", f"{space_saved_mb:.2f} MB")
        
        conn.close()
        
    except Exception as e:
        st.warning(f"Could not calculate storage metrics: {e}")

# AI Enhancement Panel
st.markdown("---")
with st.expander("πŸ€– AI Enhancement Status", expanded=False):
    col1, col2, col3 = st.columns(3)
    
    with col1:
        st.markdown("**🎯 Quality Assessment**")
        if ML_AVAILABLE and ai_quality_assessor and ai_quality_assessor.quality_model:
            st.success("βœ… Active - DistilBERT")
        else:
            st.error("❌ Not Available")
    
    with col2:
        st.markdown("**πŸ” Semantic Analysis**")
        if ML_AVAILABLE and semantic_analyzer and semantic_analyzer.embeddings_model:
            st.success("βœ… Active - MiniLM-L6-v2")
        else:
            st.error("❌ Not Available")
    
    with col3:
        st.markdown("**πŸ“Š Health Monitoring**")
        if health_monitor:
            st.success("βœ… Active - Isolation Forest")
        else:
            st.error("❌ Not Available")
    
    if ML_AVAILABLE:
        st.info("πŸ’‘ AI models are loaded and ready for enhanced data analysis!")
    else:
        st.warning("⚠️ Install ML libraries (transformers, sentence-transformers) for AI features")

# Footer
st.markdown("---")
st.markdown("""
<div style="text-align: center; padding: 1rem; background: rgba(255,255,255,0.8); border-radius: 10px; color: #2c3e50;">
    <p><strong>πŸš€ Ultimate Data Harvester with AI</strong> - Professional data collection platform</p>
    <p style="font-size: 0.9rem; color: #7f8c8d;">
        πŸ” Recursive endpoint discovery β€’ πŸ€– AI quality assessment β€’ 🎯 Session management β€’ πŸ’Ύ Smart database storage β€’ πŸ“Š Real-time analytics
    </p>
</div>
""", unsafe_allow_html=True)