import type {
  ArticleInput,
  ConfirmedFactCard,
  OptimizedArticle,
} from "../domain/types";
import { optimizedArticleSchema } from "../domain/validation";
import { generateValidatedJson } from "../llm/client";
import {
  ARTICLE_OPTIMIZER_SYSTEM_PROMPT,
  buildArticleOptimizerPrompt,
} from "../llm/prompts";

export interface OptimizeArticleInput {
  input: ArticleInput;
  factCard: ConfirmedFactCard;
}

export async function optimizeArticle({
  input,
  factCard,
}: OptimizeArticleInput): Promise<OptimizedArticle> {
  const llmArticle = await generateValidatedJson({
    schema: optimizedArticleSchema,
    system: ARTICLE_OPTIMIZER_SYSTEM_PROMPT,
    prompt: buildArticleOptimizerPrompt(input, factCard),
    temperature: 0.2,
    task: "article_optimizer",
  });

  return optimizedArticleSchema.parse({
    ...llmArticle,
    image_suggestions: [],
  });
}
