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
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
/**
 * Analytics Interaction Tracking API
 * POST /api/analytics/interaction - Record interactions (clicks, scrolls, custom events)
 *
 * Security Features:
 * - Origin/Referer validation (primary defense - browser-enforced)
 * - Rate limiting per IP (500 requests/minute for interactions)
 * - Bot detection (blocks automated tools)
 * - Anomaly detection (SQL injection, suspicious patterns)
 *
 * Note: Same-origin hosting provides stronger security than Google Analytics.
 * Tokens not required due to browser CORS protections.
 */

import { NextRequest, NextResponse } from 'next/server';
import { getSQLiteAdapter } from '@/lib/vfs/adapters/server';
import {
  interactionRateLimiter,
  RATE_LIMIT_CONFIG,
  getIdentifier,
} from '@/lib/analytics/rate-limiter';
import {
  validateOrigin,
  getAllowedOrigins,
  isLikelyBot,
  isSuspiciousRequest,
} from '@/lib/analytics/security';

interface InteractionData {
  deploymentId: string;
  pagePath: string;
  interactionType: 'click' | 'scroll' | 'exit' | 'custom';
  elementSelector?: string;
  coordinates?: {
    x: number;
    y: number;
    scrollY?: number;
    viewportWidth?: number;
    viewportHeight?: number;
    documentHeight?: number;
  };
  scrollDepth?: number;
  timeOnPage?: number;
  customData?: Record<string, unknown>;
  userAgent?: string;
  // token field removed - origin validation provides sufficient security
}

interface BatchInteractionData {
  batch: boolean;
  interactions: InteractionData[];
}

export async function POST(request: NextRequest) {
  try {
    const body: InteractionData | BatchInteractionData = await request.json();

    // Check if this is a batch request
    if ('batch' in body && body.batch === true) {
      return handleBatchInteractions(request, body);
    }

    // Handle single interaction (backward compatibility)
    const {
      deploymentId,
      pagePath,
      interactionType,
      elementSelector,
      coordinates,
      scrollDepth,
      timeOnPage,
      userAgent,
    } = body as InteractionData;

    // 1. Rate Limiting Check (higher limit for interactions)
    const identifier = getIdentifier(request);
    const rateLimitAllowed = interactionRateLimiter.check(
      identifier,
      RATE_LIMIT_CONFIG.interaction
    );

    if (!rateLimitAllowed) {
      const resetTime = interactionRateLimiter.getResetTime(
        identifier,
        RATE_LIMIT_CONFIG.interaction
      );

      return NextResponse.json(
        { error: 'Rate limit exceeded' },
        {
          status: 429,
          headers: {
            'Retry-After': resetTime.toString(),
            'X-RateLimit-Limit': RATE_LIMIT_CONFIG.interaction.limit.toString(),
            'X-RateLimit-Remaining': '0',
          },
        }
      );
    }

    // 2. Validate required fields
    if (!deploymentId || !pagePath || !interactionType) {
      return NextResponse.json(
        { error: 'Missing required fields: deploymentId, pagePath, interactionType' },
        { status: 400 }
      );
    }

    // 3. Anomaly Detection
    if (isSuspiciousRequest({ pagePath, userAgent })) {
      console.warn('[Analytics Interaction] Suspicious request detected:', {
        deploymentId,
        pagePath,
        ip: identifier,
      });
      return NextResponse.json(
        { error: 'Invalid request' },
        { status: 400 }
      );
    }

    // 4. Bot Detection
    if (userAgent && isLikelyBot(userAgent)) {
      return NextResponse.json({ success: true });
    }

    const adapter = getSQLiteAdapter();
    await adapter.init();

    // 5. Verify deployment exists (from core database)
    const deployment = await adapter.getDeployment(deploymentId);
    if (!deployment) {
      return NextResponse.json(
        { error: 'Deployment not found' },
        { status: 404 }
      );
    }

    // 6. Check if analytics is enabled
    if (!deployment.analytics.enabled || deployment.analytics.provider !== 'builtin') {
      return NextResponse.json(
        { error: 'Built-in analytics not enabled for this deployment' },
        { status: 403 }
      );
    }

    // 6b. Check if deployment database is enabled (created when deployment is published)
    const deploymentDb = adapter.getAnalyticsDatabaseInstance(deploymentId);
    if (!deploymentDb) {
      return NextResponse.json(
        { error: 'Deployment database not enabled' },
        { status: 404 }
      );
    }

    // 7. Check if specific feature is enabled
    const features = deployment.analytics.features || {};
    if (interactionType === 'click' && !features.heatmaps) {
      return NextResponse.json(
        { error: 'Heatmaps feature not enabled' },
        { status: 403 }
      );
    }

    if (interactionType === 'scroll' && !features.engagementTracking && !features.heatmaps) {
      return NextResponse.json(
        { error: 'Engagement tracking not enabled' },
        { status: 403 }
      );
    }

    if (interactionType === 'exit' && !features.engagementTracking) {
      return NextResponse.json(
        { error: 'Engagement tracking not enabled' },
        { status: 403 }
      );
    }

    // 8. CORS/Origin Validation (Primary Security Layer)
    const allowedOrigins = getAllowedOrigins(deploymentId, deployment.customDomain);
    if (!validateOrigin(request, allowedOrigins)) {
      console.warn('[Analytics Interaction] Invalid origin (rejected):', {
        origin: request.headers.get('origin'),
        referer: request.headers.get('referer'),
        allowedOrigins,
        deploymentId,
        ip: identifier,
      });
      return NextResponse.json(
        { error: 'Origin not allowed' },
        { status: 403 }
      );
    }

    // Generate session ID
    const sessionId = generateSessionId(userAgent || request.headers.get('user-agent') || '', request);

    // Normalize path for consistent tracking
    const normalizedPath = normalizePath(pagePath);

    // Record interaction using DeploymentDatabase
    deploymentDb.recordInteraction({
      sessionId,
      pagePath: normalizedPath,
      interactionType,
      elementSelector,
      coordinates: coordinates ? {
        x: coordinates.x,
        y: coordinates.y,
        scrollY: coordinates.scrollY,
        viewportWidth: coordinates.viewportWidth,
        viewportHeight: coordinates.viewportHeight,
        documentHeight: coordinates.documentHeight,
      } : undefined,
      scrollDepth,
      timeOnPage,
    });

    return NextResponse.json({ success: true });
  } catch (error) {
    console.error('[Analytics Interaction API] Error:', error);
    return NextResponse.json(
      { error: 'Failed to track interaction' },
      { status: 500 }
    );
  }
}

