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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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/**
 * Badge Definitions & Evaluation Engine for OmniRoute Gamification
 *
 * Defines 20+ built-in badges across 5 categories and evaluates unlock
 * criteria against user activity. All DB access goes through dynamic imports
 * to avoid circular dependencies.
 *
 * @module lib/gamification/badges
 */

import type { BadgeDefinition } from "../db/gamification";

// ─── Built-in Badge Definitions ──────────────────────────────────────────────

/**
 * All built-in badges shipped with OmniRoute.
 * Spread with `{ created_at: new Date().toISOString() }` when inserting.
 */
export const BUILTIN_BADGES: Omit<BadgeDefinition, "createdAt">[] = [
  // ── Token Usage (Milestone) ──────────────────────────────────────────────
  {
    id: "first-token",
    name: "First Token",
    description: "Made your first API request",
    icon: "sparkles",
    category: "usage",
    rarity: "common",
    criteria: JSON.stringify({ type: "action_count", action: "request", threshold: 1 }),
    hidden: 0,
  },
  {
    id: "token-consumer",
    name: "Token Consumer",
    description: "Made 1,000 API requests",
    icon: "zap",
    category: "usage",
    rarity: "uncommon",
    criteria: JSON.stringify({ type: "action_count", action: "request", threshold: 1000 }),
    hidden: 0,
  },
  {
    id: "token-machine",
    name: "Token Machine",
    description: "Made 10,000 API requests",
    icon: "cpu",
    category: "usage",
    rarity: "rare",
    criteria: JSON.stringify({ type: "action_count", action: "request", threshold: 10000 }),
    hidden: 0,
  },
  {
    id: "token-whale",
    name: "Token Whale",
    description: "Made 100,000 API requests",
    icon: "whale",
    category: "usage",
    rarity: "legendary",
    criteria: JSON.stringify({ type: "action_count", action: "request", threshold: 100000 }),
    hidden: 0,
  },

  // ── Token Sharing (Social) ───────────────────────────────────────────────
  {
    id: "generous",
    name: "Generous",
    description: "Shared 1,000 tokens with others",
    icon: "gift",
    category: "sharing",
    rarity: "common",
    criteria: JSON.stringify({ type: "action_count", action: "token_share", threshold: 1000 }),
    hidden: 0,
  },
  {
    id: "philanthropist",
    name: "Philanthropist",
    description: "Shared 10,000 tokens with others",
    icon: "heart",
    category: "sharing",
    rarity: "uncommon",
    criteria: JSON.stringify({ type: "action_count", action: "token_share", threshold: 10000 }),
    hidden: 0,
  },
  {
    id: "token-santa",
    name: "Token Santa",
    description: "Shared 100,000 tokens with others",
    icon: "santa",
    category: "sharing",
    rarity: "rare",
    criteria: JSON.stringify({ type: "action_count", action: "token_share", threshold: 100000 }),
    hidden: 0,
  },
  {
    id: "community-hero",
    name: "Community Hero",
    description: "Shared 1,000,000 tokens with others",
    icon: "trophy",
    category: "sharing",
    rarity: "legendary",
    criteria: JSON.stringify({
      type: "action_count",
      action: "token_share",
      threshold: 1000000,
    }),
    hidden: 0,
  },

  // ── Contribution (Achievement) ───────────────────────────────────────────
  {
    id: "explorer",
    name: "Explorer",
    description: "Used 5 different providers",
    icon: "compass",
    category: "contribution",
    rarity: "uncommon",
    criteria: JSON.stringify({ type: "unique_count", action: "provider", threshold: 5 }),
    hidden: 0,
  },
  {
    id: "polyglot",
    name: "Polyglot",
    description: "Used 10 different models",
    icon: "languages",
    category: "contribution",
    rarity: "rare",
    criteria: JSON.stringify({ type: "unique_count", action: "model", threshold: 10 }),
    hidden: 0,
  },
  {
    id: "architect",
    name: "Architect",
    description: "Created 3 combo routes",
    icon: "blocks",
    category: "contribution",
    rarity: "uncommon",
    criteria: JSON.stringify({ type: "action_count", action: "combo_create", threshold: 3 }),
    hidden: 0,
  },
  {
    id: "speedster",
    name: "Speedster",
    description: "Maintained <500ms avg latency for 100 requests",
    icon: "gauge",
    category: "contribution",
    rarity: "rare",
    criteria: JSON.stringify({
      type: "threshold",
      metric: "avg_latency",
      threshold: 500,
      window: 100,
    }),
    hidden: 0,
  },
  {
    id: "resilient",
    name: "Resilient",
    description: "100% uptime for 7 days",
    icon: "shield",
    category: "contribution",
    rarity: "rare",
    criteria: JSON.stringify({ type: "threshold", metric: "uptime", threshold: 100, window: 7 }),
    hidden: 0,
  },

  // ── Streak (Engagement) ──────────────────────────────────────────────────
  {
    id: "daily-user",
    name: "Daily User",
    description: "Active for 3 consecutive days",
    icon: "flame",
    category: "streak",
    rarity: "common",
    criteria: JSON.stringify({ type: "streak", threshold: 3 }),
    hidden: 0,
  },
  {
    id: "weekly-warrior",
    name: "Weekly Warrior",
    description: "Active for 7 consecutive days",
    icon: "sword",
    category: "streak",
    rarity: "uncommon",
    criteria: JSON.stringify({ type: "streak", threshold: 7 }),
    hidden: 0,
  },
  {
    id: "monthly-master",
    name: "Monthly Master",
    description: "Active for 30 consecutive days",
    icon: "crown",
    category: "streak",
    rarity: "rare",
    criteria: JSON.stringify({ type: "streak", threshold: 30 }),
    hidden: 0,
  },
  {
    id: "unstoppable",
    name: "Unstoppable",
    description: "Active for 365 consecutive days",
    icon: "infinity",
    category: "streak",
    rarity: "legendary",
    criteria: JSON.stringify({ type: "streak", threshold: 365 }),
    hidden: 0,
  },

  // ── Rare / Legendary ─────────────────────────────────────────────────────
  {
    id: "early-adopter",
    name: "Early Adopter",
    description: "Joined within the first month of gamification",
    icon: "rocket",
    category: "rare",
    rarity: "legendary",
    criteria: JSON.stringify({ type: "first", window_days: 30 }),
    hidden: 0,
  },
  {
    id: "bug-hunter",
    name: "Bug Hunter",
    description: "Reported 5 issues",
    icon: "bug",
    category: "rare",
    rarity: "rare",
    criteria: JSON.stringify({ type: "action_count", action: "issue_report", threshold: 5 }),
    hidden: 0,
  },
  {
    id: "contributor",
    name: "Contributor",
    description: "Merged 1 pull request",
    icon: "git-merge",
    category: "rare",
    rarity: "rare",
    criteria: JSON.stringify({ type: "action_count", action: "pr_merge", threshold: 1 }),
    hidden: 0,
  },
  {
    id: "community-leader",
    name: "Community Leader",
    description: "Reached top 10 on any leaderboard",
    icon: "medal",
    category: "rare",
    rarity: "rare",
    criteria: JSON.stringify({ type: "rank", threshold: 10 }),
    hidden: 0,
  },
  {
    id: "secret-badge",
    name: "???",
    description: "A hidden achievement awaits...",
    icon: "question",
    category: "rare",
    rarity: "legendary",
    criteria: JSON.stringify({ type: "hidden" }),
    hidden: 1,
  },
];

