import { apiGet, apiPost } from "./client";
import type { ImageContent, ModelInfo, SessionState } from "../types/message";
import type { BashResult } from "../utils/bash";
import type { NewSessionResponse } from "../types/session";

export interface ChatResponse {
	ok?: boolean;
	accepted?: boolean;
	slash?: boolean;
	message?: string;
	action?: "new_session" | "reload_messages" | "reload_sessions" | "copy";
	sessionFile?: string;
	error?: string;
}

export type SendMode = "prompt" | "steer" | "follow_up";

export function sendChat(
	message: string,
	images?: ImageContent[],
	options?: { streamingBehavior?: "steer" | "followUp" },
): Promise<ChatResponse> {
	return apiPost("/api/chat", { message, images, streamingBehavior: options?.streamingBehavior });
}

export function sendSteer(message: string, images?: ImageContent[]): Promise<{ ok: boolean }> {
	return apiPost("/api/steer", { message, images });
}

export function sendFollowUp(message: string, images?: ImageContent[]): Promise<{ ok: boolean }> {
	return apiPost("/api/follow-up", { message, images });
}

export function runBash(command: string, options?: { excludeFromContext?: boolean }): Promise<BashResult> {
	return apiPost("/api/bash", { command, excludeFromContext: options?.excludeFromContext });
}

export function abortChat(): Promise<{ ok: boolean }> {
	return apiPost("/api/abort");
}

export function abortRetry(): Promise<{ ok: boolean }> {
	return apiPost("/api/abort-retry");
}

export function sendExtensionUiResponse(
	id: string,
	response: Record<string, unknown>,
): Promise<{ ok: boolean }> {
	return apiPost("/api/extension-ui-response", { id, response });
}

export function fetchSessionState(): Promise<SessionState> {
	return apiGet("/api/session-state");
}

export function fetchModels(): Promise<{ models: ModelInfo[] }> {
	return apiGet("/api/models");
}

export function setModel(provider: string, modelId: string): Promise<{ ok: boolean }> {
	return apiPost("/api/model", { provider, modelId });
}

export function setThinkingLevel(level: string): Promise<{ ok: boolean }> {
	return apiPost("/api/thinking", { level });
}

export function createNewSession(): Promise<NewSessionResponse> {
	return apiPost("/api/new-session");
}