/**
 * Generate anonymous session ID (same logic as pageview tracking)
 */
function generateSessionId(userAgent: string, request: NextRequest): string {
  const forwarded = request.headers.get('x-forwarded-for');
  const ip = forwarded ? forwarded.split(',')[0] : '';
  const anonymizedIP = anonymizeIP(ip);

  const fingerprint = `${userAgent}|${anonymizedIP}|${new Date().toDateString()}`;

  let hash = 0;
  for (let i = 0; i < fingerprint.length; i++) {
    const char = fingerprint.charCodeAt(i);
    hash = ((hash << 5) - hash) + char;
    hash = hash & hash;
  }

  return Math.abs(hash).toString(36);
}

function anonymizeIP(ip: string): string {
  if (!ip) return '';

  if (ip.includes(':')) {
    const parts = ip.split(':');
    return parts.slice(0, 4).join(':') + '::';
  } else {
    const parts = ip.split('.');
    return parts.slice(0, 2).join('.') + '.0.0';
  }
}

/**
 * Normalize page path for consistent tracking
 */
function normalizePath(path: string): string {
  if (!path || path === '/') return '/index.html';

  // Remove trailing slash
  let normalized = path.replace(/\/$/, '');

  // If path doesn't have an extension, it's likely a directory - add /index.html
  if (!normalized.includes('.') || normalized.split('/').pop()?.indexOf('.') === -1) {
    normalized += '/index.html';
  }

  return normalized;
}

/**
 * Handle batch interaction tracking
 * Processes multiple interactions in a single request for improved performance
 */
