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.upsertPhoneCallSession, { roomName: String(body.roomName || ""), participantIdentity: String(body.participantIdentity || ""), siteUrl: body.siteUrl ? String(body.siteUrl) : undefined, pathname: body.pathname ? String(body.pathname) : undefined, pageUrl: body.pageUrl ? String(body.pageUrl) : undefined, source: "phone-agent", metadata: body.metadata ? String(body.metadata) : undefined, startedAt: typeof body.startedAt === "number" ? body.startedAt : undefined, recordingDisclosureAt: typeof body.recordingDisclosureAt === "number" ? body.recordingDisclosureAt : undefined, recordingStatus: body.recordingStatus || "pending", } ) return NextResponse.json({ success: true, sessionId: result?._id, roomName: result?.roomName, }) } catch (error) { console.error("Failed to start phone call sync:", error) return NextResponse.json( { error: "Failed to start phone call sync" }, { status: 500 } ) } }