import { NextResponse } from "next/server";
import { syncThreadsOwnPostsAndInsights } from "@/lib/services/threads-api-sync";

export const maxDuration = 120;

export async function POST(request: Request) {
  try {
    const body = (await request.json().catch(() => ({}))) as {
      postsLimit?: number;
      repliesLimit?: number;
    };
    const result = await syncThreadsOwnPostsAndInsights({
      postsLimit: body.postsLimit,
      repliesLimit: body.repliesLimit,
    });
    return NextResponse.json(result);
  } catch (error) {
    const message = error instanceof Error ? error.message : "Threads 同步失敗";
    return NextResponse.json({ error: message }, { status: 500 });
  }
}
