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 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useEffect } from 'react';
import {
  User,
  Files,
  Database,
  GitPullRequest,
  Heart,
  Globe,
  Lock,
  Calendar,
  CheckCircle2,
  Clock,
  ArrowRight,
  ExternalLink,
  Search,
  Filter,
  Activity,
  FileText,
  Shield,
  Layers,
  Sparkles,
  ArrowLeft,
  X,
  Share2,
  Copy
} from 'lucide-react';
import { api } from '../api/client';

export default function UserProfileView({
  userIdOrUsername,
  onClose,
  onSelectWorkspace,
  currentUser
}) {
  const [profileData, setProfileData] = useState(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState('');
  const [activeTab, setActiveTab] = useState('workspaces'); // 'workspaces' | 'activity'
  const [workspaceSearch, setWorkspaceSearch] = useState('');
  const [activityFilter, setActivityFilter] = useState('ALL');
  const [copiedId, setCopiedId] = useState(false);
  const [requestingAccess, setRequestingAccess] = useState({});

  useEffect(() => {
    if (userIdOrUsername) {
      loadProfile(userIdOrUsername);
    }
  }, [userIdOrUsername]);

  const loadProfile = async (target) => {
    setLoading(true);
    setError('');
    try {
      const data = await api.getUserProfile(target);
      setProfileData(data);
    } catch (err) {
      setError(`Could not load profile: ${err.message}`);
    } finally {
      setLoading(false);
    }
  };

  const handleCopyId = () => {
    if (!profileData?.user?.id) return;
    navigator.clipboard.writeText(profileData.user.id);
    setCopiedId(true);
    setTimeout(() => setCopiedId(false), 2000);
  };

  const handleRequestAccess = async (workspaceId) => {
    setRequestingAccess((prev) => ({ ...prev, [workspaceId]: true }));
    try {
      await api.requestWorkspaceCollab(workspaceId, 'REQUESTED_COLLAB');
      // Update local state to show pending
      setProfileData((prev) => {
        if (!prev) return prev;
        return {
          ...prev,
          workspaces: prev.workspaces.map((w) =>
            w.id === workspaceId ? { ...w, viewer_status: 'PENDING' } : w
          ),
        };
      });
    } catch (err) {
      alert(`Could not request access: ${err.message || err}`);
    } finally {
      setRequestingAccess((prev) => ({ ...prev, [workspaceId]: false }));
    }
  };

  if (loading) {
    return (
      <div className="max-w-6xl mx-auto px-4 py-16 flex flex-col items-center justify-center space-y-4">
        <div className="w-10 h-10 border-2 border-emerald-500 border-t-transparent rounded-full animate-spin" />
        <p className="text-xs text-slate-400 font-mono">Loading user profile & workspace history...</p>
      </div>
    );
  }

  if (error || !profileData?.user) {
    return (
      <div className="max-w-xl mx-auto my-12 p-8 rounded-2xl bg-[#090D16] border border-slate-800 text-center space-y-4">
        <User className="w-10 h-10 text-slate-500 mx-auto" />
        <h3 className="text-sm font-bold text-white">Profile Not Found</h3>
        <p className="text-xs text-slate-400">{error || 'This user does not exist or has no public profile.'}</p>
        {onClose && (
          <button
            onClick={onClose}
            className="px-4 py-2 rounded-xl bg-slate-800 text-xs text-slate-200 hover:text-white transition"
          >
            Go Back
          </button>
        )}
      </div>
    );
  }

  const { user, stats, workspaces = [], activities = [] } = profileData;

  // Filter workspaces
  const filteredWorkspaces = workspaces.filter((w) => {
    const q = workspaceSearch.toLowerCase().trim();
    return !q || w.name.toLowerCase().includes(q) || (w.description || '').toLowerCase().includes(q);
  });

  // Filter activities
  const filteredActivities = activities.filter((act) => {
    if (activityFilter === 'ALL') return true;
    if (activityFilter === 'DOCUMENTS') return act.type === 'DOCUMENT_STAGED';
    if (activityFilter === 'PR_REVIEWS') return act.type === 'PR_REVIEW';
    if (activityFilter === 'FACTS') return act.type === 'FACT_EXTRACTED';
    return true;
  });

  return (
    <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8 space-y-8 animate-in fade-in duration-200">
      {/* Top Navigation / Breadcrumb */}
      {onClose && (
        <div className="flex items-center justify-between pb-2 border-b border-slate-800/80">
          <button
            onClick={onClose}
            className="text-xs text-slate-400 hover:text-white transition flex items-center space-x-1.5 font-semibold"
          >
            <ArrowLeft className="w-4 h-4" />
            <span>Back to Explorer</span>
          </button>
          <span className="text-[11px] text-slate-500 font-mono">Public User Profile</span>
        </div>
      )}

      {/* GitHub-style Header Profile Card */}
      <div className="bg-[#090D16] border border-slate-800 rounded-3xl p-6 sm:p-8 relative overflow-hidden shadow-2xl">
        {/* Ambient Gradient Glow */}
        <div className="absolute top-0 right-0 w-80 h-80 bg-gradient-to-br from-emerald-500/10 via-sky-500/5 to-transparent rounded-full blur-3xl pointer-events-none" />

        <div className="flex flex-col md:flex-row md:items-center justify-between gap-6 relative z-10">
          {/* User Info */}
          <div className="flex items-start space-x-5">
            <div className="w-20 h-20 sm:w-24 sm:h-24 rounded-2xl bg-gradient-to-tr from-emerald-500 via-teal-500 to-sky-500 p-[2px] shadow-xl shrink-0">
              <div className="w-full h-full bg-[#060910] rounded-[14px] flex items-center justify-center text-3xl font-black text-emerald-400 font-mono">
                {(user.username || user.email || 'U')[0].toUpperCase()}
              </div>
            </div>

            <div className="space-y-1.5">
              <div className="flex flex-wrap items-center gap-2.5">
                <h1 className="text-xl sm:text-2xl font-bold text-white tracking-tight">
                  {user.username || 'Collaborator'}
                </h1>
                <span className="px-2.5 py-0.5 rounded-full text-[10px] font-mono font-bold uppercase bg-emerald-500/10 text-emerald-400 border border-emerald-500/30">
                  {user.role || 'USER'}
                </span>
                {user.is_self && (
                  <span className="px-2 py-0.5 rounded text-[10px] font-mono bg-sky-500/10 text-sky-400 border border-sky-500/30">
                    YOU
                  </span>
                )}
              </div>

              <p className="text-xs text-slate-400 font-mono">{user.email}</p>

              <div className="flex items-center space-x-3 text-[11px] text-slate-500 font-mono pt-1">
                <button
                  onClick={handleCopyId}
                  className="hover:text-slate-300 transition flex items-center space-x-1"
                  title="Copy User ID"
                >
                  <Copy className="w-3 h-3 text-slate-500" />
                  <span>ID: {user.id.slice(0, 8)}...</span>
                  {copiedId && <span className="text-emerald-400 font-bold">&check;</span>}
                </button>
                <span>&bull;</span>
                <span className="flex items-center space-x-1">
                  <Globe className="w-3 h-3 text-slate-500" />
                  <span>Public Contributor</span>
                </span>
              </div>
            </div>
          </div>

          {/* Quick Metrics Bar */}
          <div className="grid grid-cols-2 sm:grid-cols-4 gap-3 bg-[#0D121F] border border-slate-800/80 rounded-2xl p-4 shrink-0">
            <div className="text-center px-3">
              <span className="block text-xl font-extrabold text-white font-mono">
                {stats.total_workspaces}
              </span>
              <span className="text-[10px] uppercase font-mono text-slate-400 tracking-wider">
                Workspaces
              </span>
            </div>
            <div className="text-center px-3 border-l border-slate-800">
              <span className="block text-xl font-extrabold text-sky-400 font-mono">
                {stats.total_documents}
              </span>
              <span className="text-[10px] uppercase font-mono text-slate-400 tracking-wider">
                Documents
              </span>
            </div>
            <div className="text-center px-3 border-l border-slate-800">
              <span className="block text-xl font-extrabold text-emerald-400 font-mono">
                {stats.total_facts}
              </span>
              <span className="text-[10px] uppercase font-mono text-slate-400 tracking-wider">
                Facts Verified
              </span>
            </div>
            <div className="text-center px-3 border-l border-slate-800">
              <span className="block text-xl font-extrabold text-violet-400 font-mono">
                {stats.total_reviews}
              </span>
              <span className="text-[10px] uppercase font-mono text-slate-400 tracking-wider">
                PR Reviews
              </span>
            </div>
          </div>
        </div>
      </div>

      {/* Main Tabs Navigation */}
      <div className="flex border-b border-slate-800 text-xs font-semibold">
        <button
          onClick={() => setActiveTab('workspaces')}
          className={`pb-3 px-4 border-b-2 transition flex items-center space-x-2 ${
            activeTab === 'workspaces'
              ? 'border-emerald-400 text-white'
              : 'border-transparent text-slate-400 hover:text-slate-200'
          }`}
        >
          <Layers className="w-4 h-4 text-emerald-400" />
          <span>Workspaces & Repositories</span>
          <span className="px-2 py-0.2 rounded-full bg-slate-800 text-slate-300 font-mono text-[10px]">
            {workspaces.length}
          </span>
        </button>

        <button
          onClick={() => setActiveTab('activity')}
          className={`pb-3 px-4 border-b-2 transition flex items-center space-x-2 ${
            activeTab === 'activity'
              ? 'border-emerald-400 text-white'
              : 'border-transparent text-slate-400 hover:text-slate-200'
          }`}
        >
          <Activity className="w-4 h-4 text-violet-400" />
          <span>Activity & Workspace Details</span>
          <span className="px-2 py-0.2 rounded-full bg-slate-800 text-slate-300 font-mono text-[10px]">
            {activities.length}
          </span>
        </button>
      </div>

      {/* TAB 1: WORKSPACES */}
      {activeTab === 'workspaces' && (
        <div className="space-y-4">
          {/* Search bar inside workspaces */}
          <div className="flex items-center justify-between gap-3">
            <div className="relative flex-1 max-w-sm">
              <Search className="w-3.5 h-3.5 text-slate-500 absolute left-3 top-1/2 -translate-y-1/2" />
              <input
                type="text"
                placeholder="Find a workspace..."
                value={workspaceSearch}
                onChange={(e) => setWorkspaceSearch(e.target.value)}
                className="w-full bg-[#090D16] border border-slate-800 rounded-xl pl-9 pr-3 py-1.5 text-xs text-white placeholder-slate-500 focus:outline-none focus:border-emerald-500 font-mono"
              />
            </div>
            <span className="text-[11px] text-slate-500 font-mono">
              Showing {filteredWorkspaces.length} of {workspaces.length} workspaces
            </span>
          </div>

          {filteredWorkspaces.length === 0 ? (
            <div className="py-12 text-center text-xs text-slate-500 border border-dashed border-slate-800 rounded-2xl">
              No matching workspaces found.
            </div>
          ) : (
            <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
              {filteredWorkspaces.map((ws) => (
                <div
                  key={ws.id}
                  className="bg-[#090D16] border border-slate-800/90 hover:border-slate-700 rounded-2xl p-5 space-y-3 transition group flex flex-col justify-between"
                >
                  <div className="space-y-2">
                    <div className="flex items-start justify-between gap-2">
                      <div className="flex items-center space-x-2 min-w-0">
                        <div className={`p-1.5 rounded-lg shrink-0 ${
                          ws.visibility === 'public'
                            ? 'bg-sky-500/10 text-sky-400 border border-sky-500/20'
                            : 'bg-amber-500/10 text-amber-400 border border-amber-500/20'
                        }`}>
                          {ws.visibility === 'public' ? (
                            <Globe className="w-4 h-4" />
                          ) : (
                            <Lock className="w-4 h-4" />
                          )}
                        </div>
                        <h3 className="font-bold text-sm text-white group-hover:text-emerald-300 transition truncate">
                          {ws.name}
                        </h3>
                      </div>

                      <div className="flex items-center space-x-1.5 shrink-0">
                        <span className={`px-2 py-0.5 rounded text-[9px] font-mono font-bold uppercase ${
                          ws.visibility === 'public'
                            ? 'bg-sky-500/10 text-sky-400 border border-sky-500/20'
                            : 'bg-amber-500/10 text-amber-400 border border-amber-500/20'
                        }`}>
                          {ws.visibility}
                        </span>
                        <span className="px-1.5 py-0.5 rounded text-[9px] font-mono bg-slate-800 text-slate-400">
                          {ws.user_role}
                        </span>
                      </div>
                    </div>

                    <p className="text-xs text-slate-400 line-clamp-2 leading-relaxed">
                      {ws.description || 'Deterministic document intelligence and master knowledge register.'}
                    </p>
                  </div>

                  <div className="pt-3 border-t border-slate-800/80 flex items-center justify-between text-[11px] text-slate-400 font-mono">
                    <div className="flex items-center space-x-3">
                      <span className="flex items-center space-x-1">
                        <Files className="w-3.5 h-3.5 text-slate-500" />
                        <span>{ws.doc_count} docs</span>
                      </span>
                      <span>&bull;</span>
                      <span className="flex items-center space-x-1">
                        <Database className="w-3.5 h-3.5 text-emerald-400" />
                        <span>{ws.knowledge_count} facts</span>
                      </span>
                      {ws.pending_pr_count > 0 && (
                        <>
                          <span>&bull;</span>
                          <span className="text-amber-400 font-semibold">
                            {ws.pending_pr_count} PRs
                          </span>
                        </>
                      )}
                    </div>

                    {ws.is_accessible ? (
                      <button
                        onClick={() => onSelectWorkspace && onSelectWorkspace(ws)}
                        className="px-3 py-1 rounded-lg bg-emerald-600 hover:bg-emerald-500 text-white font-semibold text-xs transition flex items-center space-x-1"
                      >
                        <span>Open</span>
                        <ArrowRight className="w-3 h-3" />
                      </button>
                    ) : (
                      <div className="flex items-center space-x-2">
                        {ws.viewer_status === 'PENDING' ? (
                          <span className="text-[10px] text-amber-400 bg-amber-500/10 border border-amber-500/20 px-2 py-1 rounded-md font-mono">
                            Pending Approval
                          </span>
                        ) : (
                          <button
                            onClick={() => handleRequestAccess(ws.id)}
                            disabled={Boolean(requestingAccess[ws.id])}
                            className="px-2.5 py-1 rounded-lg bg-teal-600 hover:bg-teal-500 text-white font-semibold text-[11px] transition flex items-center space-x-1 disabled:opacity-50"
                          >
                            <span>{requestingAccess[ws.id] ? 'Requesting...' : 'Request Access'}</span>
                          </button>
                        )}
                      </div>
                    )}
                  </div>
                </div>
              ))}
            </div>
          )}
        </div>
      )}

      {/* TAB 2: DETAILED ACTIVITY TRAIL ("literally every detail about what they did in that workspace") */}
      {activeTab === 'activity' && (
        <div className="space-y-4">
          {/* Activity Filters */}
          <div className="flex flex-wrap items-center justify-between gap-3 text-xs">
            <div className="flex items-center space-x-1.5">
              <span className="text-slate-500 font-mono text-[11px] mr-1">Activity Type:</span>
              {[
                { id: 'ALL', label: 'All Activities' },
                { id: 'DOCUMENTS', label: 'Documents Staged' },
                { id: 'PR_REVIEWS', label: 'PR Reviews' },
                { id: 'FACTS', label: 'Facts Extracted' }
              ].map((filter) => (
                <button
                  key={filter.id}
                  onClick={() => setActivityFilter(filter.id)}
                  className={`px-2.5 py-1 rounded-lg text-[11px] font-semibold transition ${
                    activityFilter === filter.id
                      ? 'bg-emerald-500/10 text-emerald-300 border border-emerald-500/30'
                      : 'text-slate-400 hover:text-white bg-slate-900 border border-slate-800'
                  }`}
                >
                  {filter.label}
                </button>
              ))}
            </div>
            <span className="text-[11px] text-slate-500 font-mono">
              {filteredActivities.length} actions recorded
            </span>
          </div>

          {filteredActivities.length === 0 ? (
            <div className="py-12 text-center text-xs text-slate-500 border border-dashed border-slate-800 rounded-2xl">
              No matching activity recorded for this filter.
            </div>
          ) : (
            <div className="divide-y divide-slate-800/80 bg-[#090D16] border border-slate-800 rounded-2xl overflow-hidden">
              {filteredActivities.map((act) => {
                let badgeTheme = {
                  bg: 'bg-slate-800',
                  text: 'text-slate-300',
                  icon: Activity,
                  label: 'ACTIVITY'
                };
                if (act.type === 'DOCUMENT_STAGED') {
                  badgeTheme = {
                    bg: 'bg-blue-500/10 border-blue-500/30 text-blue-400',
                    icon: FileText,
                    label: 'DOCUMENT STAGED'
                  };
                } else if (act.type === 'PR_REVIEW') {
                  badgeTheme = {
                    bg: 'bg-violet-500/10 border-violet-500/30 text-violet-400',
                    icon: GitPullRequest,
                    label: 'PR REVIEW'
                  };
                } else if (act.type === 'FACT_EXTRACTED') {
                  badgeTheme = {
                    bg: 'bg-emerald-500/10 border-emerald-500/30 text-emerald-400',
                    icon: Sparkles,
                    label: 'FACT VERIFIED'
                  };
                }

                const IconComponent = badgeTheme.icon;

                return (
                  <div
                    key={act.id}
                    className="p-4 sm:p-5 flex flex-col sm:flex-row sm:items-center justify-between gap-3 hover:bg-[#0D121F] transition text-xs"
                  >
                    <div className="flex items-start space-x-3.5">
                      <div className={`p-2 rounded-xl border shrink-0 mt-0.5 ${badgeTheme.bg}`}>
                        <IconComponent className="w-4 h-4" />
                      </div>
                      <div className="space-y-1">
                        <div className="flex flex-wrap items-center gap-2">
                          <span className={`px-2 py-0.2 rounded text-[9px] font-mono font-bold border ${badgeTheme.bg}`}>
                            {badgeTheme.label}
                          </span>
                          <span className="text-slate-400 font-mono text-[11px]">in</span>
                          <span className="font-bold text-white font-mono">
                            {act.workspace_name}
                          </span>
                        </div>
                        <p className="text-slate-200 text-xs font-medium leading-relaxed">
                          {act.description}
                        </p>
                      </div>
                    </div>

                    <div className="flex items-center space-x-3 shrink-0 self-end sm:self-center font-mono text-[11px] text-slate-500">
                      <Clock className="w-3 h-3 text-slate-600" />
                      <span>{act.timestamp ? new Date(act.timestamp).toLocaleDateString() : 'recent'}</span>
                      {onSelectWorkspace && (
                        <button
                          onClick={() => onSelectWorkspace({ id: act.workspace_id, name: act.workspace_name })}
                          className="px-2 py-1 rounded bg-slate-800 hover:bg-slate-700 text-slate-300 hover:text-white transition flex items-center space-x-1"
                        >
                          <span>Go</span>
                          <ArrowRight className="w-3 h-3" />
                        </button>
                      )}
                    </div>
                  </div>
                );
              })}
            </div>
          )}
        </div>
      )}
    </div>
  );
}