/* eslint-disable @typescript-eslint/no-explicit-any */
/*
 * Copyright (c) 2025 Bytedance, Inc. and its affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * Agent Web UI implementation type
 *
 * - `static`: local static directory.
 * - `remote`: remote web ui implementation.
 */
export type AgentWebUIImplementationType = 'static' | 'remote';

/**
 * Navigation item icon type
 */
export type WorkspaceNavItemIcon =
  | 'code'
  | 'monitor'
  | 'terminal'
  | 'browser'
  | 'desktop'
  | 'default';

/**
 * Navigation item behavior type
 */
export type WorkspaceNavItemBehavior = 'new-page' | 'embed-frame';

/**
 * Navigation item configuration for workspace
 */
export interface WorkspaceNavItem {
  /**
   * Navigation item title displayed on the button
   */
  title: string;
  /**
   * Link URL to open in new tab or embed in workspace
   */
  link: string;
  /**
   * Icon type for the navigation item
   * @defaultValue 'default'
   */
  icon?: WorkspaceNavItemIcon;
  /**
   * Behavior when clicking the navigation item
   * @defaultValue 'new-page'
   */
  behavior?: WorkspaceNavItemBehavior;
  /**
   * Auto-activate the navigation item when true
   * Only applicable when behavior is 'embed-frame'
   * Can be a boolean or a string expression that evaluates runtime settings
   * String expressions can reference runtimeSettings properties and use JavaScript operators
   * @defaultValue false
   * @example
   * true // Static boolean
   * "runtimeSettings.devMode === true" // Simple expression
   * "runtimeSettings.sessionType === 'debugging' || runtimeSettings.autoActivateVNC === true" // Complex expression
   */
  autoActive?: boolean | string;
}

/**
 * Layout mode configuration
 */
export type LayoutMode = 'default' | 'narrow-chat';

/**
 * Layout configuration options
 */
export interface LayoutConfig {
  /**
   * Default layout mode
   * @defaultValue 'default'
   */
  defaultLayout?: LayoutMode;
  /**
   * Enable layout switch button in toolbar
   * @defaultValue false
   */
  enableLayoutSwitchButton?: boolean;
  /**
   * Enable sidebar display
   * @defaultValue true
   */
  enableSidebar?: boolean;
  /**
   * Enable home route registration
   * @defaultValue true
   */
  enableHome?: boolean;
}

/**
 * Workspace configuration options
 */
export interface WorkspaceConfig {
  /**
   * Navigation items to display in the workspace header
   */
  navItems?: WorkspaceNavItem[];
}

/**
 * GUI Agent configuration for web UI
 */
export interface TarkoWebUIGUIAgentConfig {
  /**
   * Default GUI Agent screenshot rendering strategy
   *
   * - `both`: Default to display both before and after screenshots for action comparison
   * - `beforeAction`: Default to display only beforeAction screenshot, suitable for agents that take screenshots in onEachAgentLoopStart (e.g., Agent-TARS)
   * - `afterAction`: Default to display only afterAction screenshot, suitable for agents that take screenshots in onEachAgentLoopEnd (e.g., Omni-TARS)
   *
   * Note: Mouse cursor overlay is only rendered on beforeAction screenshots as it indicates where the action will be performed
   *
   * @defaultValue 'afterAction'
   */
  defaultScreenshotRenderStrategy?: 'both' | 'beforeAction' | 'afterAction';
  /**
   * Whether to enable runtime screenshot rendering strategy switch
   *
   * When enabled, users can dynamically switch between different screenshot display modes (both/beforeAction/afterAction)
   * during runtime through UI controls. When disabled, only the default strategy is used.
   *
   * @defaultValue false
   */
  enableScreenshotRenderStrategySwitch?: boolean;
  /**
   * Whether to enable GUI Agent action rendering
   *
   * Since actions are generated by the GUI Agent model based on beforeActionImage,
   * by design, GUI actions should only be rendered on BeforeActionImage.
   * When set to false, it will hide the GUI operation details card (thinking process, steps, action commands, etc.)
   *
   * @defaultValue true
   */
  renderGUIAction?: boolean;
  /**
   * Whether to render browser shell around screenshots
   *
   * - `true`: Display screenshots wrapped in browser shell UI
   * - `false`: Display screenshots directly without browser shell, suitable for screenshots that already contain browser chrome or computer use scenarios
   *
   * @defaultValue true
   */
  renderBrowserShell?: boolean;
}

