import { NextRequest, NextResponse } from 'next/server';
import prisma from '../../../lib/prisma';

export async function POST(request: NextRequest) {
  console.log('>>> ANALYTICS API HIT');
  try {
    const body = await request.json();
    console.log('>>> PAYLOAD:', JSON.stringify(body));
    const { type, path, label, value, metadata } = body;

    if (!type || !path) {
      return NextResponse.json({ error: 'Type et Path requis' }, { status: 400 });
    }

    const event = await prisma.analyticsEvent.create({
      data: {
        type,
        path,
        label: label || null,
        value: value || null,
        metadata: metadata || {},
      },
    });

    return NextResponse.json({ success: true, id: event.id });
  } catch (error) {
    console.error('Error recording analytics event:', error);
    return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 });
  }
}
