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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
<script lang="ts">
  import { onMount } from 'svelte';
  import { fade, fly } from 'svelte/transition';
  import type { ActivityEntry, PicletInstance, LeaderboardEntry } from '$lib/db/schema';
  import { db } from '$lib/db';
  import PullToRefresh from '../UI/PullToRefresh.svelte';
  import { CanonicalService } from '$lib/services/canonicalService';

  let activities: ActivityEntry[] = $state([]);
  let leaderboard: LeaderboardEntry[] = $state([]);
  let isLoading = $state(true);
  let isRefreshing = $state(false);
  let activeView: 'recent' | 'leaderboard' = $state('recent');

  // Mock data for development (will be replaced with server data)
  const mockActivities: ActivityEntry[] = [
    {
      id: 1,
      type: 'discovery',
      title: 'New Discovery!',
      description: 'Fraser discovered the first "Pillow" Piclet',
      picletTypeId: 'pillow_001',
      discovererName: 'Fraser',
      rarity: 'legendary',
      createdAt: new Date(Date.now() - 1000 * 60 * 5) // 5 minutes ago
    },
    {
      id: 2,
      type: 'variation',
      title: 'Variation Found',
      description: 'Alex found a "Velvet Pillow" variation',
      picletTypeId: 'pillow_002',
      discovererName: 'Alex',
      rarity: 'rare',
      createdAt: new Date(Date.now() - 1000 * 60 * 30) // 30 minutes ago
    },
    {
      id: 3,
      type: 'milestone',
      title: 'Milestone Reached!',
      description: 'Sam collected 100 unique Piclets!',
      picletTypeId: 'trophy',
      discovererName: 'Sam',
      rarity: 'epic',
      createdAt: new Date(Date.now() - 1000 * 60 * 60) // 1 hour ago
    }
  ];

  const mockLeaderboard: LeaderboardEntry[] = [
    { username: 'Fraser', totalDiscoveries: 156, uniqueDiscoveries: 45, rarityScore: 2340, rank: 1 },
    { username: 'Alex', totalDiscoveries: 134, uniqueDiscoveries: 38, rarityScore: 1890, rank: 2 },
    { username: 'Sam', totalDiscoveries: 98, uniqueDiscoveries: 31, rarityScore: 1560, rank: 3 },
    { username: 'Jordan', totalDiscoveries: 87, uniqueDiscoveries: 28, rarityScore: 1230, rank: 4 },
    { username: 'Casey', totalDiscoveries: 76, uniqueDiscoveries: 22, rarityScore: 980, rank: 5 }
  ];

  onMount(async () => {
    await loadActivityData();
  });

  async function loadActivityData() {
    isLoading = true;
    try {
      // Check if player has any discoveries
      const playerPiclets = await db.picletInstances.toArray();

      if (playerPiclets.length === 0) {
        // No discoveries yet - show empty state
        activities = [];
        leaderboard = [];
      } else {
        // TODO: Fetch from server once backend is ready
        // For now, use mock data
        activities = mockActivities;
        leaderboard = mockLeaderboard;

        // Load recent local discoveries
        const recentLocal = playerPiclets
          .filter(p => p.collectedAt)
          .sort((a, b) => (b.collectedAt?.getTime() || 0) - (a.collectedAt?.getTime() || 0))
          .slice(0, 5)
          .map((p, index) => ({
            id: 1000 + index,
            type: 'discovery' as const,
            title: p.isCanonical ? 'New Discovery!' : 'Variation Found',
            description: `You ${p.isCanonical ? 'discovered' : 'found a variation of'} "${p.objectName || p.typeId}"`,
            picletTypeId: p.typeId,
            discovererName: 'You',
            rarity: CanonicalService.calculateRarity(p.scanCount) as any,
            createdAt: p.collectedAt || new Date()
          }));

        // Merge with mock activities
        activities = [...recentLocal, ...activities].slice(0, 10);
      }
    } catch (error) {
      console.error('Error loading activity data:', error);
    }
    isLoading = false;
  }

  async function handleRefresh() {
    isRefreshing = true;
    try {
      await loadActivityData();
    } catch (error) {
      console.error('Error refreshing activity:', error);
    }
    isRefreshing = false;
  }

  function formatTime(date: Date): string {
    const now = Date.now();
    const diff = now - date.getTime();

    const minutes = Math.floor(diff / (1000 * 60));
    const hours = Math.floor(diff / (1000 * 60 * 60));
    const days = Math.floor(diff / (1000 * 60 * 60 * 24));

    if (minutes < 1) return 'just now';
    if (minutes < 60) return `${minutes}m ago`;
    if (hours < 24) return `${hours}h ago`;
    if (days < 7) return `${days}d ago`;

    return date.toLocaleDateString();
  }

  function getRarityColor(rarity: string): string {
    switch (rarity) {
      case 'legendary': return '#FFD700';
      case 'epic': return '#9B59B6';
      case 'rare': return '#3498DB';
      case 'uncommon': return '#2ECC71';
      default: return '#95A5A6';
    }
  }

  function getActivityIcon(type: string): string {
    switch (type) {
      case 'discovery': return '✨';
      case 'variation': return 'πŸ”„';
      case 'milestone': return 'πŸ†';
      default: return 'πŸ“';
    }
  }
</script>

