
import React, { ReactNode } from 'react';

// ============== Types ==============

export enum EditorType {
  JSON_REST = 'JSON_REST',
  YAML_CONFIG = 'YAML_CONFIG',
  EMAIL_HTML = 'EMAIL_HTML',
  SCRIPT_JS = 'SCRIPT_JS',
  DB_QUERY = 'DB_QUERY',
  XML_TEMPLATE = 'XML_TEMPLATE',
  REST_API = 'REST_API',
  AGENT = 'AGENT'
}

export type SqlDialect = 'postgres' | 'mysql' | 'mssql' | 'duckdb' | 'seekdb' | 'postgres-vector';

export interface DbConnection {
  id: string;
  name: string;
  dialect: SqlDialect;
  connectionString: string;
}

export interface UserFunction {
  id: string;
  name: string;
  params: string[];
  body: string;
}

export interface HostImage {
  id: string;
  name: string;
  url: string;
}

export interface AuthConfig {
  type: 'none' | 'basic' | 'bearer' | 'apiKey';
  username?: string;
  password?: string;
  token?: string;
  apiKeyKey?: string;
  apiKeyValue?: string;
  apiKeyIn?: 'header' | 'query';
}

export interface NamedAuthConfig extends AuthConfig {
  id: string;
  name: string;
}

export interface ApiSource {
  id: string;
  name: string;
  baseUrl: string;
  specUrl?: string;
  spec?: any;
  lastFetched: number;
}

export type MemoryBackend = 'NONE' | 'IN_MEMORY' | 'REDIS' | 'POSTGRES' | 'MYSQL';

export interface McpServer {
  id: string;
  name: string;
  config: string;
}

export interface AgentExample {
  input: string;
  output: string;
}

export interface AgentConfig {
  id: string;
  name: string;
  description: string;
  agentType: 'conversational' | 'task' | 'orchestrator';
  provider: 'google' | 'openai' | 'anthropic' | 'ollama';
  modelId: string;
  temperature: number;
  maxTokens: number;
  topP: number;
  jsonMode: boolean;
  mode: 'STANDARD' | 'DAISY_CHAIN';
  systemMessage: string;
  userMessageInput: string;
  fewShotExamples: AgentExample[];
  outputParser: 'TEXT' | 'JSON' | 'AUTO_FIX';
  structuredOutputMethod: 'SCHEMA' | 'EXAMPLE';
  jsonSchemaDefinition: string;
  structuredOutputExample: string;
  autoRepair: boolean;
  connectedTools: string[];
  mcpServers: McpServer[];
  memoryBackend: MemoryBackend;
  memoryOptions?: { connectionString?: string };
  sessionId?: string;
  contextWindowLimit?: number;
}

export interface EmailSnippet {
  name: string;
  description: string;
  icon: ReactNode;
  content: string;
}

export interface EmailSnippetGroup {
  id: string;
  title: string;
  snippets: EmailSnippet[];
}

export interface XmlSnippet {
  name: string;
  description: string;
  icon: ReactNode;
  content: string;
}

export interface XmlSnippetGroup {
  id: string;
  title: string;
  snippets: XmlSnippet[];
}

export interface SqlItem {
  name: string;
  desc: string;
  value?: string;
}

export interface SqlGroup {
  id: string;
  title: string;
  items: SqlItem[];
}

export type SqlLibrary = Record<string, SqlGroup[]>;

export type RestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';

export interface RestParam {
  id: string;
  key: string;
  value: string;
  enabled: boolean;
  description?: string;
}

export interface RestRequest {
  id: string;
  name: string;
  meta?: { origin: 'swagger' | 'custom', sourceId?: string, operationId?: string };
  method: RestMethod;
  url: string;
  pathParams: RestParam[];
  params: RestParam[];
  headers: RestParam[];
  auth: AuthConfig;
  bodyType: 'none' | 'json' | 'xml' | 'text';
  body: string;
}

export interface RestResponse {
  status: number;
  statusText: string;
  time: number;
  size: number;
  headers: Record<string, string>;
  data: string;
  timestamp: number;
}

export interface VariableNode {
  key: string;
  path: string;
  type: 'string' | 'number' | 'boolean' | 'object' | 'array' | 'null';
  value: any;
  children?: VariableNode[];
}

// ============== Common Editor Props ==============

interface CommonEditorProps {
  variables: Record<string, any>;
  variablesJson: string;
  onVariablesChange: (json: string) => void;
  variableError: string | null;
  functions: UserFunction[];
  onFunctionsChange: (functions: UserFunction[]) => void;
}

// ============== Editor Components ==============

export interface JsonEditorProps extends CommonEditorProps {
  content: string;
  onChange: (content: string) => void;
  onAiAssist?: (prompt: string) => Promise<string>;
}
export declare const JsonEditor: React.FC<JsonEditorProps>;

export interface YamlEditorProps extends CommonEditorProps {
  content: string;
  onChange: (content: string) => void;
  onAiAssist?: (prompt: string) => Promise<string>;
}
export declare const YamlEditor: React.FC<YamlEditorProps>;

