import prisma from './prisma'
import { Prisma } from '@/generated/prisma/client'

export type AnalyticsEventType =
  | 'pledge_start'
  | 'amount_selected'
  | 'rail_selected'
  | 'identity_submitted'
  | 'pledge_completed'
  | 'instruction_copy_clicked'
  | 'i_paid_clicked'
  | 'payment_matched'

export async function trackEvent(
  eventType: AnalyticsEventType,
  data: {
    pledgeId?: string
    eventId?: string
    qrSourceId?: string
    metadata?: Record<string, unknown>
  }
) {
  try {
    await prisma.analyticsEvent.create({
      data: {
        eventType,
        pledgeId: data.pledgeId,
        eventId: data.eventId,
        qrSourceId: data.qrSourceId,
        metadata: (data.metadata || {}) as Prisma.InputJsonValue,
      },
    })
  } catch (error) {
    console.error('Analytics tracking error:', error)
    // Never throw - analytics should not break the flow
  }
}
