import type { RpcMessage } from "./message";

export interface AgentStartEvent {
	type: "agent_start";
}

export interface AgentEndEvent {
	type: "agent_end";
}

export interface MessageStartEvent {
	type: "message_start";
	message: RpcMessage;
}

export interface MessageUpdateEvent {
	type: "message_update";
	message: RpcMessage;
}

export interface MessageEndEvent {
	type: "message_end";
	message: RpcMessage;
}

export interface ToolExecutionStartEvent {
	type: "tool_execution_start";
	toolCallId?: string;
	toolName: string;
	args?: unknown;
}

export interface ToolExecutionUpdateEvent {
	type: "tool_execution_update";
	toolCallId?: string;
	toolName: string;
	partialResult?: { content?: unknown };
}

export interface ToolExecutionEndEvent {
	type: "tool_execution_end";
	toolCallId?: string;
	toolName: string;
	isError?: boolean;
	result?: { content?: unknown };
}

export interface CompactionStartEvent {
	type: "compaction_start";
	reason?: string;
}

export interface CompactionEndEvent {
	type: "compaction_end";
	reason?: string;
	aborted?: boolean;
	errorMessage?: string;
	willRetry?: boolean;
	result?: { tokensBefore?: number; summary?: string };
}

export interface QueueUpdateEvent {
	type: "queue_update";
	steering?: string[];
	followUp?: string[];
}

export interface AutoRetryStartEvent {
	type: "auto_retry_start";
	attempt?: number;
	maxAttempts?: number;
	delayMs?: number;
	errorMessage?: string;
}

export interface AutoRetryEndEvent {
	type: "auto_retry_end";
	success?: boolean;
	attempt?: number;
	finalError?: string;
}

export interface ExtensionErrorEvent {
	type: "extension_error";
	extensionPath?: string;
	event?: string;
	error?: string;
}

export interface BashUpdateEvent {
	type: "bash_update";
	command?: string;
	partialOutput?: string;
}

export interface ExtensionUiRequestEvent {
	type: "extension_ui_request";
	id: string;
	method: string;
	title?: string;
	message?: string;
	options?: string[];
	notifyType?: "info" | "warning" | "error";
	statusKey?: string;
	statusText?: string;
	widgetKey?: string;
	widgetLines?: string[];
	prefill?: string;
	[key: string]: unknown;
}

/** Agent stream events replayed on SSE reconnect (excludes connected / prompt_rejected). */
export type StreamSseEvent =
	| AgentStartEvent
	| AgentEndEvent
	| MessageStartEvent
	| MessageUpdateEvent
	| MessageEndEvent
	| ToolExecutionStartEvent
	| ToolExecutionUpdateEvent
	| ToolExecutionEndEvent
	| CompactionStartEvent
	| CompactionEndEvent
	| QueueUpdateEvent
	| AutoRetryStartEvent
	| AutoRetryEndEvent
	| ExtensionErrorEvent
	| BashUpdateEvent
	| ExtensionUiRequestEvent;

export interface ConnectedEvent {
	type: "connected";
	isStreaming?: boolean;
	replay?: StreamSseEvent[];
}

export interface PromptRejectedEvent {
	type: "prompt_rejected";
	error: string;
}

export type SseEvent = ConnectedEvent | PromptRejectedEvent | StreamSseEvent;

export interface SkillInfo {
	name?: string;
	command?: string;
	description?: string;
	path?: string;
	scope?: string;
	source?: string;
	enabled?: boolean;
	toggleable?: boolean;
}

export interface McpToolInfo {
	server?: string;
	name?: string;
	description?: string;
	parameters?: string[];
	required?: string[];
	enabled?: boolean;
}

export interface McpServerInfo {
	name?: string;
	configured?: boolean;
	enabled?: boolean;
	cached?: boolean;
	toolCount?: number;
	enabledToolCount?: number;
	resourceCount?: number;
	cachedAt?: string;
	tools?: McpToolInfo[];
}

export interface ExtensionInfo {
	name?: string;
	path?: string;
	resolvedPath?: string;
	kind?: string;
	category?: "local" | "npm";
	scope?: string;
	source?: string;
	location?: string;
	version?: string;
	enabled?: boolean;
	toggleable?: boolean;
	commands?: string[];
	tools?: string[];
	handlers?: string[];
	flags?: string[];
	shortcuts?: string[];
}

export interface SettingsData {
	systemPrompt?: string;
	systemPromptPath?: string;
	modelsConfig?: string;
	modelsConfigPath?: string;
	userAvatarUrl?: string;
	agentAvatarUrl?: string;
	skills?: SkillInfo[];
	mcpTools?: McpServerInfo[];
	mcpConfigPath?: string;
	mcpCachePath?: string;
	extensions?: ExtensionInfo[];
	extensionsPath?: string;
	error?: string;
}

export interface AvatarSettings {
	userAvatarUrl: string;
	agentAvatarUrl: string;
}

export type SettingsPaneId = "prompt" | "models" | "skills" | "mcp" | "extensions" | "other" | "terminal" | "env" | "prompts";

export interface AgentToolInfo {
	name: string;
	found: boolean;
	version?: string;
	path?: string;
}

export interface RuntimeInfo {
	name: string;
	found: boolean;
	version?: string;
	path?: string;
}

export interface EnvironmentToolsInfo {
	agentTools?: AgentToolInfo[];
	runtimes?: RuntimeInfo[];
}

export interface EnvironmentInfo {
	// 系统信息
	platform?: string;
	arch?: string;
	osRelease?: string;
	hostname?: string;
	numCPU?: number;
	totalMemMb?: number;
	freeMemMb?: number;
	memUsagePct?: string;
	// 进程信息
	goVersion?: string;
	pid?: number;
	uptime?: number;
	execPath?: string;
	goroutines?: number;
	goHeapMb?: number;
	// 路径与配置
	port?: number;
	agentDir?: string;
	dataDir?: string;
	repoRoot?: string;
	cwd?: string;
	piCmd?: string;
	sproutclawCmd?: string;
	// Agent 工具
	agentTools?: AgentToolInfo[];
	// 兼容旧字段
	nodeVersion?: string;
	osType?: string;
}

export interface RetryState {
	active: boolean;
	attempt?: number;
	maxAttempts?: number;
	delayMs?: number;
	startedAt?: number;
	errorMessage?: string;
}

export interface ExtensionDialogState {
	id: string;
	method: "select" | "confirm" | "input" | "editor";
	title: string;
	message?: string;
	options?: string[];
	prefill?: string;
}
