import { NextRequest, NextResponse } from 'next/server';
import { recordHttpRequest } from '@/lib/metrics';

// Force Node.js runtime
export const runtime = 'nodejs';

interface TrackPayload {
  method: string;
  path: string;
  statusCode: number;
  durationMs: number;
}

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

    const { method, path, statusCode, durationMs } = body;

    if (!method || !path || statusCode === undefined || durationMs === undefined) {
      return NextResponse.json(
        { error: 'Missing required fields' },
        { status: 400 }
      );
    }

    recordHttpRequest(method, path, statusCode, durationMs);

    return NextResponse.json({ success: true }, { status: 200 });
  } catch (error) {
    console.error('Track error:', error);
    return NextResponse.json(
      { error: 'Failed to track request' },
      { status: 500 }
    );
  }
}