export interface HtmlEditorProps extends CommonEditorProps {
  content: string;
  onChange: (content: string) => void;
  emailBlockGroups: EmailSnippetGroup[];
  hostImages: HostImage[];
  onAddImage: (image: HostImage) => void;
  onDeleteImage: (id: string) => void;
  onAiAssist?: (prompt: string) => Promise<string>;
}
export declare const HtmlEditor: React.FC<HtmlEditorProps>;

export interface ScriptEditorProps extends CommonEditorProps {
  content: string;
  onChange: (content: string) => void;
  onAiAssist?: (prompt: string) => Promise<string>;
}
export declare const ScriptEditor: React.FC<ScriptEditorProps>;

export interface DbQueryEditorProps extends CommonEditorProps {
  content: string;
  onChange: (content: string) => void;
  connections: DbConnection[];
  activeConnectionId: string;
  onActiveConnectionChange: (id: string) => void;
  onUpdateConnections: (connections: DbConnection[]) => void;
  onExecuteQuery: (query: string, connection: DbConnection) => void;
  isExecuting: boolean;
  executionResult: string | null;
  onCancelQuery: () => void;
  sqlLibrary: SqlLibrary;
  onAiAssist?: (prompt: string) => Promise<string>;
}
export declare const DbQueryEditor: React.FC<DbQueryEditorProps>;

export interface XmlEditorProps extends CommonEditorProps {
  content: string;
  onChange: (content: string) => void;
  xmlBlockGroups: XmlSnippetGroup[];
  onAiAssist?: (prompt: string) => Promise<string>;
}
export declare const XmlEditor: React.FC<XmlEditorProps>;

export interface RestEditorProps extends CommonEditorProps {
  authCredentials: NamedAuthConfig[];
  onAuthCredentialsChange: (credentials: NamedAuthConfig[]) => void;
  apiSources: ApiSource[];
  onApiSourcesChange: (sources: ApiSource[]) => void;
  onAiAssist?: (prompt: string) => Promise<string>;
}
export declare const RestEditor: React.FC<RestEditorProps>;

export interface AgentEditorProps extends CommonEditorProps {
  config: AgentConfig;
  onChange: (config: AgentConfig) => void;
  onAiAssist?: (prompt: string) => Promise<string>;
  onRun: () => void;
  isRunning: boolean;
  runError: string | null;
  externalRunTrigger?: { message: string; timestamp: number } | null;
}
export declare const AgentEditor: React.FC<AgentEditorProps>;

// ============== Shared UI Components ==============

export declare const CodeEditor: React.FC<any>;
export declare const TreeView: React.FC<any>;
export declare const VariableTree: React.FC<any>;
export declare const FunctionPanel: React.FC<any>;
export declare const ToolsPanel: React.FC<any>;
export declare const AiChatPanel: React.FC<any>;
export declare const KeyValueEditor: React.FC<any>;

// ============== Constants ==============

export declare const DEFAULT_EMAIL_SNIPPET_GROUPS: EmailSnippetGroup[];
export declare const DEFAULT_SQL_DIALECT_DATA: SqlLibrary;
export declare const DEFAULT_XML_SNIPPET_GROUPS: XmlSnippetGroup[];

// ============== Defaults ==============

export declare const DEFAULT_VARIABLES_JSON: string;
export declare const DEFAULT_JSON_CONTENT: string;
export declare const DEFAULT_YAML_CONTENT: string;
export declare const DEFAULT_HTML_CONTENT: string;
export declare const DEFAULT_SCRIPT_CONTENT: string;
export declare const DEFAULT_SQL_CONTENT: string;
export declare const DEFAULT_XML_CONTENT: string;
export declare const DEFAULT_FUNCTIONS: UserFunction[];
export declare const DEFAULT_DB_CONNECTIONS: DbConnection[];
export declare const DEFAULT_HOST_IMAGES: HostImage[];
export declare const DEFAULT_AGENT_CONFIG: AgentConfig;

// ============== AI Service ==============

export declare function generateJsonAssistResponse(prompt: string, content: string, variables: string, functions: UserFunction[]): Promise<string>;
export declare function generateYamlAssistResponse(prompt: string, content: string, variables: string, functions: UserFunction[]): Promise<string>;
export declare function generateHtmlAssistResponse(prompt: string, content: string, variables: string, functions: UserFunction[], images: HostImage[]): Promise<string>;
export declare function generateScriptAssistResponse(prompt: string, content: string, variables: string, functions: UserFunction[]): Promise<string>;
export declare function generateSqlAssistResponse(prompt: string, content: string, variables: string, functions: UserFunction[], connection?: DbConnection): Promise<string>;
export declare function generateXmlAssistResponse(prompt: string, content: string, variables: string, functions: UserFunction[]): Promise<string>;
export declare function generateRestAssistResponse(prompt: string, variables: string, functions: UserFunction[], apiSources: ApiSource[]): Promise<string>;
export declare function runAgentSimulation(config: AgentConfig, userMessage: string, variables: Record<string, any>, functions: UserFunction[]): Promise<string>;
export declare function generateAgentAssistResponse(prompt: string, config: AgentConfig): Promise<string>;
