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
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
import React, { useState, useMemo, useCallback } from "react";
import { useAgentGraph } from "@/context/AgentGraphContext";
import { useNavigation } from "@/context/NavigationContext";
import {
  Search,
  Bell,
  Settings,
  HelpCircle,
  User,
  ChevronDown,
  FileText,
  GitBranch,
} from "lucide-react";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Input } from "@/components/ui/input";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { WelcomeGuideModal } from "@/components/shared/WelcomeGuideModal";
import { SettingsModal } from "@/components/shared/SettingsModal";

export function TopNavBar() {
  const { state, actions } = useAgentGraph();
  const navigation = useNavigation();
  const [searchQuery, setSearchQuery] = useState("");
  const [isSearchOpen, setIsSearchOpen] = useState(false);
  const [isHelpModalOpen, setIsHelpModalOpen] = useState(false);
  const [isNotificationsOpen, setIsNotificationsOpen] = useState(false);
  const [isSettingsModalOpen, setIsSettingsModalOpen] = useState(false);

  const handleDashboardClick = useCallback(() => {
    actions.setActiveView("welcome");
  }, [actions]);

  const searchResults = useMemo(() => {
    if (!searchQuery.trim()) return { traces: [], graphs: [] };

    const query = searchQuery.toLowerCase();

    const traces = state.traces.filter(
      (trace) =>
        trace.filename.toLowerCase().includes(query) ||
        trace.title?.toLowerCase().includes(query) ||
        trace.description?.toLowerCase().includes(query)
    );

    const graphs = state.knowledgeGraphs.filter(
      (kg) =>
        kg.filename.toLowerCase().includes(query) ||
        kg.kg_id.toLowerCase().includes(query)
    );

    return { traces: traces.slice(0, 5), graphs: graphs.slice(0, 5) };
  }, [searchQuery, state.traces, state.knowledgeGraphs]);

  const handleSearchSelect = useCallback(
    (type: "trace" | "graph", id: string) => {
      if (type === "trace") {
        const trace = state.traces.find((t) => t.id.toString() === id);
        if (trace) {
          actions.setSelectedTrace(trace);
          actions.setActiveView("trace-kg");
        }
      } else {
        const graph = state.knowledgeGraphs.find((kg) => kg.kg_id === id);
        if (graph) {
          actions.setSelectedKnowledgeGraph(graph);
          actions.setActiveView("kg-visualizer");
        }
      }
      setIsSearchOpen(false);
      setSearchQuery("");
    },
    [state.traces, state.knowledgeGraphs, actions]
  );

  // Close search when clicking outside
  React.useEffect(() => {
    const handleClickOutside = (event: MouseEvent) => {
      const target = event.target as Element;
      if (!target.closest(".search-container")) {
        setIsSearchOpen(false);
      }
    };

    if (isSearchOpen) {
      document.addEventListener("mousedown", handleClickOutside);
      return () =>
        document.removeEventListener("mousedown", handleClickOutside);
    }

    return undefined;
  }, [isSearchOpen]);

  const results = searchResults;
  const hasResults = results.traces.length > 0 || results.graphs.length > 0;

  // Memoize notification calculations to prevent infinite loops
  const unreadNotificationsCount = useMemo(() => {
    return navigation.state.notifications.filter((n) => !n.read).length;
  }, [navigation.state.notifications]);

  const hasUnreadNotifications = unreadNotificationsCount > 0;

  return (
    <header className="h-16 border-b border-slate-700 bg-gradient-to-r from-slate-800 via-slate-850 to-slate-800 backdrop-blur supports-[backdrop-filter]:bg-slate-800/60 sticky top-0 z-50 shadow-lg">
      <div className="flex items-center justify-between h-full px-6">
        {/* Left Section - Logo and Brand */}
        <div className="flex items-center gap-6">
          {/* Logo */}
          <div
            className="flex items-center gap-3 cursor-pointer hover:opacity-80 transition-opacity"
            onClick={handleDashboardClick}
          >
            <div className="relative">
              <div className="h-8 w-8 rounded-lg bg-gradient-to-br from-violet-500 to-purple-600 flex items-center justify-center shadow-md">
                {/* Simple, Modern AgentGraph Icon */}
                <svg
                  width="18"
                  height="18"
                  viewBox="0 0 20 20"
                  fill="none"
                  className="text-white"
                >
                  {/* Main diamond shape representing AI core */}
                  <path
                    d="M10 2L16 8L10 14L4 8L10 2Z"
                    fill="currentColor"
                    opacity="0.9"
                  />

                  {/* Connection nodes */}
                  <circle cx="10" cy="6" r="1.5" fill="white" opacity="0.8" />
                  <circle cx="6" cy="10" r="1.5" fill="white" opacity="0.8" />
                  <circle cx="14" cy="10" r="1.5" fill="white" opacity="0.8" />

                  {/* Graph connections */}
                  <path
                    d="M10 16L18 8M10 16L2 8"
                    stroke="currentColor"
                    strokeWidth="1.5"
                    opacity="0.6"
                    strokeLinecap="round"
                  />
                </svg>
              </div>
              <div className="absolute -top-1 -right-1 h-3 w-3 rounded-full bg-green-400 border-2 border-slate-800"></div>
            </div>
            <div className="flex flex-col">
              <span className="text-lg font-bold bg-gradient-to-r from-white to-slate-200 bg-clip-text text-transparent">
                AgentGraph
              </span>
            </div>
          </div>

          {/* Navigation Divider */}
          <div className="h-8 w-px bg-gradient-to-b from-transparent via-slate-600 to-transparent" />

          {/* Enhanced Search Bar */}
          <div className="flex-1 max-w-2xl mx-8">
            <div className="relative search-container">
              <Search className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-slate-400" />
              <Input
                placeholder="Search traces, graphs, and more..."
                value={searchQuery}
                onChange={(e) => setSearchQuery(e.target.value)}
                onFocus={() => setIsSearchOpen(true)}
                className="w-full pl-10 h-9 bg-slate-700/50 border-slate-600 text-white placeholder-slate-400 hover:bg-slate-700/70 focus:bg-slate-700 transition-colors"
              />

              {/* Search Results Dropdown */}
              {isSearchOpen && (
                <div className="absolute top-full left-0 right-0 mt-1 bg-popover border rounded-md shadow-lg z-50 max-h-80 overflow-y-auto">
                  {searchQuery.trim() && hasResults ? (
                    <div className="p-2">
                      {results.traces.length > 0 && (
                        <div className="mb-4">
                          <div className="px-2 py-1 text-xs font-medium text-muted-foreground uppercase tracking-wide">
                            Traces
                          </div>
                          {results.traces.map((trace) => (
                            <Button
                              key={trace.id}
                              variant="ghost"
                              className="w-full justify-start h-auto p-2 mb-1"
                              onClick={() =>
                                handleSearchSelect("trace", trace.id.toString())
                              }
                            >
                              <FileText className="h-4 w-4 mr-2 flex-shrink-0" />
                              <div className="text-left">
                                <div className="font-medium truncate">
                                  {trace.filename}
                                </div>
                                {trace.description && (
                                  <div className="text-xs text-muted-foreground truncate">
                                    {trace.description}
                                  </div>
                                )}
                              </div>
                            </Button>
                          ))}
                        </div>
                      )}
                      {results.graphs.length > 0 && (
                        <div>
                          <div className="px-2 py-1 text-xs font-medium text-muted-foreground uppercase tracking-wide">
                            Agent Graphs
                          </div>
                          {results.graphs.map((graph) => (
                            <Button
                              key={graph.kg_id}
                              variant="ghost"
                              className="w-full justify-start h-auto p-2 mb-1"
                              onClick={() =>
                                handleSearchSelect("graph", graph.kg_id)
                              }
                            >
                              <GitBranch className="h-4 w-4 mr-2 flex-shrink-0" />
                              <div className="text-left">
                                <div className="font-medium truncate">
                                  {graph.filename}
                                </div>
                                <div className="text-xs text-muted-foreground">
                                  {graph.entity_count} entities,{" "}
                                  {graph.relation_count} relations
                                </div>
                              </div>
                            </Button>
                          ))}
                        </div>
                      )}
                    </div>
                  ) : searchQuery.trim() && !hasResults ? (
                    <div className="p-8 text-center text-muted-foreground">
                      <Search className="h-8 w-8 mx-auto mb-2" />
                      <p className="text-sm">No results found</p>
                    </div>
                  ) : (
                    <div className="p-8 text-center text-muted-foreground">
                      <Search className="h-8 w-8 mx-auto mb-2" />
                      <p className="text-sm">Start typing to search...</p>
                    </div>
                  )}
                </div>
              )}
            </div>
          </div>
        </div>

        {/* Right Section - User Actions with enhanced styling */}
        <div className="flex items-center gap-1">
          {/* Notifications */}
          <DropdownMenu
            open={isNotificationsOpen}
            onOpenChange={setIsNotificationsOpen}
          >
            <DropdownMenuTrigger asChild>
              <Button
                variant="ghost"
                size="sm"
                className="relative text-slate-300 hover:bg-slate-700/50 hover:text-yellow-400 transition-colors"
              >
                <Bell className="h-4 w-4" />
                {hasUnreadNotifications && (
                  <Badge
                    variant="destructive"
                    className="absolute -top-1 -right-1 h-5 w-5 rounded-full p-0 text-xs animate-pulse flex items-center justify-center"
                  >
                    {unreadNotificationsCount}
                  </Badge>
                )}
              </Button>
            </DropdownMenuTrigger>
            <DropdownMenuContent align="end" className="w-80">
              <div className="px-4 py-3 border-b">
                <h4 className="font-semibold">Notifications</h4>
                <p className="text-sm text-muted-foreground">
                  {unreadNotificationsCount} unread
                </p>
              </div>
              <div className="max-h-80 overflow-y-auto">
                {navigation.state.notifications.length > 0 ? (
                  navigation.state.notifications
                    .slice(0, 10)
                    .map((notification) => {
                      // Get notification type colors
                      const getNotificationColor = (type: string) => {
                        switch (type) {
                          case "success":
                            return "border-l-green-500 bg-green-50 dark:bg-green-950/20";
                          case "error":
                            return "border-l-red-500 bg-red-50 dark:bg-red-950/20";
                          case "warning":
                            return "border-l-yellow-500 bg-yellow-50 dark:bg-yellow-950/20";
                          case "info":
                            return "border-l-blue-500 bg-blue-50 dark:bg-blue-950/20";
                          default:
                            return "border-l-gray-500 bg-gray-50 dark:bg-gray-950/20";
                        }
                      };

                      return (
                        <DropdownMenuItem
                          key={notification.id}
                          className={`flex flex-col items-start p-4 hover:bg-muted/50 border-l-4 ${getNotificationColor(
                            notification.type
                          )}`}
                          onClick={() =>
                            navigation.actions.markNotificationRead(
                              notification.id
                            )
                          }
                        >
                          <div className="flex justify-between w-full">
                            <span className="font-medium">
                              {notification.title}
                            </span>
                            <span className="text-xs text-muted-foreground">
                              {notification.timestamp.toLocaleDateString()}
                            </span>
                          </div>
                          <p className="text-sm text-muted-foreground mt-1">
                            {notification.message}
                          </p>
                        </DropdownMenuItem>
                      );
                    })
                ) : (
                  <div className="p-8 text-center text-muted-foreground">
                    <Bell className="h-8 w-8 mx-auto mb-2" />
                    <p className="text-sm">No notifications</p>
                  </div>
                )}
              </div>
              {navigation.state.notifications.length > 0 && (
                <div className="border-t p-2">
                  <Button
                    variant="ghost"
                    size="sm"
                    className="w-full"
                    onClick={() => navigation.actions.clearAllNotifications()}
                  >
                    Clear all
                  </Button>
                </div>
              )}
            </DropdownMenuContent>
          </DropdownMenu>

          {/* Help */}
          <Button
            variant="ghost"
            size="sm"
            className="text-slate-300 hover:bg-slate-700/50 hover:text-green-400 transition-colors"
            onClick={() => setIsHelpModalOpen(true)}
          >
            <HelpCircle className="h-4 w-4" />
          </Button>

          {/* Settings */}
          <Button
            variant="ghost"
            size="sm"
            className="text-slate-300 hover:bg-slate-700/50 hover:text-slate-100 transition-colors"
            onClick={() => setIsSettingsModalOpen(true)}
          >
            <Settings className="h-4 w-4" />
          </Button>

          {/* Divider */}
          <div className="h-6 w-px bg-slate-600 mx-1" />

          {/* User Menu with enhanced styling */}
          <DropdownMenu>
            <DropdownMenuTrigger asChild>
              <Button
                variant="ghost"
                size="sm"
                className="flex items-center gap-2 text-slate-300 hover:bg-slate-700/50 hover:text-white transition-colors px-3"
              >
                <div className="h-7 w-7 rounded-full bg-gradient-to-br from-indigo-500 to-purple-600 flex items-center justify-center">
                  <User className="h-4 w-4 text-white" />
                </div>
                <span className="hidden sm:block">User</span>
                <ChevronDown className="h-3 w-3" />
              </Button>
            </DropdownMenuTrigger>
            <DropdownMenuContent align="end" className="w-56">
              <DropdownMenuItem>
                <User className="mr-2 h-4 w-4" />
                Profile
              </DropdownMenuItem>
              <DropdownMenuItem>
                <Settings className="mr-2 h-4 w-4" />
                Settings
              </DropdownMenuItem>
              <DropdownMenuItem>
                <HelpCircle className="mr-2 h-4 w-4" />
                Help & Support
              </DropdownMenuItem>
              <DropdownMenuSeparator />
              <DropdownMenuItem>Log out</DropdownMenuItem>
            </DropdownMenuContent>
          </DropdownMenu>
        </div>
      </div>

      {/* Help Modal */}
      <WelcomeGuideModal
        open={isHelpModalOpen}
        onOpenChange={setIsHelpModalOpen}
      />

      {/* Settings Modal */}
      <SettingsModal
        open={isSettingsModalOpen}
        onOpenChange={setIsSettingsModalOpen}
      />
    </header>
  );
}