export interface BaseTelemetryEvent {
  name: string;
  properties: Record<string, any>;
}

export interface MCPInspectorOpenEventData {
  serverUrl?: string;
  connectionCount?: number;
}

export class MCPInspectorOpenEvent implements BaseTelemetryEvent {
  name = "mcp_inspector_open";
  properties: Record<string, any>;

  constructor(data: MCPInspectorOpenEventData) {
    this.properties = {
      server_url: data.serverUrl,
      connection_count: data.connectionCount,
    };
  }
}

export interface MCPToolExecutionEventData {
  toolName: string;
  serverId?: string;
  success: boolean;
  duration?: number;
  error?: string;
}

export class MCPToolExecutionEvent implements BaseTelemetryEvent {
  name = "mcp_tool_execution";
  properties: Record<string, any>;

  constructor(data: MCPToolExecutionEventData) {
    this.properties = {
      tool_name: data.toolName,
      server_id: data.serverId,
      success: data.success,
      duration: data.duration,
      error: data.error,
    };
  }
}

export interface MCPResourceReadEventData {
  resourceUri: string;
  serverId?: string;
  success: boolean;
  error?: string;
}

export class MCPResourceReadEvent implements BaseTelemetryEvent {
  name = "mcp_resource_read";
  properties: Record<string, any>;

  constructor(data: MCPResourceReadEventData) {
    this.properties = {
      resource_uri: data.resourceUri,
      server_id: data.serverId,
      success: data.success,
      error: data.error,
    };
  }
}

export interface MCPPromptCallEventData {
  promptName: string;
  serverId?: string;
  success: boolean;
  error?: string;
}

export class MCPPromptCallEvent implements BaseTelemetryEvent {
  name = "mcp_prompt_call";
  properties: Record<string, any>;

  constructor(data: MCPPromptCallEventData) {
    this.properties = {
      prompt_name: data.promptName,
      server_id: data.serverId,
      success: data.success,
      error: data.error,
    };
  }
}

export interface MCPServerConnectionEventData {
  serverId: string;
  serverUrl: string;
  success: boolean;
  connectionType?: "http" | "sse";
  error?: string;
}

export class MCPServerConnectionEvent implements BaseTelemetryEvent {
  name = "mcp_server_connection";
  properties: Record<string, any>;

  constructor(data: MCPServerConnectionEventData) {
    this.properties = {
      server_id: data.serverId,
      server_url: data.serverUrl,
      success: data.success,
      connection_type: data.connectionType,
      error: data.error,
    };
  }
}

export interface MCPChatMessageEventData {
  serverId?: string;
  provider:
    | "openai"
    | "openai-compatible"
    | "anthropic"
    | "google"
    | "openrouter";
  model: string;
  messageCount: number;
  toolCallsCount?: number;
  success: boolean;
  executionMode: "client-side" | "server-side";
  duration?: number;
  error?: string;
}

export class MCPChatMessageEvent implements BaseTelemetryEvent {
  name = "mcp_chat_message";
  properties: Record<string, any>;

  constructor(data: MCPChatMessageEventData) {
    this.properties = {
      server_id: data.serverId,
      provider: data.provider,
      model: data.model,
      message_count: data.messageCount,
      tool_calls_count: data.toolCallsCount,
      success: data.success,
      execution_mode: data.executionMode,
      duration: data.duration,
      error: data.error,
    };
  }
}

export interface MCPServerAddedEventData {
  serverId: string;
  serverUrl: string;
  connectionType?: "http" | "sse";
  viaProxy?: boolean;
}

export class MCPServerAddedEvent implements BaseTelemetryEvent {
  name = "mcp_server_added";
  properties: Record<string, any>;

  constructor(data: MCPServerAddedEventData) {
    this.properties = {
      server_id: data.serverId,
      server_url: data.serverUrl,
      connection_type: data.connectionType,
      via_proxy: data.viaProxy,
    };
  }
}

export interface MCPServerRemovedEventData {
  serverId: string;
}

export class MCPServerRemovedEvent implements BaseTelemetryEvent {
  name = "mcp_server_removed";
  properties: Record<string, any>;

  constructor(data: MCPServerRemovedEventData) {
    this.properties = {
      server_id: data.serverId,
    };
  }
}

export interface MCPCommandPaletteOpenEventData {
  trigger: "keyboard" | "button";
}

export class MCPCommandPaletteOpenEvent implements BaseTelemetryEvent {
  name = "mcp_command_palette_open";
  properties: Record<string, any>;

  constructor(data: MCPCommandPaletteOpenEventData) {
    this.properties = {
      trigger: data.trigger,
    };
  }
}

export interface MCPToolSavedEventData {
  toolName: string;
  serverId?: string;
}

export class MCPToolSavedEvent implements BaseTelemetryEvent {
  name = "mcp_tool_saved";
  properties: Record<string, any>;

  constructor(data: MCPToolSavedEventData) {
    this.properties = {
      tool_name: data.toolName,
      server_id: data.serverId,
    };
  }
}

export interface MCPTunnelActionEventData {
  action: "start" | "stop";
  success: boolean;
  tunnelUrl?: string | null;
}

export class MCPTunnelActionEvent implements BaseTelemetryEvent {
  name = "mcp_tunnel_action";
  properties: Record<string, any>;

  constructor(data: MCPTunnelActionEventData) {
    this.properties = {
      action: data.action,
      success: data.success,
      tunnel_url: data.tunnelUrl,
    };
  }
}

export interface MCPDeployClickEventData {
  referrer: string;
}

export class MCPDeployClickEvent implements BaseTelemetryEvent {
  name = "mcp_deploy_click";
  properties: Record<string, any>;

  constructor(data: MCPDeployClickEventData) {
    this.properties = {
      referrer: data.referrer,
    };
  }
}

export interface MCPChatConfiguredEventData {
  provider: string;
  model: string;
}

export class MCPChatConfiguredEvent implements BaseTelemetryEvent {
  name = "mcp_chat_configured";
  properties: Record<string, any>;

  constructor(data: MCPChatConfiguredEventData) {
    this.properties = {
      provider: data.provider,
      model: data.model,
    };
  }
}

export interface MCPTabNavigationEventData {
  tab: string;
  previousTab: string | null;
}

export class MCPTabNavigationEvent implements BaseTelemetryEvent {
  name = "mcp_tab_navigation";
  properties: Record<string, any>;

  constructor(data: MCPTabNavigationEventData) {
    this.properties = {
      tab: data.tab,
      previous_tab: data.previousTab,
    };
  }
}

export interface MCPAddToClientEventData {
  client: string;
}

export class MCPAddToClientEvent implements BaseTelemetryEvent {
  name = "mcp_add_to_client";
  properties: Record<string, any>;

  constructor(data: MCPAddToClientEventData) {
    this.properties = {
      client: data.client,
    };
  }
}

export interface MCPSessionDurationEventData {
  durationSeconds: number;
  tabsVisited: number;
  toolsExecuted: number;
}

export class MCPSessionDurationEvent implements BaseTelemetryEvent {
  name = "mcp_session_duration";
  properties: Record<string, any>;

  constructor(data: MCPSessionDurationEventData) {
    this.properties = {
      duration_seconds: data.durationSeconds,
      tabs_visited: data.tabsVisited,
      tools_executed: data.toolsExecuted,
    };
  }
}
