import { NextResponse } from "next/server" import { fetchAction } from "convex/nextjs" import { api } from "@/convex/_generated/api" import { requireGhlSyncAuth } from "@/app/api/internal/ghl/shared" export async function POST(request: Request) { const authError = await requireGhlSyncAuth(request) if (authError) { return authError } try { const body = await request.json().catch(() => ({})) const result = await fetchAction(api.crm.runGhlMirror, { reason: body.reason ? String(body.reason) : "internal", forceFullBackfill: Boolean(body.forceFullBackfill), maxPagesPerRun: typeof body.maxPagesPerRun === "number" ? body.maxPagesPerRun : undefined, contactsLimit: typeof body.contactsLimit === "number" ? body.contactsLimit : undefined, messagesLimit: typeof body.messagesLimit === "number" ? body.messagesLimit : undefined, recordingsPageSize: typeof body.recordingsPageSize === "number" ? body.recordingsPageSize : undefined, }) return NextResponse.json(result) } catch (error) { console.error("Failed to run GHL sync:", error) return NextResponse.json( { error: error instanceof Error ? error.message : "Failed to run GHL sync", }, { status: 500 } ) } }