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

export const mcpDomain = "mcp";
export const mcpDefaultName = "Model Context Protocol";
export const mcpDefaultHttpPath = '/mcp';
export const mcpDefaultProtocolVersion = '2025-03-26';
export const mcpDefaultTimeoutMs = 10000;

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

export interface IMcpRawState {
  protocolVersion?: string;
  serverInfo?: {
    name?: string;
    version?: string;
    [key: string]: unknown;
  };
  capabilities?: Record<string, unknown>;
  tools: IMcpToolDefinition[];
  toolCount: number;
  initialized: boolean;
  endpointUrl?: string;
  fetchedAt: string;
}

export type TMcpRawData = IMcpRawState | TSimpleLocalRawData;
export interface IMcpSnapshot extends ISimpleLocalSnapshot {}
export interface IMcpConfig extends ISimpleLocalConfig {
  url?: string;
  accessToken?: string;
  protocolVersion?: string;
  macAddress?: string;
  deviceId?: string;
  headers?: Record<string, string>;
}
export interface IHomeAssistantMcpConfig extends IMcpConfig {}

export const mcpProfile: ISimpleLocalIntegrationProfile = {
  domain: mcpDomain,
  displayName: "Model Context Protocol",
  manufacturer: "Model Context Protocol",
  model: "Model Context Protocol local integration",
  defaultName: mcpDefaultName,
  defaultHttpPath: mcpDefaultHttpPath,
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "sensor",
  ],
  serviceDomains: [
    "mcp",
  ],
  controlServices: [
    "call_tool",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "mcp",
    "Model Context Protocol",
    "Mcp",
    "Model",
    "Context",
    "Protocol",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/mcp",
    "upstreamDomain": "mcp",
    "iotClass": "local_polling",
    "qualityScale": "silver",
    "requirements": [
      "mcp==1.26.0",
    ],
    "dependencies": [
      "application_credentials",
    ],
    "codeowners": [
      "@allenporter",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/mcp",
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "call_tool",
      ],
      "platforms": [
        "sensor",
      ],
      "controls": "native streamable HTTP tools/call when configured with a reachable MCP endpoint; otherwise no command success is reported",
    },
    "localApi": {
      "implemented": [
        "manual local setup for Model Context Protocol endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "native MCP streamable HTTP JSON-RPC initialize, notifications/initialized, tools/list, and tools/call over fetch",
        "bearer-token header support when config.token or config.accessToken is supplied",
        "runtime mapping for MCP server info and tool snapshots",
        "safe command delegation to injected client.execute or commandExecutor for custom services",
      ],
      "explicitUnsupported": [
        "OAuth authorization discovery/token refresh from Home Assistant application_credentials; pass a static bearer token or injected client instead",
        "legacy MCP SSE fallback transport; only streamable HTTP POST JSON-RPC 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",
      ],
    },
  },
};
