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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9fa64b
c2ea5ed
 
 
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
import React, { useState } from "react";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import {
  Database,
  ChevronDown,
  Clock,
  Activity,
  GitBranch,
  GitCompare,
} from "lucide-react";
import { AvailableGraph } from "@/types";

interface GraphSelectorProps {
  availableGraphs: AvailableGraph[];
  selectedGraph1: AvailableGraph | null;
  selectedGraph2: AvailableGraph | null;
  onSelectionChange: (
    graph1: AvailableGraph | null,
    graph2: AvailableGraph | null
  ) => void;
  onCompareGraphs: () => Promise<void>;
  isLoading: boolean;
}

export const GraphSelector: React.FC<GraphSelectorProps> = ({
  availableGraphs,
  selectedGraph1,
  selectedGraph2,
  onSelectionChange,
  onCompareGraphs,
  isLoading,
}) => {
  const [expandedGraphs, setExpandedGraphs] = useState<Set<number>>(new Set());

  const toggleGraphExpansion = (graphId: number) => {
    const newExpanded = new Set(expandedGraphs);
    if (newExpanded.has(graphId)) {
      newExpanded.delete(graphId);
    } else {
      newExpanded.add(graphId);
    }
    setExpandedGraphs(newExpanded);
  };

  const handleGraphSelect = (graph: AvailableGraph) => {
    // If clicking on already selected graph, deselect it
    if (selectedGraph1?.id === graph.id) {
      onSelectionChange(null, selectedGraph2);
      return;
    }

    if (selectedGraph2?.id === graph.id) {
      onSelectionChange(selectedGraph1, null);
      return;
    }

    // Select in first available slot
    if (!selectedGraph1) {
      onSelectionChange(graph, selectedGraph2);
    } else if (!selectedGraph2) {
      onSelectionChange(selectedGraph1, graph);
    } else {
      // Both slots filled, replace the first one
      onSelectionChange(graph, selectedGraph2);
    }
  };

  const clearAllSelections = () => {
    onSelectionChange(null, null);
  };

  const getSelectionClass = (graph: AvailableGraph) => {
    if (selectedGraph1?.id === graph.id) return "selected selected-1";
    if (selectedGraph2?.id === graph.id) return "selected selected-2";
    return "";
  };

  const formatDate = (dateString: string) => {
    return new Date(dateString).toLocaleDateString();
  };

  const getGraphTypeIcon = (type: string) => {
    switch (type) {
      case "final":
        return Activity;
      case "chunk":
        return Clock;
      default:
        return GitBranch;
    }
  };

  const truncateGraphName = (
    name: string,
    maxLength: number = 35
  ): string => {
    if (!name) return "";
    if (name.length <= maxLength) return name;

    // Smart truncation - keep beginning and end if possible
    const start = name.substring(0, Math.floor(maxLength * 0.6));
    const end = name.substring(name.length - Math.floor(maxLength * 0.3));
    return `${start}...${end}`;
  };

  // Prefer a human-friendly system name if available
  const getGraphDisplayName = (graph: AvailableGraph | null | undefined) => {
    if (!graph) return "";
    return graph.system_name && graph.system_name.trim().length > 0
      ? graph.system_name
      : graph.filename;
  };

  const getSelectionNumber = (graph: AvailableGraph) => {
    if (selectedGraph1?.id === graph.id) return "1";
    if (selectedGraph2?.id === graph.id) return "2";
    return null;
  };

  const canCompare = selectedGraph1 && selectedGraph2;

  return (
    <div className="space-y-6">
      {/* Header */}
      <div className="flex items-center justify-between">
        <div>
          <h2 className="text-lg font-semibold">Select Graphs to Compare</h2>
          <p className="text-sm text-muted-foreground">
            Choose exactly 2 knowledge graphs for comparison
          </p>
        </div>
        <div className="flex items-center gap-2">
          {canCompare && (
            <Button
              onClick={onCompareGraphs}
              disabled={isLoading}
              className="flex items-center gap-2"
            >
              <GitCompare className="h-4 w-4" />
              {isLoading ? "Comparing..." : "Compare Graphs"}
            </Button>
          )}
          {(selectedGraph1 || selectedGraph2) && (
            <Button variant="outline" onClick={clearAllSelections}>
              Clear Selection
            </Button>
          )}
        </div>
      </div>

      {/* Selection Indicators */}
      <div className="grid grid-cols-2 gap-4">
        <Card
          className={`transition-all ${
            selectedGraph1 ? "ring-2 ring-blue-500" : "border-dashed"
          }`}
        >
          <CardContent className="p-4">
            <div className="flex items-center gap-2">
              <div className="w-6 h-6 rounded-full bg-blue-500 text-white text-xs flex items-center justify-center font-medium">
                1
              </div>
              <div className="flex-1">
                {selectedGraph1 ? (
                  <div>
                    <div
                      className="font-medium text-sm whitespace-normal break-words"
                      title={getGraphDisplayName(selectedGraph1)}
                    >
                      {getGraphDisplayName(selectedGraph1)}
                    </div>
                    <div className="text-xs text-muted-foreground">
                      {selectedGraph1.graph_type} •{" "}
                      {selectedGraph1.entity_count} entities
                    </div>
                  </div>
                ) : (
                  <div className="text-sm text-muted-foreground">
                    Select first graph
                  </div>
                )}
              </div>
            </div>
          </CardContent>
        </Card>

        <Card
          className={`transition-all ${
            selectedGraph2 ? "ring-2 ring-green-500" : "border-dashed"
          }`}
        >
          <CardContent className="p-4">
            <div className="flex items-center gap-2">
              <div className="w-6 h-6 rounded-full bg-green-500 text-white text-xs flex items-center justify-center font-medium">
                2
              </div>
              <div className="flex-1">
                {selectedGraph2 ? (
                  <div>
                    <div
                      className="font-medium text-sm whitespace-normal break-words"
                      title={getGraphDisplayName(selectedGraph2)}
                    >
                      {getGraphDisplayName(selectedGraph2)}
                    </div>
                    <div className="text-xs text-muted-foreground">
                      {selectedGraph2.graph_type} •{" "}
                      {selectedGraph2.entity_count} entities
                    </div>
                  </div>
                ) : (
                  <div className="text-sm text-muted-foreground">
                    Select second graph
                  </div>
                )}
              </div>
            </div>
          </CardContent>
        </Card>
      </div>

      {/* Graph List */}
      <div className="space-y-4">
        {availableGraphs.length === 0 ? (
          <Card>
            <CardContent className="p-8 text-center">
              <Database className="h-12 w-12 mx-auto text-muted-foreground mb-4" />
              <h3 className="text-lg font-semibold mb-2">
                No Graphs Available
              </h3>
              <p className="text-muted-foreground">
                No knowledge graphs found for comparison. Upload traces and
                generate graphs first.
              </p>
            </CardContent>
          </Card>
        ) : (
          <Card>
            <CardHeader>
              <CardTitle className="text-base flex items-center gap-2">
                <Database className="h-4 w-4" />
                Available Graphs
                <Badge variant="outline">{availableGraphs.length}</Badge>
              </CardTitle>
            </CardHeader>
            <CardContent className="p-0">
              <div className="space-y-0">
                {availableGraphs.map((graph) => {
                  const hasChunks =
                    graph.chunk_graphs && graph.chunk_graphs.length > 0;
                  const isExpanded = expandedGraphs.has(graph.id);
                  const IconComponent = getGraphTypeIcon(graph.graph_type);

                  return (
                    <div key={graph.id} className="border-b last:border-b-0">
                      {/* Final Graph Item */}
                      <div
                        className={`relative p-5 cursor-pointer hover:bg-muted/50 transition-all duration-200 ${getSelectionClass(
                          graph
                        )}`}
                        onClick={() => handleGraphSelect(graph)}
                      >
                        <div className="flex items-center gap-4">
                          <div className="flex items-center gap-3 flex-1 min-w-0">
                            <IconComponent className="h-5 w-5 text-primary flex-shrink-0" />
                            <div className="flex-1 min-w-0">
                              <div
                                className="font-semibold text-base text-foreground mb-1 whitespace-normal break-words"
                                title={getGraphDisplayName(graph)}
                              >
                                {getGraphDisplayName(graph)}
                              </div>
                              <div className="flex items-center gap-3 flex-wrap">
                                <Badge
                                  variant={
                                    graph.graph_type === "final"
                                      ? "default"
                                      : "secondary"
                                  }
                                  className="text-xs font-medium px-2 py-1"
                                >
                                  {graph.graph_type}
                                </Badge>
                                {graph.trace_title && (
                                  <span className="text-xs text-muted-foreground truncate">
                                    from{" "}
                                    {truncateGraphName(graph.trace_title, 25)}
                                  </span>
                                )}
                              </div>
                            </div>
                          </div>

                          <div className="flex items-center gap-6 text-sm">
                            <div className="text-center">
                              <div className="font-bold text-foreground">
                                {graph.entity_count}
                              </div>
                              <div className="text-xs text-muted-foreground">
                                entities
                              </div>
                            </div>
                            <div className="text-center">
                              <div className="font-bold text-foreground">
                                {graph.relation_count}
                              </div>
                              <div className="text-xs text-muted-foreground">
                                relations
                              </div>
                            </div>
                            {hasChunks && (
                              <div className="text-center">
                                <div className="font-bold text-foreground">
                                  {graph.chunk_graphs!.length}
                                </div>
                                <div className="text-xs text-muted-foreground">
                                  chunks
                                </div>
                              </div>
                            )}
                            <div className="text-center">
                              <div className="font-bold text-foreground">
                                {formatDate(graph.creation_timestamp)}
                              </div>
                              <div className="text-xs text-muted-foreground">
                                created
                              </div>
                            </div>
                          </div>

                          <div className="flex items-center gap-2 flex-shrink-0">
                            {hasChunks && (
                              <Button
                                variant="ghost"
                                size="sm"
                                onClick={(e) => {
                                  e.stopPropagation();
                                  toggleGraphExpansion(graph.id);
                                }}
                                className="p-1 h-8 w-8 hover:bg-muted"
                              >
                                <ChevronDown
                                  className={`h-4 w-4 transition-transform ${
                                    isExpanded ? "rotate-180" : ""
                                  }`}
                                />
                              </Button>
                            )}
                          </div>
                        </div>

                        {/* Selection Number Badge */}
                        {getSelectionNumber(graph) && (
                          <div
                            className={`absolute top-3 right-3 w-7 h-7 rounded-full text-white text-sm font-bold flex items-center justify-center shadow-lg transition-all duration-300 ${
                              getSelectionNumber(graph) === "1"
                                ? "bg-blue-500 shadow-blue-500/30"
                                : "bg-green-500 shadow-green-500/30"
                            }`}
                            style={{
                              animation: "selectionPulse 0.5s ease",
                            }}
                          >
                            {getSelectionNumber(graph)}
                          </div>
                        )}
                      </div>

                      {/* Chunk Graphs */}
                      {hasChunks && isExpanded && (
                        <div className="bg-muted/20 border-t">
                          {graph.chunk_graphs!.map((chunk) => {
                            const ChunkIcon = getGraphTypeIcon(
                              chunk.graph_type
                            );
                            return (
                              <div
                                key={chunk.id}
                                className={`relative p-4 pl-12 cursor-pointer hover:bg-muted/40 transition-all duration-200 border-t border-muted/40 ${getSelectionClass(
                                  chunk
                                )}`}
                                onClick={() => handleGraphSelect(chunk)}
                              >
                                <div className="flex items-center gap-4">
                                  <div className="flex items-center gap-3 flex-1 min-w-0">
                                    <ChunkIcon className="h-4 w-4 text-orange-500 flex-shrink-0" />
                                    <div className="flex-1 min-w-0">
                                      <div className="font-semibold text-sm text-foreground mb-1">
                                        Window{" "}
                                        {(chunk.window_info?.index || 0) + 1}/
                                        {chunk.window_info?.total || "?"}
                                      </div>
                                      <div className="flex items-center gap-2">
                                        <Badge
                                          variant="secondary"
                                          className="text-xs font-medium px-2 py-1"
                                        >
                                          {chunk.graph_type}
                                        </Badge>
                                        {chunk.window_info && (
                                          <span className="text-xs text-muted-foreground">
                                            {chunk.window_info.start_char?.toLocaleString() ||
                                              "N/A"}{" "}
                                            -{" "}
                                            {chunk.window_info.end_char?.toLocaleString() ||
                                              "N/A"}{" "}
                                            chars
                                          </span>
                                        )}
                                      </div>
                                    </div>
                                  </div>

                                  <div className="flex items-center gap-6 text-sm">
                                    <div className="text-center">
                                      <div className="font-bold text-foreground">
                                        {chunk.entity_count}
                                      </div>
                                      <div className="text-xs text-muted-foreground">
                                        entities
                                      </div>
                                    </div>
                                    <div className="text-center">
                                      <div className="font-bold text-foreground">
                                        {chunk.relation_count}
                                      </div>
                                      <div className="text-xs text-muted-foreground">
                                        relations
                                      </div>
                                    </div>
                                  </div>
                                </div>

                                {/* Selection Number Badge for Chunks */}
                                {getSelectionNumber(chunk) && (
                                  <div
                                    className={`absolute top-3 right-3 w-6 h-6 rounded-full text-white text-xs font-bold flex items-center justify-center shadow-lg transition-all duration-300 ${
                                      getSelectionNumber(chunk) === "1"
                                        ? "bg-blue-500 shadow-blue-500/30"
                                        : "bg-green-500 shadow-green-500/30"
                                    }`}
                                    style={{
                                      animation: "selectionPulse 0.5s ease",
                                    }}
                                  >
                                    {getSelectionNumber(chunk)}
                                  </div>
                                )}
                              </div>
                            );
                          })}
                        </div>
                      )}
                    </div>
                  );
                })}
              </div>
            </CardContent>
          </Card>
        )}
      </div>

      {/* Selection Summary removed */}
    </div>
  );
};