/**
 * Welcome card configuration
 */
export interface WelcomeCard {
  /**
   * Card title
   */
  title: string;
  /**
   * Card category for grouping
   */
  category: string;
  /**
   * Card prompt content
   */
  prompt?: string;
  /**
   * Card background image URL
   */
  image?: string;
  /**
   * Agent options to pass when creating session
   */
  agentOptions?: Record<string, any>;
}

/**
 * Debug configuration options
 */
export interface DebugConfig {
  /**
   * Enable Event Stream Viewer for debugging
   * Shows real-time event stream in a modal accessible from sidebar
   *
   * @defaultValue false
   */
  enableEventStreamViewer?: boolean;
}

/**
 * Base agent implementation interface
 */
export interface BaseAgentWebUIImplementation {
  /**
   * Agent implementation type
   *
   * @defaultValue static
   */
  type?: AgentWebUIImplementationType;
  /**
   * Web UI Logo
   *
   * @defaultValue Tarko logo
   */
  logo?: string;
  /**
   * Web UI site title, usually displayed in the upper right corner of the navbar
   * also used in meta.
   *
   * @defaultValue Agent Name
   */
  title?: string;
  /**
   * Web UI Sub title
   *
   * @defaultValue Agent Subtitle, Subtitle, for Home or SEO
   */
  subtitle?: string;
  /**
   * Web UI hero title, usually displayed on the home page, The project's positioning
   * and welcome message, telling people your positioning
   */
  welcomTitle?: string;
  /**
   * Welcome prompts
   */
  welcomePrompts?: string[];
  /**
   * Welcome cards configuration
   */
  welcomeCards?: WelcomeCard[];
  /**
   * Enable contextual file selector with @ syntax
   * When enabled, users can type @ in the input to search and select workspace files/directories
   *
   * @defaultValue false
   */
  enableContextualSelector?: boolean;
  /**
   * Base path for routing deployment
   * Supports both static paths and regex patterns
   *
   * @example
   * base: "/agent-ui"           // Static path
   * base: "/tenant-.+"         // Regex pattern
   * base: "/(foo|bar)/app"     // Regex with groups
   */
  base?: string;
  /**
   * Workspace configuration
   */
  workspace?: WorkspaceConfig;
  /**
   * Layout configuration
   */
  layout?: LayoutConfig;
  /**
   * GUI Agent configuration for web UI
   */
  guiAgent?: TarkoWebUIGUIAgentConfig;
  /**
   * Debug configuration
   */
  debug?: DebugConfig;
}

/**
 * Static implementation
 */
export interface StaticAgentWebUIImplementation extends BaseAgentWebUIImplementation {
  type?: 'static';
  /**
   * Web UI Static Path, example implementation: `@tarko/agent-ui`.
   */
  staticPath: string;
}

/**
 * Remote implementation (TODO)
 */
export interface RemoteAgentWebUIImplementation extends BaseAgentWebUIImplementation {
  type?: 'remote';
  remoteUrl?: string;
}

/**
 * Union type for all agent implementations
 */
export type AgentWebUIImplementation =
  | StaticAgentWebUIImplementation
  | RemoteAgentWebUIImplementation;

/**
 * Utility type to extract implementation by type
 */
export type AgentWebUIImplementationByType<T extends AgentWebUIImplementationType> =
  T extends 'static'
    ? StaticAgentWebUIImplementation
    : T extends 'remote'
      ? RemoteAgentWebUIImplementation
      : never;

/**
 * Type guard to check if implementation is of specific type
 */
export function isAgentWebUIImplementationType<T extends AgentWebUIImplementationType>(
  implementation: AgentWebUIImplementation,
  type: T,
): implementation is AgentWebUIImplementationByType<T> {
  return implementation.type === type;
}
