import type { CallToolResultPayload, McpTool } from './types'

export type McpServerConfig = McpHttpConfig | McpSseConfig | McpStdioConfig

export interface McpHttpConfig {
  id: string
  name: string
  transport: 'http'
  url: string
  auth?: {
    type: 'bearer'
    token: string | (() => string)
  }
  headers?: Record<string, string>
  requestTimeoutMs?: number
  enabled?: boolean
}

export interface McpSseConfig {
  id: string
  name: string
  transport: 'sse'
  url: string
  auth?: {
    type: 'bearer'
    token: string | (() => string)
  }
  headers?: Record<string, string>
  requestTimeoutMs?: number
  enabled?: boolean
}

export interface McpStdioConfig {
  id: string
  name: string
  transport: 'stdio'
  command: string
  args: string[]
  env?: Record<string, string>
  cwd?: string
  autoRestart?: boolean
  requestTimeoutMs?: number
  restartDelayMs?: number
  maxRestarts?: number
  restartWindowMs?: number
  enabled?: boolean
}

export abstract class McpClient {
  protected _enableLogging = false

  abstract readonly config: McpServerConfig
  abstract readonly connected: boolean

  abstract initialize(): Promise<void>
  abstract disconnect(): Promise<void>

  abstract listTools(): Promise<McpTool[]>
  abstract callTool(
    name: string,
    args: Record<string, unknown>,
  ): Promise<CallToolResultPayload>

  abstract getStatus(): {
    connected: boolean
    toolCount: number
    error?: string
  }
}
