import type { ToolErrorResult, ToolBaseResult } from "./types.js";

export function createOkBase(projectId: string, warnings: readonly string[] = []): ToolBaseResult {
  return {
    ok: true,
    project_id: projectId,
    warnings: [...warnings]
  };
}

export function createToolError(errorCode: string, detail: string): ToolErrorResult {
  return {
    ok: false,
    error_code: errorCode,
    detail
  };
}

export function toMcpText(result: unknown): { content: [{ type: "text"; text: string }] } {
  return {
    content: [{ type: "text", text: JSON.stringify(result, null, 2) }]
  };
}

export function toMcpSuccess(summary: string, structuredContent: Record<string, unknown>): {
  content: [{ type: "text"; text: string }];
  structuredContent: Record<string, unknown>;
} {
  return {
    content: [{ type: "text", text: summary }],
    structuredContent
  };
}

export function toMcpError(
  errorCode: string,
  detail: string,
  projectId?: string
): {
  content: [{ type: "text"; text: string }];
  structuredContent: ToolErrorResult;
  isError: true;
} {
  const structured: ToolErrorResult = projectId
    ? { ok: false, error_code: errorCode, detail, project_id: projectId }
    : { ok: false, error_code: errorCode, detail };
  return {
    content: [{ type: "text", text: `[${errorCode}] ${detail}` }],
    structuredContent: structured,
    isError: true
  };
}
