import { mkdir, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { ensureWorkspacePath } from "../agent/tools/path-utils.js";

export interface WorkspaceContent {
  filePath: string;
  content: string;
  bytes: number;
}

export async function writeWorkspaceFile(
  workspacePath: string,
  relativePath: string,
  content: string,
): Promise<WorkspaceContent> {
  const filePath = join(workspacePath, relativePath);
  ensureWorkspacePath(workspacePath, filePath);
  await mkdir(dirname(filePath), { recursive: true });
  await writeFile(filePath, content, "utf8");

  return {
    filePath,
    content,
    bytes: Buffer.byteLength(content, "utf8"),
  };
}
