import { NextResponse } from 'next/server';
import { enrichLeadWithEmail } from '@/lib/enrich';

export async function POST(request: Request) {
  try {
    const body = await request.json();
    const { leadId } = body;

    if (!leadId) {
      return NextResponse.json({ error: 'Lead ID is required' }, { status: 400 });
    }

    const result = await enrichLeadWithEmail(leadId);

    if (!result.success) {
      return NextResponse.json({ error: result.error || 'Enrichment failed' }, { status: 422 });
    }

    return NextResponse.json(result);
  } catch (error: any) {
    console.error('Enrichment API Error:', error);
    return NextResponse.json(
      { error: error.message || 'Failed to enrich lead' },
      { status: 500 }
    );
  }
}