// ─── Criteria Types ──────────────────────────────────────────────────────────

interface ActionCountCriteria {
  type: "action_count";
  action: string;
  threshold: number;
}

interface StreakCriteria {
  type: "streak";
  threshold: number;
}

interface UniqueCountCriteria {
  type: "unique_count";
  action: string;
  threshold: number;
}

interface ThresholdCriteria {
  type: "threshold";
  metric: string;
  threshold: number;
  window?: number;
}

interface RankCriteria {
  type: "rank";
  threshold: number;
}

interface FirstCriteria {
  type: "first";
  window_days: number;
}

interface HiddenCriteria {
  type: "hidden";
}

type BadgeCriteria =
  | ActionCountCriteria
  | StreakCriteria
  | UniqueCountCriteria
  | ThresholdCriteria
  | RankCriteria
  | FirstCriteria
  | HiddenCriteria;

// ─── Helper: Action Count ────────────────────────────────────────────────────

/**
 * Get the total count of a specific action for an API key from the XP audit log.
 */
async function getActionCount(apiKeyId: string, action: string): Promise<number> {
  const { getDbInstance } = await import("../db/core");
  const db = getDbInstance();

  const row = db
    .prepare(
      `SELECT COALESCE(SUM(
        CASE WHEN metadata IS NOT NULL
          THEN CAST(json_extract(metadata, '$.amount') AS INTEGER)
          ELSE 1
        END
      ), 0) AS total
      FROM xp_audit_log
      WHERE api_key_id = ? AND action = ?`
    )
    .get(apiKeyId, action) as { total: number } | undefined;

  return row?.total ?? 0;
}

