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
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
import React, { useState, useMemo } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Badge } from "@/components/ui/badge";
import { ErrorBoundary } from "../shared/ErrorBoundary";
import {
  BarChart3,
  FileText,
  Upload,
  GitCompare,
  Link,
  Layers,
  Search,
  Bell,
  Settings,
  HelpCircle,
  User,
  ChevronDown,
  ChevronRight,
  PanelLeftClose,
  PanelLeftOpen,
} from "lucide-react";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { useAgentGraph } from "@/context/AgentGraphContext";
import { useNavigation } from "@/context/NavigationContext";

import { WelcomeGuideModal } from "@/components/shared/WelcomeGuideModal";
import { SettingsModal } from "@/components/shared/SettingsModal";

interface SidebarItem {
  id: string;
  label: string;
  icon: React.ComponentType<{ className?: string }>;
  onClick: () => void;
  badge?: string | number;
}

interface SidebarGroup {
  id: string;
  label: string;
  items: SidebarItem[];
  isCollapsible?: boolean;
}

interface SidebarProps {
  isCollapsed: boolean;
  onToggleSidebar: () => void;
}

export function AppSidebar({ isCollapsed, onToggleSidebar }: SidebarProps) {
  const { actions } = useAgentGraph();
  const navigation = useNavigation();

  const [expandedGroups, setExpandedGroups] = useState<string[]>([
    "main",
    "tools",
  ]);
  const [searchQuery, setSearchQuery] = useState("");
  const [isNotificationsOpen, setIsNotificationsOpen] = useState(false);
  const [isHelpModalOpen, setIsHelpModalOpen] = useState(false);
  const [isSettingsModalOpen, setIsSettingsModalOpen] = useState(false);

  const handleDashboardClick = () => {
    actions.setActiveView("welcome");
  };

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

  const hasUnreadNotifications = unreadNotificationsCount > 0;

  const navigationGroups: SidebarGroup[] = [
    {
      id: "main",
      label: "Main",
      isCollapsible: true,
      items: [
        {
          id: "dashboard",
          label: "Dashboard",
          icon: BarChart3,
          onClick: () => actions.setActiveView("welcome"),
        },
        {
          id: "traces",
          label: "My Traces",
          icon: FileText,
          onClick: () => actions.setActiveView("traces"),
        },
        {
          id: "gallery",
          label: "Gallery",
          icon: Layers,
          onClick: () => actions.setActiveView("example-traces"),
        },
      ],
    },
    {
      id: "tools",
      label: "Tools",
      isCollapsible: true,
      items: [
        {
          id: "upload",
          label: "Upload",
          icon: Upload,
          onClick: () => actions.setActiveView("upload"),
        },
        {
          id: "compare",
          label: "Compare",
          icon: GitCompare,
          onClick: () => actions.setActiveView("graph-comparison"),
        },
        {
          id: "connections",
          label: "Connections",
          icon: Link,
          onClick: () => actions.setActiveView("connections"),
        },
      ],
    },
  ];

  const toggleGroup = (groupId: string) => {
    setExpandedGroups((prev) =>
      prev.includes(groupId)
        ? prev.filter((id) => id !== groupId)
        : [...prev, groupId]
    );
  };

  return (
    <>
      <aside
        className={`${
          isCollapsed ? "w-16" : "w-64"
        } border-r border-slate-700 bg-gradient-to-b from-slate-800 to-slate-900 text-white transition-[width] duration-300 ease-in-out overflow-hidden flex flex-col h-full fixed left-0 top-0 z-40`}
      >
        {/* Header - Logo and Brand */}
        <div className="flex items-center justify-center p-3 border-b border-slate-700">
          {!isCollapsed && (
            <div
              className="flex items-center gap-2 cursor-pointer hover:opacity-80 transition-opacity"
              onClick={handleDashboardClick}
            >
              <div className="relative">
                <div className="h-7 w-7 rounded-lg bg-gradient-to-br from-violet-500 to-purple-600 flex items-center justify-center shadow-md">
                  <svg
                    width="16"
                    height="16"
                    viewBox="0 0 20 20"
                    fill="none"
                    className="text-white"
                  >
                    <path
                      d="M10 2L16 8L10 14L4 8L10 2Z"
                      fill="currentColor"
                      opacity="0.9"
                    />
                    <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"
                    />
                    <path
                      d="M10 16L18 8M10 16L2 8"
                      stroke="currentColor"
                      strokeWidth="1.5"
                      opacity="0.6"
                      strokeLinecap="round"
                    />
                  </svg>
                </div>
              </div>
              <div>
                <h1 className="text-lg font-bold">AgentGraph</h1>
              </div>
            </div>
          )}
        </div>

        {/* Search Bar and Toggle */}
        {!isCollapsed ? (
          <div className="border-b border-slate-700">
            <div className="p-3 flex items-center gap-2">
              <div className="relative flex-1">
                <Search className="absolute left-2 top-1/2 transform -translate-y-1/2 text-slate-400 h-3.5 w-3.5" />
                <Input
                  type="text"
                  placeholder="Search traces, graphs..."
                  value={searchQuery}
                  onChange={(e) => setSearchQuery(e.target.value)}
                  className="pl-8 h-8 bg-slate-700/50 border-slate-600 text-white placeholder-slate-400 focus:bg-slate-700 text-xs"
                />
              </div>
              <Button
                variant="ghost"
                size="sm"
                onClick={onToggleSidebar}
                className="h-7 w-7 p-0 hover:bg-slate-700/50 text-slate-400 hover:text-white transition-colors flex-shrink-0"
                title="Collapse sidebar"
              >
                <PanelLeftClose className="h-3.5 w-3.5" />
              </Button>
            </div>
          </div>
        ) : (
          // Collapsed state - show expand button
          <div className="p-2 border-b border-slate-700">
            <Button
              variant="ghost"
              size="sm"
              onClick={onToggleSidebar}
              className="h-7 w-7 p-0 hover:bg-slate-700/50 text-slate-400 hover:text-white transition-colors mx-auto block"
              title="Expand sidebar"
            >
              <PanelLeftOpen className="h-3.5 w-3.5" />
            </Button>
          </div>
        )}

        {/* Navigation Groups */}
        <div className="flex-1 overflow-y-auto">
          <nav className="p-2">
            <ErrorBoundary>
              {navigationGroups.map((group) => (
                <div key={group.id} className="mb-3">
                  {!isCollapsed && (
                    <div className="flex items-center justify-between px-2 py-1 mb-1">
                      <span className="text-xs font-semibold text-slate-400 uppercase tracking-wide">
                        {group.label}
                      </span>
                      {group.isCollapsible && (
                        <Button
                          variant="ghost"
                          size="sm"
                          onClick={() => toggleGroup(group.id)}
                          className="h-3 w-3 p-0 text-slate-400 hover:text-white"
                        >
                          <ChevronRight
                            className={`h-2.5 w-2.5 transition-transform ${
                              expandedGroups.includes(group.id)
                                ? "rotate-90"
                                : ""
                            }`}
                          />
                        </Button>
                      )}
                    </div>
                  )}

                  <div
                    className={`space-y-1 ${
                      !isCollapsed && !expandedGroups.includes(group.id)
                        ? "hidden"
                        : ""
                    }`}
                  >
                    {group.items.map((item) => (
                      <Button
                        key={item.id}
                        onClick={item.onClick}
                        className={`${
                          isCollapsed
                            ? "w-10 h-10 p-0 justify-center mx-auto"
                            : "w-full justify-start gap-2 h-8 px-2"
                        } text-xs font-medium text-slate-200 hover:bg-slate-700/50 hover:text-white transition-colors duration-200 relative`}
                        variant="ghost"
                        title={isCollapsed ? item.label : undefined}
                      >
                        <item.icon className="h-3.5 w-3.5 flex-shrink-0" />
                        {!isCollapsed && (
                          <>
                            <span className="truncate">{item.label}</span>
                            {item.badge && (
                              <span className="ml-auto text-xs bg-slate-600/50 text-slate-200 px-1 py-0.5 rounded flex-shrink-0">
                                {item.badge}
                              </span>
                            )}
                          </>
                        )}
                      </Button>
                    ))}
                  </div>
                </div>
              ))}
            </ErrorBoundary>
          </nav>
        </div>

        {/* Bottom Section */}
        <div className="border-t border-slate-700 p-2">
          <div className={`${isCollapsed ? "space-y-1" : "space-y-1"}`}>
            {/* Notifications */}
            <DropdownMenu
              open={isNotificationsOpen}
              onOpenChange={setIsNotificationsOpen}
            >
              <DropdownMenuTrigger asChild>
                <Button
                  variant="ghost"
                  className={`${
                    isCollapsed
                      ? "w-10 h-10 p-0 justify-center mx-auto"
                      : "w-full justify-start gap-2 h-8 px-2"
                  } text-slate-300 hover:bg-slate-700/50 hover:text-yellow-400 transition-colors relative`}
                  title={isCollapsed ? "Notifications" : undefined}
                >
                  <Bell className="h-3.5 w-3.5 flex-shrink-0" />
                  {!isCollapsed && (
                    <span className="text-xs">Notifications</span>
                  )}
                  {hasUnreadNotifications && (
                    <Badge
                      variant="destructive"
                      className="absolute -top-1 -right-1 h-4 w-4 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) => (
                        <DropdownMenuItem
                          key={notification.id}
                          className="flex flex-col items-start p-4 hover:bg-muted/50"
                          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>
              </DropdownMenuContent>
            </DropdownMenu>

            {/* Help */}
            <Button
              variant="ghost"
              onClick={() => setIsHelpModalOpen(true)}
              className={`${
                isCollapsed
                  ? "w-10 h-10 p-0 justify-center mx-auto"
                  : "w-full justify-start gap-2 h-8 px-2"
              } text-slate-300 hover:bg-slate-700/50 hover:text-white transition-colors`}
              title={isCollapsed ? "Help" : undefined}
            >
              <HelpCircle className="h-3.5 w-3.5 flex-shrink-0" />
              {!isCollapsed && <span className="text-xs">Help</span>}
            </Button>

            {/* Settings */}
            <Button
              variant="ghost"
              onClick={() => setIsSettingsModalOpen(true)}
              className={`${
                isCollapsed
                  ? "w-10 h-10 p-0 justify-center mx-auto"
                  : "w-full justify-start gap-2 h-8 px-2"
              } text-slate-300 hover:bg-slate-700/50 hover:text-white transition-colors`}
              title={isCollapsed ? "Settings" : undefined}
            >
              <Settings className="h-3.5 w-3.5 flex-shrink-0" />
              {!isCollapsed && <span className="text-xs">Settings</span>}
            </Button>

            {/* User Menu */}
            <DropdownMenu>
              <DropdownMenuTrigger asChild>
                <Button
                  variant="ghost"
                  className={`${
                    isCollapsed
                      ? "w-10 h-10 p-0 justify-center mx-auto"
                      : "w-full justify-start gap-2 h-8 px-2"
                  } text-slate-300 hover:bg-slate-700/50 hover:text-white transition-colors`}
                  title={isCollapsed ? "User Menu" : undefined}
                >
                  <div className="h-5 w-5 rounded-full bg-gradient-to-br from-indigo-500 to-purple-600 flex items-center justify-center flex-shrink-0">
                    <User className="h-2.5 w-2.5 text-white" />
                  </div>
                  {!isCollapsed && (
                    <>
                      <span className="text-xs">User</span>
                      <ChevronDown className="h-2.5 w-2.5 ml-auto" />
                    </>
                  )}
                </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>
      </aside>

      {/* Modals and Dialogs */}
      <WelcomeGuideModal
        open={isHelpModalOpen}
        onOpenChange={setIsHelpModalOpen}
      />
      <SettingsModal
        open={isSettingsModalOpen}
        onOpenChange={setIsSettingsModalOpen}
      />
    </>
  );
}