import type { SuccessResponse } from "../../types"

// Transport types
export type McpTransportType = "streamable-http" | "sse"

// Basic schemas from server
export interface McpConnectionInfo {
  id: string
  name: string
  transportType: McpTransportType
  url?: string
  isConnected: boolean
  lastError?: string
  toolCount: number
  resourceCount: number
  promptCount: number
  createdAt: string
  lastUsed: string | null
}

export interface McpTool {
  name: string
  title?: string
  description?: string
  inputSchema: {
    type: "object"
    properties?: Record<string, any>
    required?: string[]
  }
  outputSchema?: {
    type: "object"
    properties?: Record<string, any>
    required?: string[]
  }
  annotations?: {
    title?: string
    readOnlyHint?: boolean
    openWorldHint?: boolean
  }
}

export interface RefreshResult {
  success: boolean
  error?: string
}

// Request types
export interface CreateConnectionRequest {
  name: string
  transportType: McpTransportType
  url: string
  headers?: Record<string, string>
}

export interface UpdateConnectionRequest {
  name?: string
  transportType?: McpTransportType
  url?: string
  headers?: Record<string, string>
  connectionId: string
}

export type UpdateConnectionResponse = {
  code: 0 | 1
  /**
   * If code is 1, the authorizationUrl is required
   */
  authorizationUrl?: string
}

export interface ConnectionParams {
  connectionId: string
}

export interface RefreshToolsRequest {
  connectionIds?: string[]
}

// Response types
export type CreateConnectionResponse = {
  /**
   * 0: success, 1: need oauth
   */
  code: 0 | 1
  authorizationUrl?: string
}

export type GetConnectionsResponse = SuccessResponse<McpConnectionInfo[]>

export type DeleteConnectionResponse = SuccessResponse

export type GetToolsResponse = SuccessResponse<McpTool[]>

export type RefreshToolsResponse = SuccessResponse<{
  total: number
  successful: number
  failed: number
  results: RefreshResult[]
}>