// ─── Helper: Unique Count ────────────────────────────────────────────────────

/**
 * Get the count of unique values for a given type (provider, model, etc.)
 * from the XP audit log metadata.
 */
async function getUniqueCount(apiKeyId: string, type: string): Promise<number> {
  const { getDbInstance } = await import("../db/core");
  const db = getDbInstance();

  const row = db
    .prepare(
      `SELECT COUNT(DISTINCT json_extract(metadata, '$.' || ?)) AS total
      FROM xp_audit_log
      WHERE api_key_id = ? AND metadata IS NOT NULL`
    )
    .get(type, apiKeyId) as { total: number } | undefined;

  return row?.total ?? 0;
}

// ─── Helper: Streak ──────────────────────────────────────────────────────────

/**
 * Get the current streak count for an API key.
 * Delegates to the streaks module to avoid duplication.
 */
async function getStreak(apiKeyId: string): Promise<number> {
  const { getStreak: fetchStreak } = await import("./streaks");
  const data = await fetchStreak(apiKeyId);
  return data.currentStreak;
}

// ─── Helper: Leaderboard Rank ────────────────────────────────────────────────

/**
 * Get the rank of an API key on the global leaderboard.
 * Rank = number of users with a higher score + 1.
 */
async function getRank(apiKeyId: string, scope: string): Promise<number> {
  const { getDbInstance } = await import("../db/core");
  const db = getDbInstance();

  const scoreRow = db
    .prepare("SELECT score FROM leaderboard WHERE api_key_id = ? AND scope = ?")
    .get(apiKeyId, scope) as { score: number } | undefined;

  if (!scoreRow) return Infinity;

  const rankRow = db
    .prepare("SELECT COUNT(*) AS rank FROM leaderboard WHERE scope = ? AND score > ?")
    .get(scope, scoreRow.score) as { rank: number } | undefined;

  return (rankRow?.rank ?? 0) + 1;
}

// ─── Badge Evaluation Engine ─────────────────────────────────────────────────

/**
 * Evaluate if an action triggers any badge unlocks.
 *
 * Iterates over all badge definitions, skips already-earned badges,
 * and checks each unearned badge's criteria against current user state.
 * Returns the list of newly unlocked badge IDs.
 *
 * @param apiKeyId - The API key to evaluate
 * @param action - The action that was just performed (e.g. "request", "token_share")
 * @param metadata - Optional context (provider, model, amount, etc.)
 * @returns Array of newly unlocked badge IDs
 */
