import type { LlmProviderStatus, LlmTaskName } from "./client";

export interface LlmAuditSummary {
  provider: LlmProviderStatus["provider"];
  model: string;
  task: LlmTaskName;
  duration_ms: number;
  schema_valid: boolean;
  error_summary: string | null;
  input_hash: string;
  output_hash: string | null;
}

export async function hashContent(value: string) {
  const data = new TextEncoder().encode(value);
  const digest = await crypto.subtle.digest("SHA-256", data);
  return Array.from(new Uint8Array(digest))
    .map((byte) => byte.toString(16).padStart(2, "0"))
    .join("");
}

export async function createLlmAuditSummary({
  provider,
  model,
  task,
  duration_ms,
  schema_valid,
  prompt,
  output,
  error_summary,
}: {
  provider: LlmProviderStatus["provider"];
  model: string;
  task: LlmTaskName;
  duration_ms: number;
  schema_valid: boolean;
  prompt: string;
  output: string | null;
  error_summary: string | null;
}): Promise<LlmAuditSummary> {
  return {
    provider,
    model,
    task,
    duration_ms,
    schema_valid,
    error_summary,
    input_hash: await hashContent(prompt),
    output_hash: output == null ? null : await hashContent(output),
  };
}
