export type MessageRole = "user" | "assistant";

export interface McpAppData {
  id: string;
  html?: string;
  url?: string;
  title?: string;
  height?: number;
}

export interface CredentialRequirement {
  service: string;
  icon: string;
  type: string; // e.g., "google-oauth2", "slack-oauth2"
  description: string;
}

export interface WorkflowOps {
  name: string;
  nodes: {
    name: string;
    type: string;
    icon: string;
    category: string;
    parameters: Record<string, string>;
  }[];
  requiredCredentials?: CredentialRequirement[];
}

export interface ChatMessage {
  id: string;
  role: MessageRole;
  content: string;
  timestamp: Date;
  mcpApps?: McpAppData[];
  workflowOps?: WorkflowOps;
  // Thread parent fields (present when this message spawned an app thread)
  threadId?: string;
  threadAppId?: string;
  threadTitle?: string;
  threadIcon?: string;
  skipLabel?: string;
}

export interface Channel {
  id: string;
  name: string;
  icon: string;
  category: string;
  description: string;
  systemPrompt: string;
  mcpApps?: string[];
  defaultApp?: string; // Auto-open this app's thread when entering the channel
}

export interface ChannelCategory {
  name: string;
  channels: Channel[];
}

// ─── Open panel tracking ─────────────────────────────────────────────────────

export interface OpenPanel {
  channelId: string;
  threadId: string;
}

// ─── App Thread types ───────────────────────────────────────────────────────

export interface ThreadMessage {
  id: string;
  role: MessageRole;
  content: string;
  timestamp: Date;
}

export interface ThreadState {
  threadId: string;
  appId: string;
  parentMessageId: string;
  category: string;
  messages: ThreadMessage[];
  appData: Record<string, unknown> | null;
  appRefreshKey: number;
  isLoading: boolean;
  skipped: boolean;
}

export interface AppThread {
  appId: string;
  messages: ThreadMessage[];
  isLoading: boolean;
  appData: Record<string, unknown> | null;
}

export interface ThreadContext {
  appId: string;
  category: string;
  previousAppData: Record<string, unknown> | null;
}

// ─── MCP JSON-RPC types ─────────────────────────────────────────────────────

export interface McpJsonRpcRequest {
  jsonrpc: "2.0";
  id?: number | string;
  method: string;
  params?: Record<string, unknown>;
}

export interface McpJsonRpcResponse {
  jsonrpc: "2.0";
  id?: number | string;
  result?: unknown;
  error?: {
    code: number;
    message: string;
    data?: unknown;
  };
}

export interface McpToolCallRequest extends McpJsonRpcRequest {
  method: "tools/call";
  params: {
    name: string;
    arguments: Record<string, unknown>;
  };
}

export interface McpContextUpdate extends McpJsonRpcRequest {
  method: "context/update";
  params: Record<string, unknown>;
}

export interface McpMessageSend extends McpJsonRpcRequest {
  method: "messages/send";
  params: {
    content: string;
  };
}