export async function evaluateBadges(
  apiKeyId: string,
  action: string,
  metadata?: Record<string, unknown>
): Promise<string[]> {
  // Import DB functions dynamically to avoid circular deps
  const { getBadgeDefinitions, unlockBadge, getBadges } = await import("../db/gamification");

  const definitions = getBadgeDefinitions();
  const earned = getBadges(apiKeyId);
  const earnedIds = new Set(earned.map((b) => b.badgeId));
  const newlyUnlocked: string[] = [];

  for (const def of definitions) {
    if (earnedIds.has(def.id)) continue; // Already earned

    if (!def.criteria) continue;

    let criteria: BadgeCriteria;
    try {
      criteria = JSON.parse(def.criteria) as BadgeCriteria;
    } catch {
      continue; // Malformed criteria, skip
    }

    let unlocked = false;

    switch (criteria.type) {
      case "action_count": {
        if (criteria.action === action) {
          const count = await getActionCount(apiKeyId, action);
          unlocked = count >= criteria.threshold;
        }
        break;
      }

      case "streak": {
        const streak = await getStreak(apiKeyId);
        unlocked = streak >= criteria.threshold;
        break;
      }

      case "unique_count": {
        if (criteria.action === action || action === "request") {
          // Check on any qualifying action, not just exact match
          const uniqueCount = await getUniqueCount(apiKeyId, criteria.action);
          unlocked = uniqueCount >= criteria.threshold;
        }
        break;
      }

      case "threshold": {
        // Threshold badges are evaluated externally (e.g. latency, uptime)
        // and triggered via metadata
        if (metadata && typeof metadata[criteria.metric] === "number") {
          const value = metadata[criteria.metric] as number;
          if (criteria.metric === "avg_latency") {
            unlocked = value < criteria.threshold;
          } else {
            unlocked = value >= criteria.threshold;
          }
        }
        break;
      }

      case "rank": {
        const rank = await getRank(apiKeyId, "global");
        unlocked = rank <= criteria.threshold;
        break;
      }

      case "first": {
        // Time-limited badge: check if user joined within window
        const { getDbInstance } = await import("../db/core");
        const db = getDbInstance();

        const firstLog = db
          .prepare(`SELECT MIN(created_at) AS first_at FROM xp_audit_log WHERE api_key_id = ?`)
          .get(apiKeyId) as { first_at: string | null } | undefined;

        if (firstLog?.first_at) {
          const joinDate = new Date(firstLog.first_at);
          const windowEnd = new Date(joinDate);
          windowEnd.setDate(windowEnd.getDate() + (criteria as FirstCriteria).window_days);
          unlocked = new Date() <= windowEnd;
        }
        break;
      }

      case "hidden": {
        // Secret badge: unlocked by having earned all other badges
        const allOtherDefs = definitions.filter(
          (d) => d.id !== def.id && !JSON.parse(d.criteria).type?.toString().includes("hidden")
        );
        const allOtherEarned = allOtherDefs.every((d) => earnedIds.has(d.id));
        unlocked = allOtherEarned;
        break;
      }
    }

    if (unlocked) {
      unlockBadge(apiKeyId, def.id);
      newlyUnlocked.push(def.id);
    }
  }

  return newlyUnlocked;
}

/**
 * Seed built-in badge definitions into the database.
 * Idempotent β€” uses INSERT OR IGNORE so existing badges are not overwritten.
 */
export async function seedBuiltinBadges(): Promise<void> {
  const { getDbInstance } = await import("../db/core");
  const db = getDbInstance();

  const insert = db.prepare(
    `INSERT OR IGNORE INTO badge_definitions (id, name, description, icon, category, rarity, criteria, hidden)
     VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
  );

  const insertMany = db.transaction((badges: typeof BUILTIN_BADGES) => {
    for (const badge of badges) {
      insert.run(
        badge.id,
        badge.name,
        badge.description,
        badge.icon,
        badge.category,
        badge.rarity,
        badge.criteria,
        badge.hidden
      );
    }
  });

  insertMany(BUILTIN_BADGES);
}