/** Workspace file API client. */

import { apiClient } from "./client";

export interface FileEntry {
	name: string;
	path: string;
	type: "file" | "directory";
	size?: number;
}

export async function listWorkspaceFiles(
	workspaceId: string,
	path: string = "",
): Promise<FileEntry[]> {
	const response = await apiClient.get<{ entries: FileEntry[] }>(
		`/workspaces/${workspaceId}/files/`,
		{ params: { path } },
	);
	return response.data.entries;
}

export async function getWorkspaceFileContent(
	workspaceId: string,
	path: string,
): Promise<string> {
	const response = await apiClient.get<{ content: string }>(
		`/workspaces/${workspaceId}/files/content`,
		{ params: { path } },
	);
	return response.data.content;
}

export async function saveWorkspaceFile(
	workspaceId: string,
	path: string,
	content: string,
	commitMessage?: string,
): Promise<void> {
	await apiClient.post(`/workspaces/${workspaceId}/files/content`, {
		path,
		content,
		message: commitMessage,
	});
}
