import type { GenerationAdapter } from "./ai-adapter-contract.js";

export type AiRuntimeProbeResult =
  | {
      code: "ai_probe_passed";
      mime_type: "image/jpeg" | "image/png" | "image/webp";
      pixel_height: number;
      pixel_width: number;
      real_calls: 1;
      success: true;
    }
  | {
      code: "ai_probe_failed";
      error_category: string;
      real_calls: 1;
      success: false;
    };

export async function runAiRuntimeProbe(adapter: GenerationAdapter): Promise<AiRuntimeProbeResult> {
  const result = await adapter.start({
    configSnapshot: { probe: true },
    generationId: "00000000-0000-4000-8000-000000000002",
    modelId: "gemini-3.1-flash-image-preview",
    prompt: "生成一张简洁的红蓝几何色块测试图，不含文字。",
    ratio: "1:1",
    referenceAssetIds: [],
  });
  if (result.status === "failed") {
    return { code: "ai_probe_failed", error_category: result.category, real_calls: 1, success: false };
  }
  if (result.status !== "completed" || result.outputs.length !== 1) {
    return { code: "ai_probe_failed", error_category: "gateway_contract_invalid", real_calls: 1, success: false };
  }
  const output = result.outputs[0]!;
  try {
    return {
      code: "ai_probe_passed",
      mime_type: output.mimeType,
      pixel_height: output.pixelHeight,
      pixel_width: output.pixelWidth,
      real_calls: 1,
      success: true,
    };
  } finally {
    output.bytes.fill(0);
  }
}