<div class="activity-page">
  <div class="view-selector">
    <button
      class="view-tab"
      class:active={activeView === 'recent'}
      onclick={() => activeView = 'recent'}
    >
      Recent Activity
    </button>
    <button
      class="view-tab"
      class:active={activeView === 'leaderboard'}
      onclick={() => activeView = 'leaderboard'}
    >
      Leaderboard
    </button>
  </div>

  <PullToRefresh onRefresh={handleRefresh}>
    {#if isLoading}
      <div class="loading">
        <div class="spinner"></div>
        <p>Loading activity...</p>
      </div>
    {:else if activeView === 'recent'}
      {#if activities.length === 0}
        <div class="empty-state">
          <div class="empty-icon">πŸ”</div>
          <h2>No Discoveries Yet</h2>
          <p>Start scanning objects to discover Piclets!</p>
          <p class="hint">Every object in the world has a unique Piclet waiting to be discovered.</p>
        </div>
      {:else}
        <div class="activity-list">
          {#each activities as activity, index (activity.id)}
            <div
              class="activity-card"
              in:fly={{ y: 20, delay: index * 50 }}
            >
              <div class="activity-icon">
                <span class="type-icon">{getActivityIcon(activity.type)}</span>
              </div>

              <div class="activity-info">
                <h3>{activity.title}</h3>
                <p>{activity.description}</p>
                <div class="activity-meta">
                  <span
                    class="rarity-badge"
                    style="background-color: {getRarityColor(activity.rarity)}"
                  >
                    {activity.rarity}
                  </span>
                  <span class="time">{formatTime(activity.createdAt)}</span>
                </div>
              </div>
            </div>
          {/each}
        </div>
      {/if}
    {:else}
      {#if leaderboard.length === 0}
        <div class="empty-state">
          <div class="empty-icon">πŸ†</div>
          <h2>No Rankings Yet</h2>
          <p>Be the first to discover Piclets!</p>
        </div>
      {:else}
        <div class="leaderboard-list">
          {#each leaderboard as entry, index (entry.username)}
            <div
              class="leaderboard-card"
              in:fly={{ y: 20, delay: index * 50 }}
            >
              <div class="rank-badge" class:gold={entry.rank === 1} class:silver={entry.rank === 2} class:bronze={entry.rank === 3}>
                #{entry.rank}
              </div>

              <div class="player-info">
                <h3>{entry.username}</h3>
                <div class="stats">
                  <span>πŸ” {entry.uniqueDiscoveries} unique</span>
                  <span>πŸ“Š {entry.totalDiscoveries} total</span>
                  <span>⭐ {entry.rarityScore} points</span>
                </div>
              </div>
            </div>
          {/each}
        </div>
      {/if}
    {/if}
  </PullToRefresh>
</div>

<style>
  .activity-page {
    height: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  .view-selector {
    display: flex;
    gap: 0.5rem;
    padding: 1rem;
    background: #fff;
    border-bottom: 1px solid #e0e0e0;
  }

  .view-tab {
    flex: 1;
    padding: 0.75rem;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    background: #f5f5f5;
    color: #666;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
  }

  .view-tab.active {
    background: #007bff;
    color: white;
    border-color: #007bff;
  }

  .loading, .empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 60vh;
    text-align: center;
    padding: 1rem;
  }

  .spinner {
    width: 48px;
    height: 48px;
    border: 4px solid #f0f0f0;
    border-top-color: #007bff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 1rem;
  }

  @keyframes spin {
    to { transform: rotate(360deg); }
  }

  .empty-icon {
    font-size: 4rem;
    margin-bottom: 1rem;
  }

  .empty-state h2 {
    margin: 0 0 0.5rem;
    font-size: 1.25rem;
    color: #333;
  }

  .empty-state p {
    color: #666;
    font-size: 0.9rem;
    margin: 0.25rem 0;
  }

  .hint {
    font-style: italic;
    color: #999;
    margin-top: 1rem;
  }

  .activity-list, .leaderboard-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding: 1rem;
    padding-bottom: 5rem;
  }

  .activity-card, .leaderboard-card {
    display: flex;
    align-items: center;
    gap: 1rem;
    background: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 12px;
    padding: 1rem;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    transition: all 0.2s ease;
  }

  .activity-card:hover, .leaderboard-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
  }

  .activity-icon {
    width: 48px;
    height: 48px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f0f7ff;
    border-radius: 50%;
  }

  .type-icon {
    font-size: 1.5rem;
  }

  .activity-info, .player-info {
    flex: 1;
  }

  .activity-info h3, .player-info h3 {
    margin: 0 0 0.25rem;
    font-size: 1rem;
    font-weight: 600;
    color: #1a1a1a;
  }

  .activity-info p {
    margin: 0 0 0.5rem;
    font-size: 0.875rem;
    color: #666;
  }

  .activity-meta {
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }

  .rarity-badge {
    padding: 0.125rem 0.5rem;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 600;
    color: white;
    text-transform: uppercase;
  }

  .time {
    font-size: 0.75rem;
    color: #999;
  }

  .rank-badge {
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: #f0f0f0;
    font-weight: bold;
    font-size: 1.25rem;
    flex-shrink: 0;
  }

  .rank-badge.gold {
    background: linear-gradient(135deg, #FFD700, #FFA500);
    color: white;
  }

  .rank-badge.silver {
    background: linear-gradient(135deg, #C0C0C0, #808080);
    color: white;
  }

  .rank-badge.bronze {
    background: linear-gradient(135deg, #CD7F32, #8B4513);
    color: white;
  }

  .stats {
    display: flex;
    gap: 1rem;
    margin-top: 0.5rem;
    font-size: 0.875rem;
    color: #666;
  }

  .stats span {
    display: flex;
    align-items: center;
    gap: 0.25rem;
  }
</style>