import type { ISimpleLocalConfig, ISimpleLocalIntegrationProfile, ISimpleLocalSnapshot, TSimpleLocalRawData } from '../../core/index.js';

export const mcpServerDomain = "mcp_server";
export const mcpServerDefaultName = "Model Context Protocol Server";
export const mcpServerDefaultHttpPath = '/api/mcp';
export const mcpServerDefaultProtocolVersion = '2025-03-26';
export const mcpServerDefaultTimeoutMs = 10000;

export interface IMcpServerToolDefinition {
  name: string;
  description?: string;
  inputSchema?: Record<string, unknown>;
  [key: string]: unknown;
}

export interface IMcpServerPromptDefinition {
  name: string;
  description?: string;
  [key: string]: unknown;
}

export interface IMcpServerResourceDefinition {
  uri: string;
  name?: string;
  title?: string;
  description?: string;
  mimeType?: string;
  [key: string]: unknown;
}

export interface IMcpServerRawState {
  protocolVersion?: string;
  serverInfo?: {
    name?: string;
    version?: string;
    [key: string]: unknown;
  };
  capabilities?: Record<string, unknown>;
  tools: IMcpServerToolDefinition[];
  prompts: IMcpServerPromptDefinition[];
  resources: IMcpServerResourceDefinition[];
  toolCount: number;
  promptCount: number;
  resourceCount: number;
  initialized: boolean;
  endpointUrl?: string;
  fetchedAt: string;
}

export type TMcpServerRawData = IMcpServerRawState | TSimpleLocalRawData;
export interface IMcpServerSnapshot extends ISimpleLocalSnapshot {}
export interface IMcpServerConfig extends ISimpleLocalConfig {
  url?: string;
  accessToken?: string;
  protocolVersion?: string;
  macAddress?: string;
  deviceId?: string;
  headers?: Record<string, string>;
}
export interface IHomeAssistantMcpServerConfig extends IMcpServerConfig {}

export const mcpServerProfile: ISimpleLocalIntegrationProfile = {
  domain: mcpServerDomain,
  displayName: "Model Context Protocol Server",
  manufacturer: "Model Context Protocol Server",
  model: "Model Context Protocol Server local integration",
  defaultName: mcpServerDefaultName,
  defaultHttpPath: mcpServerDefaultHttpPath,
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "sensor",
  ],
  serviceDomains: [
    "mcp_server",
  ],
  controlServices: [
    "call_tool",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "mcp_server",
    "Model Context Protocol Server",
    "Mcp Server",
    "Model",
    "Context",
    "Protocol",
    "Server",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/mcp_server",
    "upstreamDomain": "mcp_server",
    "integrationType": "service",
    "iotClass": "local_push",
    "qualityScale": "silver",
    "requirements": [
      "mcp==1.26.0",
      "aiohttp_sse==2.2.0",
      "anyio==4.10.0",
    ],
    "dependencies": [
      "http",
      "conversation",
    ],
    "codeowners": [
      "@allenporter",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/mcp_server",
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "call_tool",
      ],
      "platforms": [
        "sensor",
      ],
      "controls": "native streamable HTTP tools/call against the configured Home Assistant MCP Server endpoint; hosting a server is not implemented in this client class",
    },
    "localApi": {
      "implemented": [
        "manual local setup for Model Context Protocol Server endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "native MCP streamable HTTP JSON-RPC initialize, notifications/initialized, tools/list, prompts/list, resources/list, and tools/call over fetch",
        "default Home Assistant MCP Server endpoint path /api/mcp from upstream http.py",
        "bearer-token header support when config.token or config.accessToken is supplied",
        "runtime mapping for MCP Server tools, prompts, and resources",
        "safe command delegation to injected client.execute or commandExecutor for custom services",
      ],
      "explicitUnsupported": [
        "hosting a TypeScript MCP server backed by Home Assistant LLM APIs; upstream server behavior depends on Home Assistant conversation/llm runtime and the Python mcp/anyio/aiohttp_sse stack",
        "legacy MCP SSE endpoint /mcp_server/sse and /mcp_server/messages; only streamable HTTP POST JSON-RPC /api/mcp is implemented natively here",
        "stateful MCP session resumption; this client uses stateless POST JSON-RPC requests and does not persist MCP-Session-Id",
        "Home Assistant entity registry migrations and repair flows",
        "claiming tool-call success without a JSON-RPC result from the MCP endpoint or injected executor",
      ],
    },
  },
};
