File size: 29,540 Bytes
674fb4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useEffect } from 'react';
import { useAuth } from '../context/AuthContext';
import type { DriftReport, DocumentInfo } from '../types/api';
import { Database, GitMerge, Settings, Sparkles, Save, Info, Zap, AlertTriangle, Check, X, FileText } from 'lucide-react';

const API_BASE = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000/api';

const Ontology: React.FC = () => {
  const { token, logout } = useAuth();
  
  const [loading, setLoading] = useState(true);
  const [saving, setSaving] = useState(false);
  const [refining, setRefining] = useState(false);
  const [deduping, setDeduping] = useState(false);
  const [enriching, setEnriching] = useState(false);
  const [detectingDrift, setDetectingDrift] = useState(false);
  const [driftReports, setDriftReports] = useState<DriftReport[]>([]);
  
  const [version, setVersion] = useState('');
  const [entityTypes, setEntityTypes] = useState('');
  const [relationshipTypes, setRelationshipTypes] = useState('');
  const [properties, setProperties] = useState('');
  const [feedback, setFeedback] = useState('');
  
  // "Global" baseline so we can restore when switching back to global mode
  const [globalEntityTypes, setGlobalEntityTypes] = useState('');
  const [globalRelationshipTypes, setGlobalRelationshipTypes] = useState('');
  const [globalProperties, setGlobalProperties] = useState('');
  
  const [documents, setDocuments] = useState<DocumentInfo[]>([]);
  const [selectedDocId, setSelectedDocId] = useState<string>('');
  const [stats, setStats] = useState<any>(null);
  const [docSchemaLoading, setDocSchemaLoading] = useState(false);
  
  const [message, setMessage] = useState('');

  /* ── Fetch global ontology ─────────────────────────────────────── */
  const fetchOntology = async () => {
    setLoading(true);
    try {
      const res = await fetch(`${API_BASE}/ontology`, {
        headers: { Authorization: `Bearer ${token}` }
      });
      if (res.status === 401) { logout(); return; }
      if (res.ok) {
        const data = await res.json();
        setVersion(data.version || '1.0');
        const ent = data.entity_types?.join(', ') || '';
        const rel = data.relationship_types?.join(', ') || '';
        const props = JSON.stringify(data.properties || {}, null, 2);
        setEntityTypes(ent);
        setRelationshipTypes(rel);
        setProperties(props);
        // Save as global baseline
        setGlobalEntityTypes(ent);
        setGlobalRelationshipTypes(rel);
        setGlobalProperties(props);
      } else {
        setMessage('No active ontology found. Please upload documents first.');
      }
    } catch (err) {
      console.error(err);
      setMessage('FAILED TO LOAD ONTOLOGY API');
    } finally {
      setLoading(false);
    }
  };

  const fetchDocuments = async () => {
    try {
      const res = await fetch(`${API_BASE}/documents`, {
        headers: { Authorization: `Bearer ${token}` }
      });
      if (res.ok) {
        const data = await res.json();
        setDocuments(data.documents);
      }
    } catch (err) {
      console.error('Failed to fetch docs for dropdown', err);
    }
  };

  const fetchStats = async (docId: string) => {
    try {
      const url = new URL(`${API_BASE}/ontology/stats`);
      if (docId) url.searchParams.append('document_id', docId);
      const res = await fetch(url.toString(), {
        headers: { Authorization: `Bearer ${token}` }
      });
      if (res.ok) setStats(await res.json());
      else setStats(null);
    } catch { setStats(null); }
  };

  /* ── Fetch document-specific schema ─────────────────────────────── */
  const fetchDocSchema = async (docId: string) => {
    if (!docId) {
      // Restore global schema
      setEntityTypes(globalEntityTypes);
      setRelationshipTypes(globalRelationshipTypes);
      setProperties(globalProperties);
      return;
    }
    setDocSchemaLoading(true);
    try {
      const url = new URL(`${API_BASE}/ontology/stats`);
      url.searchParams.append('document_id', docId);
      const res = await fetch(url.toString(), {
        headers: { Authorization: `Bearer ${token}` }
      });
      if (res.ok) {
        const data = await res.json();
        // Populate editor with document-specific entity types and relationships
        const docEntityTypes = (data.entity_stats || [])
          .map((s: any) => s.type)
          .filter(Boolean)
          .join(', ');
        const docRelTypes = (data.relationship_stats || [])
          .map((s: any) => s.type)
          .filter(Boolean)
          .join(', ');
        
        setEntityTypes(docEntityTypes || globalEntityTypes);
        setRelationshipTypes(docRelTypes || globalRelationshipTypes);
        // Properties: keep global properties since per-doc isn't tracked separately
        setProperties(globalProperties);
      }
    } catch (err) {
      console.error('Failed to fetch doc schema', err);
    } finally {
      setDocSchemaLoading(false);
    }
  };

  useEffect(() => {
    fetchOntology();
    fetchDocuments();
    fetchStats('');
  }, [token]);

  useEffect(() => {
    fetchStats(selectedDocId);
    fetchDocSchema(selectedDocId);
  }, [selectedDocId]);

  const handleSave = async (e: React.FormEvent) => {
    e.preventDefault();
    setSaving(true);
    setMessage('');
    
    let parsedProps = {};
    try {
      parsedProps = JSON.parse(properties);
    } catch (e) {
      setMessage('ERROR: PROPERTIES MUST BE VALID JSON');
      setSaving(false);
      return;
    }

    try {
      const res = await fetch(`${API_BASE}/ontology`, {
        method: 'PUT',
        headers: { 
          'Content-Type': 'application/json',
          Authorization: `Bearer ${token}` 
        },
        body: JSON.stringify({
          entity_types: entityTypes.split(',').map(s => s.trim()).filter(Boolean),
          relationship_types: relationshipTypes.split(',').map(s => s.trim()).filter(Boolean),
          properties: parsedProps,
          approved: true
        })
      });
      
      if (res.status === 401) { logout(); return; }
      
      if (res.ok) {
        setMessage('ONTOLOGY SCHEMA UPDATED');
        fetchOntology();
        setSelectedDocId(''); // reset to global after save
      } else {
        setMessage('FAILED TO SAVE SCHEMA');
      }
    } catch (err) {
      console.error(err);
      setMessage('API ERROR DURING SAVE');
    } finally {
      setSaving(false);
    }
  };

  const handleRefine = async () => {
    setRefining(true);
    setMessage('ANALYZING GRAPH FOR UPGRADES... (THIS MAY TAKE 30s+)');
    try {
      const res = await fetch(`${API_BASE}/ontology/refine`, {
        method: 'POST',
        headers: { 
          'Content-Type': 'application/json',
          Authorization: `Bearer ${token}` 
        },
        body: JSON.stringify({ 
          feedback: feedback || undefined,
          document_id: selectedDocId || undefined
        })
      });
      
      if (res.status === 401) { logout(); return; }
      
      if (res.ok) {
        const data = await res.json();
        setMessage(`SUCCESS: ${data.changes}`);
        fetchOntology();
      } else {
        setMessage('FAILED TO REFINE SCHEMA');
      }
    } catch (err) {
      console.error(err);
      setMessage('API ERROR DURING REFINE');
    } finally {
      setRefining(false);
    }
  };

  const handleDeduplicate = async () => {
    if (!window.confirm("Run semantic merging? This cannot be undone.")) return;
    setDeduping(true);
    setMessage('SCANNING GRAPH FOR DUPLICATE ENTITIES... (THIS MAY TAKE AWHILE)');
    try {
      const res = await fetch(`${API_BASE}/entities/deduplicate`, {
        method: 'POST',
        headers: { Authorization: `Bearer ${token}` }
      });
      if (res.status === 401) { logout(); return; }
      if (res.ok) {
        const data = await res.json();
        setMessage(`DEDUPLICATION COMPLETE: Merged ${data.merged_count} entities.`);
      } else {
        setMessage('FAILED TO DEDUPLICATE ENTITIES');
      }
    } catch (err) {
      console.error(err);
      setMessage('API ERROR DURING DEDUPLICATION');
    } finally {
      setDeduping(false);
    }
  };

  const handleEnrichEntities = async () => {
    setEnriching(true);
    setMessage('GENERATING ENTITY PROFILES FROM GRAPH NEIGHBORHOODS...');
    try {
      const res = await fetch(`${API_BASE}/entities/enrich`, {
        method: 'POST',
        headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
        body: JSON.stringify({ batch_size: 20, min_connections: 1 })
      });
      if (res.status === 401) { logout(); return; }
      if (res.ok) {
        const data = await res.json();
        setMessage(`ENRICHMENT COMPLETE: ${data.message || `${data.enriched_count ?? '?'} entities profiled.`}`);
      } else {
        setMessage('FAILED TO ENRICH ENTITIES');
      }
    } catch {
      setMessage('API ERROR DURING ENRICHMENT');
    } finally {
      setEnriching(false);
    }
  };

  const handleDetectDrift = async () => {
    setDetectingDrift(true);
    setMessage('ANALYZING GRAPH DATA FOR SCHEMA DRIFT...');
    try {
      const res = await fetch(`${API_BASE}/ontology/drift/detect`, {
        method: 'POST',
        headers: { Authorization: `Bearer ${token}` }
      });
      if (res.status === 401) { logout(); return; }
      if (res.ok) {
        const data = await res.json();
        setMessage(`DRIFT REPORT CREATED: ID ${data.report_id || data.id || 'β€”'}`);
        fetchDriftReports();
      } else {
        setMessage('FAILED TO DETECT DRIFT');
      }
    } catch {
      setMessage('API ERROR DURING DRIFT DETECTION');
    } finally {
      setDetectingDrift(false);
    }
  };

  const fetchDriftReports = async () => {
    try {
      const res = await fetch(`${API_BASE}/ontology/drift`, {
        headers: { Authorization: `Bearer ${token}` }
      });
      if (res.ok) {
        const data = await res.json();
        setDriftReports(data.reports || []);
      }
    } catch {}
  };

  const handleDriftAction = async (id: string, action: 'approve' | 'reject') => {
    const res = await fetch(`${API_BASE}/ontology/drift/${id}/${action}`, {
      method: 'POST', headers: { Authorization: `Bearer ${token}` }
    });
    if (res.ok) {
      setDriftReports(d => d.filter(r => r.id !== id));
      setMessage(`Drift report ${action}d.`);
    }
  };

  const selectedDoc = documents.find(d => d.id === selectedDocId);

  return (
    <div className="container" style={{ animation: 'fadeIn 0.5s ease' }}>
      <div className="page-header flex-between">
        <div>
          <h1>ONTOLOGY MANAGEMENT</h1>
          <p className="mono-text">SCHEMA CONTROL &amp; GRAPH REFINEMENT</p>
        </div>
        <Database size={32} />
      </div>

      {/* Help info bar */}
      <div className="page-info-bar">
        <Info size={14}/>
        <span>
          <strong>ENTITY TYPES</strong> define what kinds of nodes exist in your graph.
          <strong> RELATIONSHIP TYPES</strong> define how they connect.
          Use <strong>LLM REFINEMENT</strong> to auto-suggest schema improvements from your data.
          Use <strong>DRIFT DETECTION</strong> to detect when new data doesn't fit the current schema.
          Use <strong>ENTITY ENRICHMENT</strong> to synthesize rich profiles for all graph nodes.
        </span>
      </div>

      <div className="ontology-layout">
        {/* Schema Editor */}
        <div className="card editor-card">
          <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: '1.5rem' }}>
            <h2 className="mono-text flex-center" style={{ gap: '0.5rem', margin: 0 }}>
              <Settings size={20}/> EDIT SCHEMA {version ? `v${version}` : ''}
            </h2>
            {selectedDocId && (
              <div style={{ display: 'flex', alignItems: 'center', gap: '0.4rem', background: '#000', color: '#fff', padding: '3px 10px', fontFamily: 'var(--font-mono)', fontSize: '0.68rem', fontWeight: 700, letterSpacing: '0.5px' }}>
                <FileText size={11} />
                DOC SCOPE: {selectedDoc?.filename?.slice(0, 22) ?? selectedDocId.slice(0, 12)}…
              </div>
            )}
          </div>

          {/* Document selector in editor header */}
          <div style={{ marginBottom: '1.25rem', padding: '0.75rem', background: '#f5f5f5', border: '1.5px solid #e5e5e5' }}>
            <label className="control-label" style={{ display: 'block', marginBottom: '0.4rem' }}>
              POPULATE FROM DOCUMENT
            </label>
            <select
              className="mono-text doc-dropdown"
              value={selectedDocId}
              onChange={(e) => setSelectedDocId(e.target.value)}
              style={{ width: '100%' }}
            >
              <option value="">🌐 GLOBAL β€” Full Ontology Schema</option>
              {documents.map(doc => (
                <option key={doc.id} value={doc.id}>πŸ“„ {doc.filename}</option>
              ))}
            </select>
            {docSchemaLoading && (
              <div style={{ fontFamily: 'var(--font-mono)', fontSize: '0.72rem', color: 'var(--muted-color)', marginTop: '0.4rem' }}>
                ↻ Loading document schema…
              </div>
            )}
            {selectedDocId && !docSchemaLoading && (
              <div style={{ fontFamily: 'var(--font-mono)', fontSize: '0.68rem', color: '#16a34a', marginTop: '0.4rem', display: 'flex', alignItems: 'center', gap: '0.3rem' }}>
                <Check size={11}/> Schema populated from "{selectedDoc?.filename ?? selectedDocId}"
              </div>
            )}
          </div>
          
          {loading ? (
            <div className="mono-text" style={{ padding: '2rem', textAlign: 'center' }}>LOADING SCHEMA...</div>
          ) : (
            <form onSubmit={handleSave} className="schema-form">
              <div className="form-group">
                <label className="mono-text">ENTITY TYPES (COMMA-SEPARATED)</label>
                <textarea 
                  value={entityTypes} 
                  onChange={(e) => setEntityTypes(e.target.value)}
                  className="mono-text"
                  rows={3}
                  placeholder="Person, Organization, Location, Event…"
                />
              </div>

              <div className="form-group">
                <label className="mono-text">RELATIONSHIP TYPES (COMMA-SEPARATED)</label>
                <textarea 
                  value={relationshipTypes} 
                  onChange={(e) => setRelationshipTypes(e.target.value)}
                  className="mono-text"
                  rows={3}
                  placeholder="WORKS_FOR, LOCATED_IN, RELATED_TO…"
                />
              </div>

              <div className="form-group">
                <label className="mono-text">PROPERTIES BINDING (JSON FORMAT)</label>
                <textarea 
                  value={properties} 
                  onChange={(e) => setProperties(e.target.value)}
                  className="mono-text dict-editor"
                  rows={8}
                />
              </div>

              <button 
                type="submit" 
                className="app-btn full-width" 
                disabled={saving}
                style={{ display: 'flex', justifyContent: 'center', gap: '8px' }}
              >
                <Save size={18} /> {saving ? 'SAVING...' : 'COMMIT SCHEMA CHANGES'}
              </button>
            </form>
          )}

        </div>

        {/* AI Tools */}
        <div className="tools-card-container">
          <div className="card tools-card">
            <h2 className="mono-text flex-center" style={{ gap: '0.5rem' }}>
              <Sparkles size={20}/> LLM REFINEMENT
            </h2>
            <p style={{ marginTop: '1rem', marginBottom: '0.75rem', fontSize: '0.88rem', lineHeight: 1.6 }}>
              Use the LLM Agent to scan existing document chunks and automatically suggest expansions or restructuring to the current ontology schema.
            </p>

            <div className="refine-info-box">
              <Info size={13} />
              <span>
                <strong>Global mode:</strong> samples random chunks from all documents.<br/>
                <strong>Targeted mode:</strong> only scans chunks from the selected document β€” useful for domain-specific refinement.
              </span>
            </div>

            <label className="control-label" style={{ display: 'block', marginBottom: '0.4rem', marginTop: '1rem' }}>REFINEMENT SCOPE</label>
            <select 
              className="mono-text doc-dropdown" 
              value={selectedDocId} 
              onChange={(e) => setSelectedDocId(e.target.value)}
              style={{ width: '100%', marginBottom: '1rem' }}
            >
              <option value="">🌐 GLOBAL β€” ALL DOCUMENTS (RANDOM CHUNKS)</option>
              {documents.map(doc => (
                <option key={doc.id} value={doc.id}>πŸ“„ TARGET: {doc.filename}</option>
              ))}
            </select>
            <label className="control-label" style={{ display: 'block', marginBottom: '0.4rem' }}>OPTIONAL CRITERIA</label>
            <textarea 
              placeholder="e.g., 'Focus heavily on extracting medical symptoms and treatment names'"
              value={feedback}
              onChange={(e) => setFeedback(e.target.value)}
              className="mono-text"
              rows={3}
              style={{ width: '100%', marginBottom: '1rem' }}
            />
            <button 
              onClick={handleRefine}
              className="app-btn outline-btn full-width" 
              disabled={refining || loading}
              style={{ display: 'flex', justifyContent: 'center', gap: '8px' }}
            >
              <Sparkles size={18} /> {refining ? 'ANALYZING GRAPH...' : 'REFINE SCHEMA'}
            </button>
          </div>

          <div className="card tools-card" style={{ marginTop: '2rem' }}>
            <h2 className="mono-text flex-center" style={{ gap: '0.5rem' }}>
              <GitMerge size={20}/> IDENTITY RESOLUTION
            </h2>
            <p style={{ marginTop: '1rem', marginBottom: '1rem', fontSize: '0.9rem', lineHeight: 1.5 }}>
              Scan the entire Knowledge Graph and use Semantic Embedding comparisons to detect and permanently merge duplicated entities.
            </p>
            <button
              onClick={handleDeduplicate}
              className="app-btn outline-btn full-width"
              disabled={deduping || loading}
              style={{ display: 'flex', justifyContent: 'center', gap: '8px' }}
            >
              <GitMerge size={18} /> {deduping ? 'MERGING...' : 'DEDUPLICATE ENTITIES'}
            </button>
          </div>

          {/* Entity Enrichment */}
          <div className="card tools-card" style={{ marginTop: '2rem' }}>
            <h2 className="mono-text flex-center" style={{ gap: '0.5rem' }}>
              <Zap size={20}/> ENTITY ENRICHMENT
            </h2>
            <p style={{ marginTop: '1rem', marginBottom: '1rem', fontSize: '0.9rem', lineHeight: 1.5 }}>
              Generate rich LLM-synthesized profiles for all eligible entities using their graph neighborhood context. Profiles power the Entity Chat feature.
            </p>
            <button
              onClick={handleEnrichEntities}
              className="app-btn outline-btn full-width"
              disabled={enriching || loading}
              style={{ display: 'flex', justifyContent: 'center', gap: '8px' }}
            >
              <Zap size={18} /> {enriching ? 'ENRICHING...' : 'ENRICH ALL ENTITIES'}
            </button>
          </div>

          {/* Drift Detection */}
          <div className="card tools-card" style={{ marginTop: '2rem' }}>
            <h2 className="mono-text flex-center" style={{ gap: '0.5rem' }}>
              <AlertTriangle size={20}/> ONTOLOGY DRIFT
            </h2>
            <p style={{ marginTop: '1rem', marginBottom: '1rem', fontSize: '0.9rem', lineHeight: 1.5 }}>
              Detect when new incoming data no longer fits the current schema. The drift detector proposes additions for review.
            </p>
            <button
              onClick={handleDetectDrift}
              className="app-btn outline-btn full-width"
              disabled={detectingDrift || loading}
              style={{ display: 'flex', justifyContent: 'center', gap: '8px', marginBottom: driftReports.length > 0 ? '1rem' : 0 }}
            >
              <AlertTriangle size={18} /> {detectingDrift ? 'DETECTING...' : 'DETECT DRIFT'}
            </button>
            {driftReports.length > 0 && (
              <div style={{ display: 'flex', flexDirection: 'column', gap: '0.5rem', marginTop: '0.5rem' }}>
                <div className="control-label" style={{ marginBottom: '0.25rem' }}>PENDING DRIFT REPORTS</div>
                {driftReports.slice(0, 3).map((r: any) => (
                  <div key={r.id} style={{ border: '1px solid #e5e5e5', padding: '0.6rem 0.75rem', display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: '0.5rem' }}>
                    <span style={{ fontFamily: 'var(--font-mono)', fontSize: '0.72rem', flex: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
                      {r.summary || r.id}
                    </span>
                    <div style={{ display: 'flex', gap: '4px', flexShrink: 0 }}>
                      <button onClick={() => handleDriftAction(r.id, 'approve')}
                        style={{ background: '#f0fdf4', color: '#16a34a', border: '1px solid #16a34a', padding: '2px 8px', cursor: 'pointer', fontSize: '0.72rem' }}>
                        <Check size={10}/>
                      </button>
                      <button onClick={() => handleDriftAction(r.id, 'reject')}
                        style={{ background: '#fef2f2', color: '#dc2626', border: '1px solid #dc2626', padding: '2px 8px', cursor: 'pointer', fontSize: '0.72rem' }}>
                        <X size={10}/>
                      </button>
                    </div>
                  </div>
                ))}
              </div>
            )}
          </div>

          {/* ── Live Stats Panel ── */}
          {stats && (
            <div className="card tools-card stats-panel" style={{ marginTop: '2rem' }}>
              <h2 className="mono-text flex-center" style={{ gap: '0.5rem', marginBottom: '1rem' }}>
                <Database size={20}/> GRAPH STATISTICS
                {selectedDocId && <span className="scope-badge">DOC FILTERED</span>}
              </h2>

              <div className="stats-summary">
                <div className="sum-chip">
                  <div className="sum-val">{stats.total_entities}</div>
                  <div className="sum-key">TOTAL ENTITIES</div>
                </div>
                <div className="sum-chip">
                  <div className="sum-val">{stats.total_relationships}</div>
                  <div className="sum-key">RELATIONSHIPS</div>
                </div>
              </div>

              {stats.entity_stats?.length > 0 && (
                <>
                  <div className="stat-section-lbl">ENTITY TYPES</div>
                  {stats.entity_stats.slice(0, 8).map((s: any) => (
                    <div key={s.type} className="stat-bar-row">
                      <span className="stat-bar-label">{s.type}</span>
                      <div className="stat-bar-track">
                        <div
                          className="stat-bar-fill"
                          style={{ width: `${Math.min(100, (s.count / stats.total_entities) * 100)}%` }}
                        />
                      </div>
                      <span className="stat-bar-count">{s.count}</span>
                    </div>
                  ))}
                </>
              )}
            </div>
          )}
        </div>
      </div>

      {message && (
        <div className={`status-toast ${message.includes('ERROR') || message.includes('FAILED') ? 'error' : ''}`}>
          <Info size={20} /> {message}
          <button 
            onClick={() => setMessage('')} 
            className="toast-dismiss-btn"
          >
            &times;
          </button>
        </div>
      )}

      <style>{`
        .doc-dropdown {
          background: var(--bg-color);
          color: var(--text-color);
          border: 2px solid var(--border-color);
          padding: 0.5rem;
          font-size: 0.9rem;
          font-weight: bold;
          outline: none;
          max-width: 100%;
          text-overflow: ellipsis;
        }

        .ontology-layout {
          display: grid;
          grid-template-columns: 3fr 2fr;
          gap: 2rem;
          margin-top: 1rem;
        }
        
        @media (max-width: 768px) {
          .ontology-layout {
            grid-template-columns: 1fr;
          }
        }

        .form-group {
          margin-bottom: 1.5rem;
        }

        .form-group label {
          display: block;
          margin-bottom: 0.5rem;
          font-weight: bold;
          font-size: 0.85rem;
          color: #555;
        }

        .form-group textarea {
          width: 100%;
          padding: 1rem;
          border: 2px solid var(--border-color);
          background: var(--bg-color);
          color: var(--text-color);
          resize: vertical;
          font-size: 0.9rem;
        }

        .form-group textarea:focus {
          outline: none;
          border-color: #555;
        }

        .dict-editor {
          font-family: var(--font-mono);
          white-space: pre;
          background-color: #fafafa !important;
        }

        .status-toast {
          position: fixed;
          bottom: 2rem;
          right: 2rem;
          background: var(--text-color);
          color: var(--bg-color);
          padding: 1rem 1.5rem;
          border-left: 6px solid var(--text-color);
          box-shadow: 4px 4px 0 var(--border-color);
          display: flex;
          align-items: center;
          gap: 1rem;
          z-index: 9999;
          font-family: var(--font-mono);
          font-weight: bold;
          font-size: 0.9rem;
          animation: slideUp 0.3s ease-out;
          max-width: 400px;
        }

        .status-toast.error {
          border-left-color: #ff0000;
        }

        .refine-info-box {
          background: #f5f5f5;
          border-left: 3px solid #000;
          padding: 0.5rem 0.75rem;
          font-size: 0.78rem;
          line-height: 1.6;
          display: flex;
          gap: 0.5rem;
          align-items: flex-start;
        }

        .control-label {
          font-family: var(--font-mono);
          font-size: 0.7rem;
          font-weight: 700;
          color: #666;
          letter-spacing: 1px;
          text-transform: uppercase;
        }

        .outline-btn {
          background: transparent !important;
          color: var(--text-color) !important;
          border: 2px solid var(--text-color) !important;
        }
        .outline-btn:hover:not(:disabled) {
          background: var(--text-color) !important;
          color: var(--bg-color) !important;
        }

        /* ── Stats panel ── */
        .stats-summary {
          display: grid;
          grid-template-columns: 1fr 1fr;
          gap: 0.75rem;
          margin-bottom: 1rem;
        }
        .sum-chip {
          border: 1.5px solid #e5e5e5;
          padding: 0.6rem 0.75rem;
          text-align: center;
        }
        .sum-val {
          font-family: var(--font-mono);
          font-size: 1.4rem;
          font-weight: 700;
          line-height: 1;
        }
        .sum-key {
          font-family: var(--font-mono);
          font-size: 0.62rem;
          color: #888;
          letter-spacing: 1px;
          margin-top: 0.2rem;
        }
        .stat-section-lbl {
          font-family: var(--font-mono);
          font-size: 0.65rem;
          font-weight: 700;
          color: #888;
          letter-spacing: 1px;
          margin-bottom: 0.5rem;
        }
        .stat-bar-row {
          display: flex;
          align-items: center;
          gap: 0.5rem;
          margin-bottom: 0.35rem;
          font-family: var(--font-mono);
          font-size: 0.75rem;
        }
        .stat-bar-label {
          width: 90px;
          flex-shrink: 0;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          color: #444;
        }
        .stat-bar-track {
          flex: 1;
          height: 6px;
          background: #e5e5e5;
          border-radius: 2px;
          overflow: hidden;
        }
        .stat-bar-fill {
          height: 100%;
          background: #000;
          transition: width 0.4s ease;
        }
        .stat-bar-count {
          width: 28px;
          text-align: right;
          color: #666;
          font-size: 0.72rem;
        }
        .scope-badge {
          font-size: 0.6rem;
          background: #000;
          color: #fff;
          padding: 2px 6px;
          letter-spacing: 0.5px;
          margin-left: 0.4rem;
        }
      `}</style>
    </div>
  );
};

export default Ontology;