import { NextResponse } from "next/server" import { fetchMutation } from "convex/nextjs" import { api } from "@/convex/_generated/api" import { requirePhoneAgentInternalAuth } from "@/app/api/internal/phone-calls/shared" export async function POST(request: Request) { const authError = await requirePhoneAgentInternalAuth(request) if (authError) { return authError } try { const body = await request.json() const result = await fetchMutation(api.voiceSessions.linkPhoneCallLead, { sessionId: body.sessionId, linkedLeadId: body.linkedLeadId ? String(body.linkedLeadId) : undefined, leadOutcome: body.leadOutcome || "none", handoffRequested: typeof body.handoffRequested === "boolean" ? body.handoffRequested : undefined, handoffReason: body.handoffReason ? String(body.handoffReason) : undefined, }) return NextResponse.json({ success: true, call: result }) } catch (error) { console.error("Failed to link phone call lead:", error) return NextResponse.json( { error: "Failed to link phone call lead" }, { status: 500 } ) } }