import { apiClient } from "./client";

export interface TerminalSession {
	id: string;
	name: string;
	status: string;
	has_websockets: boolean;
	created_at: string;
	last_activity_at: string | null;
}

export interface TerminalSessionListResponse {
	sessions: TerminalSession[];
}

export interface TerminalSessionCreateRequest {
	name?: string;
}

export interface TerminalSessionCreateResponse {
	id: string;
	name: string;
	status: string;
	created_at: string;
}

export async function listTerminalSessions(
	instanceId: string,
): Promise<TerminalSession[]> {
	const response = await apiClient.get(
		`/instances/${instanceId}/terminal/sessions`,
	);
	return response.data.sessions;
}

export async function createTerminalSession(
	instanceId: string,
	name?: string,
): Promise<TerminalSessionCreateResponse> {
	const response = await apiClient.post(
		`/instances/${instanceId}/terminal/sessions`,
		{ name },
	);
	return response.data;
}

export async function closeTerminalSession(
	instanceId: string,
	sessionId: string,
): Promise<{ status: string; session_id: string }> {
	const response = await apiClient.delete(
		`/instances/${instanceId}/terminal/sessions/${sessionId}`,
	);
	return response.data;
}

export async function resetTerminalSession(
	instanceId: string,
	sessionId: string,
): Promise<{ id: string; name: string; status: string }> {
	const response = await apiClient.post(
		`/instances/${instanceId}/terminal/sessions/${sessionId}/reset`,
	);
	return response.data;
}

export async function renameTerminalSession(
	instanceId: string,
	sessionId: string,
	name: string,
): Promise<{ id: string; name: string }> {
	const response = await apiClient.post(
		`/instances/${instanceId}/terminal/sessions/${sessionId}/rename`,
		{ name },
	);
	return response.data;
}
