// 图片生成生图 API，统一封装临时配置提交、生成历史查询和结果删除。
import http from "@/api/http";

export interface ProductImageGeneration {
  task_id: string;
  status: string;
  prompt: string;
  model_name: string;
  error_message: string;
  media_asset_id?: string;
  public_url: string;
  mime_type: string;
  estimated_points: string;
  actual_points?: string;
  created_at: string;
  finished_at?: string;
}

export interface ProductImageGenerationInput {
  prompt: string;
  model_id: string;
  aspect_ratio: string;
  resolution: string;
  references: File[];
}

const data = <T>(response: { data: { data: T } }) => response.data.data;

export const productImageApi = {
  generate: (input: ProductImageGenerationInput) => {
    const form = new FormData();
    form.append("prompt", input.prompt);
    form.append("model_id", input.model_id);
    form.append("aspect_ratio", input.aspect_ratio);
    form.append("resolution", input.resolution);
    input.references.forEach((file) => form.append("references", file, file.name));
    return http
      .post("/product-images/generations", form, {
        timeout: 0,
        headers: { "Content-Type": "multipart/form-data" },
      })
      .then(data<{ task_id: string }>);
  },
  generations: () => http.get("/product-images/generations").then(data<ProductImageGeneration[]>),
  removeGeneration: (taskId: string) => http.delete(`/product-images/generations/${taskId}`),
};
