import { NextResponse } from "next/server" import { fetchQuery } from "convex/nextjs" import { api } from "@/convex/_generated/api" import { requirePhoneAgentInternalAuth } from "@/app/api/internal/phone-calls/shared" import { normalizePhoneE164 } from "@/lib/phone-normalization" export async function POST(request: Request) { const authError = await requirePhoneAgentInternalAuth(request) if (authError) { return authError } try { const body = await request.json() const normalizedPhone = normalizePhoneE164(body.phone) if (!normalizedPhone) { return NextResponse.json( { error: "phone is required" }, { status: 400 } ) } const context = await fetchQuery(api.voiceSessions.getPhoneAgentContextByPhone, { normalizedPhone, }) return NextResponse.json({ success: true, normalizedPhone, ...context, }) } catch (error) { console.error("Failed to look up phone agent contact context:", error) return NextResponse.json( { error: "Failed to look up phone agent contact context" }, { status: 500 } ) } }