async function handleBatchInteractions(
  request: NextRequest,
  body: BatchInteractionData
): Promise<NextResponse> {
  const { interactions } = body;

  if (!interactions || interactions.length === 0) {
    return NextResponse.json(
      { error: 'No interactions provided in batch' },
      { status: 400 }
    );
  }

  // Validate batch size (max 100 events per batch)
  if (interactions.length > 100) {
    return NextResponse.json(
      { error: 'Batch size exceeds maximum of 100 interactions' },
      { status: 400 }
    );
  }

  // 1. Rate Limiting Check - count as single request with batch multiplier
  const identifier = getIdentifier(request);
  const rateLimitAllowed = interactionRateLimiter.check(
    identifier,
    RATE_LIMIT_CONFIG.interaction
  );

  if (!rateLimitAllowed) {
    const resetTime = interactionRateLimiter.getResetTime(
      identifier,
      RATE_LIMIT_CONFIG.interaction
    );

    return NextResponse.json(
      { error: 'Rate limit exceeded' },
      {
        status: 429,
        headers: {
          'Retry-After': resetTime.toString(),
          'X-RateLimit-Limit': RATE_LIMIT_CONFIG.interaction.limit.toString(),
          'X-RateLimit-Remaining': '0',
        },
      }
    );
  }

  // 2. Extract common fields from first interaction for validation
  const firstInteraction = interactions[0];
  const { deploymentId, userAgent } = firstInteraction;

  if (!deploymentId) {
    return NextResponse.json(
      { error: 'Missing required field: deploymentId' },
      { status: 400 }
    );
  }

  // 3. Bot Detection
  if (userAgent && isLikelyBot(userAgent)) {
    return NextResponse.json({ success: true });
  }

  const adapter = getSQLiteAdapter();
  await adapter.init();

  try {
    // 4. Verify deployment exists (from core database)
    const deployment = await adapter.getDeployment(deploymentId);
    if (!deployment) {
      return NextResponse.json(
        { error: 'Deployment not found' },
        { status: 404 }
      );
    }

    // 5. Check if analytics is enabled
    if (!deployment.analytics.enabled || deployment.analytics.provider !== 'builtin') {
      return NextResponse.json(
        { error: 'Built-in analytics not enabled for this deployment' },
        { status: 403 }
      );
    }

    // 5b. Check if deployment database is enabled (created when deployment is published)
    const deploymentDb = adapter.getAnalyticsDatabaseInstance(deploymentId);
    if (!deploymentDb) {
      return NextResponse.json(
        { error: 'Deployment database not enabled' },
        { status: 404 }
      );
    }

    // 6. CORS/Origin Validation
    const allowedOrigins = getAllowedOrigins(deploymentId, deployment.customDomain);
    if (!validateOrigin(request, allowedOrigins)) {
      console.warn('[Analytics Batch] Invalid origin (rejected):', {
        origin: request.headers.get('origin'),
        referer: request.headers.get('referer'),
        allowedOrigins,
        deploymentId,
        ip: identifier,
      });
      return NextResponse.json(
        { error: 'Origin not allowed' },
        { status: 403 }
      );
    }

    // 7. Process all interactions
    const defaultUserAgent = request.headers.get('user-agent') || '';

    let successCount = 0;
    let skipCount = 0;

    for (const interaction of interactions) {
      const {
        pagePath,
        interactionType,
        elementSelector,
        coordinates,
        scrollDepth,
        timeOnPage,
        userAgent: interactionUserAgent,
      } = interaction;

      // Validate each interaction
      if (!pagePath || !interactionType) {
        skipCount++;
        continue;
      }

      // Check feature flags for this interaction type
      const features = deployment.analytics.features || {};
      if (interactionType === 'click' && !features.heatmaps) {
        skipCount++;
        continue;
      }

      if (interactionType === 'scroll' && !features.engagementTracking && !features.heatmaps) {
        skipCount++;
        continue;
      }

      if (interactionType === 'exit' && !features.engagementTracking) {
        skipCount++;
        continue;
      }

      // Anomaly detection per interaction
      if (isSuspiciousRequest({ pagePath, userAgent: interactionUserAgent })) {
        skipCount++;
        continue;
      }

      // Generate session ID
      const sessionId = generateSessionId(
        interactionUserAgent || defaultUserAgent,
        request
      );

      // Normalize path
      const normalizedPath = normalizePath(pagePath);

      // Record interaction
      try {
        deploymentDb.recordInteraction({
          sessionId,
          pagePath: normalizedPath,
          interactionType,
          elementSelector,
          coordinates: coordinates ? {
            x: coordinates.x,
            y: coordinates.y,
            scrollY: coordinates.scrollY,
            viewportWidth: coordinates.viewportWidth,
            viewportHeight: coordinates.viewportHeight,
            documentHeight: coordinates.documentHeight,
          } : undefined,
          scrollDepth,
          timeOnPage,
        });
        successCount++;
      } catch (error) {
        console.error('[Analytics Batch] Error inserting interaction:', error);
        skipCount++;
      }
    }

    return NextResponse.json({
      success: true,
      processed: successCount,
      skipped: skipCount,
      total: interactions.length,
    });
  } catch (error) {
    console.error('[Analytics Batch] Error processing batch:', error);
    return NextResponse.json(
      { error: 'Failed to process batch interactions' },
      { status: 500 }
    );
  }
}