import { NextResponse } from "next/server";
import { ZodError } from "zod";
import { prisma, resolvePersona } from "@/lib/db";
import { getOrCreateSettings } from "@/lib/user-settings";
import { getActiveAccountProfile } from "@/lib/account-context";
import { parseProviderApiKeys } from "@/lib/ai/keys";
import { generateInboundReplyDrafts } from "@/lib/ai/generate-replies";
import { trackAiTask } from "@/lib/jobs/track";

export const maxDuration = 60;

export async function POST(
  _request: Request,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params;
    const inbound = await prisma.inboundReply.findUnique({
      where: { id },
      include: { published: true },
    });
    if (!inbound) return NextResponse.json({ error: "找不到留言" }, { status: 404 });

    const settings = await getOrCreateSettings();
    const account = await getActiveAccountProfile();
    const apiKeys = parseProviderApiKeys(settings.providerApiKeys);
    const generated = await trackAiTask("生成互動回覆", () => generateInboundReplyDrafts({
      persona: resolvePersona(settings, account),
      aiProvider: settings.aiProvider,
      aiModel: settings.aiModel,
      apiKeys,
      publishedText: inbound.published?.text,
      replyText: inbound.text,
      authorName: inbound.authorName,
      count: 2,
    }));

    await prisma.inboundReply.update({
      where: { id },
      data: {
        sentiment: generated.sentiment,
        intent: generated.intent,
        status: "DRAFTED",
      },
    });

    const drafts = [];
    for (const draft of generated.drafts) {
      drafts.push(
        await prisma.replyDraft.create({
          data: {
            inboundReplyId: id,
            text: draft.text,
            rationale: draft.rationale,
          },
        })
      );
    }

    return NextResponse.json({ drafts, sentiment: generated.sentiment, intent: generated.intent });
  } catch (error) {
    if (error instanceof ZodError) {
      return NextResponse.json(
        { error: "AI 回傳格式不完整（缺少回覆正文），請再試一次" },
        { status: 500 }
      );
    }
    const message = error instanceof Error ? error.message : "生成回覆草稿失敗";
    return NextResponse.json({ error: message }, { status: 500 });
  }
}
