import { NextResponse } from "next/server";
import { prisma } from "@/lib/db";
import { getOrCreateSettings } from "@/lib/user-settings";
import { generateDraftImage } from "@/lib/ai/generate-draft-image";
import { parseProviderApiKeys } from "@/lib/ai/keys";
import {
  MAX_DRAFT_IMAGES,
  parseDraftImagePaths,
  saveDraftImage,
  syncDraftImageFields,
} from "@/lib/drafts/images";
import { trackAiTask } from "@/lib/jobs/track";

export const maxDuration = 120;

export async function POST(
  _request: Request,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params;
    const draft = await prisma.draft.findUnique({ where: { id } });
    if (!draft) {
      return NextResponse.json({ error: "找不到草稿" }, { status: 404 });
    }

    const existing = parseDraftImagePaths(draft);
    if (existing.length >= MAX_DRAFT_IMAGES) {
      return NextResponse.json(
        { error: `每篇草稿最多 ${MAX_DRAFT_IMAGES} 張配圖` },
        { status: 400 }
      );
    }

    const settings = await getOrCreateSettings();
    const apiKeys = parseProviderApiKeys(settings.providerApiKeys);

    const { image, prompt } = await trackAiTask("AI 生成貼文配圖", () => generateDraftImage({
      text: draft.text,
      hook: draft.hook,
      imageBrief: draft.imageBrief,
      angle: draft.angle,
      aiProvider: settings.aiProvider,
      aiModel: settings.aiModel,
      apiKeys,
    }));

    const imagePath = await saveDraftImage(id, image.uint8Array, image.mimeType);
    const nextPaths = [...existing, imagePath];

    const updated = await prisma.draft.update({
      where: { id },
      data: syncDraftImageFields(nextPaths),
    });

    return NextResponse.json({
      draft: updated,
      imagePaths: parseDraftImagePaths(updated),
      prompt,
    });
  } catch (error) {
    const message = error instanceof Error ? error.message : "AI 生圖失敗";
    return NextResponse.json({ error: message }, { status: 500 });
  }
}
