import type {
  CommandManifestPayload,
  SlashCommandApprovalResultPayload,
  SlashCommandPayload,
  SlashCommandResultPayload,
  SystemReloadPayload,
} from '../commands/index.js';

export interface MessageAckPayload {
  conversationId: string;
  messageId: string;
}

export interface AgentStartPayload {
  conversationId: string;
}

export interface AgentEndPayload {
  conversationId: string;
  usage?: SessionUsagePayload;
}

/** Session metadata emitted with agent:end and on session:info */
export interface SessionUsagePayload {
  provider: string;
  modelId: string;
  thinkingLevel: string;
  tokens: {
    input: number;
    output: number;
    cacheRead: number;
    cacheWrite: number;
    total: number;
  };
  cost: number;
  context: {
    percent: number | null;
    window: number;
  };
}

export interface AgentTextPayload {
  conversationId: string;
  text: string;
}

export interface AgentThinkingPayload {
  conversationId: string;
  text: string;
}

export interface ToolStartPayload {
  conversationId: string;
  toolCallId: string;
  toolName: string;
}

export interface ToolEndPayload {
  conversationId: string;
  toolCallId: string;
  toolName: string;
  isError: boolean;
}

export interface ErrorPayload {
  conversationId: string;
  error: string;
}

export interface ChatMessagePayload {
  conversationId?: string;
  content: string;
  provider?: string;
  modelId?: string;
  agentId?: string;
}

/** Routing decision summary included in session:info for transparency */
export interface RoutingDecisionInfo {
  model: string;
  provider: string;
  ruleName: string;
  reason: string;
}

/** Session info pushed when session is created or model changes */
export interface SessionInfoPayload {
  conversationId: string;
  provider: string;
  modelId: string;
  thinkingLevel: string;
  availableThinkingLevels: string[];
  /** Present when automatic routing determined the model for this session */
  routingDecision?: RoutingDecisionInfo;
}

/** Client request to change thinking level */
export interface SetThinkingPayload {
  conversationId: string;
  level: string;
}

/** Client request to abort the current agent operation */
export interface AbortPayload {
  conversationId: string;
}

/** Socket.IO typed event map: server → client */
export interface ServerToClientEvents {
  'message:ack': (payload: MessageAckPayload) => void;
  'agent:start': (payload: AgentStartPayload) => void;
  'agent:end': (payload: AgentEndPayload) => void;
  'agent:text': (payload: AgentTextPayload) => void;
  'agent:thinking': (payload: AgentThinkingPayload) => void;
  'agent:tool:start': (payload: ToolStartPayload) => void;
  'agent:tool:end': (payload: ToolEndPayload) => void;
  'session:info': (payload: SessionInfoPayload) => void;
  'commands:manifest': (payload: CommandManifestPayload) => void;
  'command:result': (payload: SlashCommandResultPayload) => void;
  'command:approval': (payload: SlashCommandApprovalResultPayload) => void;
  'system:reload': (payload: SystemReloadPayload) => void;
  error: (payload: ErrorPayload) => void;
}

/** Socket.IO typed event map: client → server */
export interface ClientToServerEvents {
  message: (data: ChatMessagePayload) => void;
  'set:thinking': (data: SetThinkingPayload) => void;
  'command:execute': (data: SlashCommandPayload) => void;
  'command:approve': (data: SlashCommandPayload) => void;
  abort: (data: AbortPayload) => void;
}
