/**
 * Gen UI 类型定义
 * 定义 AI 返回的 UI Schema 结构
 */

import type { SandboxExecutionResult } from '@/types/sandbox';

// 基础组件类型
export type ComponentType = 
  | 'stats_row'
  | 'data_table'
  | 'chart'
  | 'alert'
  | 'action_buttons'
  | 'approval_card'
  | 'insight_card'
  | 'progress_bar'
  | 'text'
  | 'divider'
  | 'container'
  // Agent 相关组件
  | 'agent_status'
  | 'agent_capabilities'
  | 'agent_thinking'
  | 'agent_settings_card'
  | 'agent_settings_list'
  // Sandbox 相关组件
  | 'sandbox_result'
  // 动态列表组件 (GenUI 化)
  | 'insight_feed'
  | 'approval_list';

// 统计项
export interface StatItem {
  label: string;
  value: number | string;
  trend?: string;
  trendDirection?: 'up' | 'down' | 'neutral';
  icon?: string;
  color?: 'default' | 'success' | 'warning' | 'error' | 'info';
}

// 表格列定义
export interface TableColumn {
  key: string;
  label: string;
  type?: 'text' | 'number' | 'date' | 'status' | 'action';
  width?: string;
}

// 表格行数据
export interface TableRow {
  id: string;
  [key: string]: any;
}

// 图表数据点
export interface ChartDataPoint {
  label: string;
  value: number;
  [key: string]: any;
}

// 操作按钮
export interface ActionButton {
  id: string;
  label: string;
  action: string;
  variant?: 'default' | 'secondary' | 'destructive' | 'outline' | 'ghost' | 'link';
  icon?: string;
  disabled?: boolean;
  params?: Record<string, any>;
}

// 审批卡片数据
export interface ApprovalData {
  id: string;
  title: string;
  description: string;
  type: string;
  priority?: 'low' | 'medium' | 'high' | 'urgent';
  createdAt: string;
  metadata?: Record<string, any>;
}

// 洞察卡片数据
export interface InsightData {
  id: string;
  type: 'info' | 'warning' | 'success' | 'error';
  title: string;
  description: string;
  action?: ActionButton;
}

// 组件属性映射
export interface ComponentPropsMap {
  stats_row: {
    items: StatItem[];
    columns?: number;
  };
  data_table: {
    title?: string;
    columns: TableColumn[];
    data: TableRow[];
    actions?: ActionButton[];
    pagination?: boolean;
    pageSize?: number;
  };
  chart: {
    title?: string;
    type: 'bar' | 'line' | 'pie' | 'area';
    data: ChartDataPoint[];
    xKey?: string;
    yKey?: string;
    height?: number;
  };
  alert: {
    type: 'info' | 'warning' | 'success' | 'error';
    title: string;
    description?: string;
    dismissible?: boolean;
  };
  action_buttons: {
    buttons: ActionButton[];
    layout?: 'horizontal' | 'vertical';
    align?: 'left' | 'center' | 'right';
  };
  approval_card: {
    data: ApprovalData;
    onApprove?: string;
    onReject?: string;
  };
  insight_card: {
    data: InsightData;
  };
  progress_bar: {
    label: string;
    value: number;
    max: number;
    showPercentage?: boolean;
    color?: 'default' | 'success' | 'warning' | 'error';
  };
  text: {
    content: string;
    variant?: 'h1' | 'h2' | 'h3' | 'h4' | 'body' | 'caption' | 'muted';
  };
  divider: {
    margin?: 'sm' | 'md' | 'lg';
  };
  container: {
    layout?: 'vertical' | 'horizontal' | 'grid';
    gap?: 'sm' | 'md' | 'lg';
    columns?: number;
    children: UIComponent[];
  };
  // Sandbox 组件属性
  sandbox_result: {
    result: SandboxExecutionResult;
    showRerun?: boolean;
    showCancel?: boolean;
  };
  // 动态列表组件属性
  insight_feed: {
    title?: string;
    insights: Array<{
      id: string;
      type: 'opportunity' | 'warning' | 'auto_executed' | 'suggestion' | 'success' | 'info' | 'alert' | 'trend';
      title: string;
      description: string;
      metric?: { label: string; value: string; trend?: 'up' | 'down' | 'neutral' };
      actionLabel?: string;
      actionId?: string;
      timestamp?: string;
      priority?: 'low' | 'medium' | 'high' | 'urgent';
      agent?: string;
    }>;
    layout?: 'list' | 'grid' | 'compact';
    height?: string;
    showRefresh?: boolean;
    emptyText?: string;
    groupBy?: 'type' | 'priority' | 'agent' | 'none';
  };
  approval_list: {
    title?: string;
    approvals: Array<{
      id: string;
      type: 'marketing' | 'aftersales' | 'flash_sale' | 'group_buy' | 'product';
      title: string;
      description: string;
      agent: string;
      priority: 'low' | 'medium' | 'high' | 'urgent';
      amount?: number;
      timestamp?: string;
      metadata?: Record<string, any>;
    }>;
    layout?: 'list' | 'cards' | 'compact';
    height?: string;
    showRefresh?: boolean;
    groupBy?: 'type' | 'priority' | 'agent' | 'none';
    emptyText?: string;
    showActions?: boolean;
  };
}

// UI 组件定义
export interface UIComponent<T extends ComponentType = ComponentType> {
  id?: string;
  type: T;
  props: T extends keyof ComponentPropsMap ? ComponentPropsMap[T] : Record<string, any>;
}

// UI Schema 根结构
export interface UISchema {
  version?: string;
  layout?: 'vertical' | 'horizontal' | 'grid';
  gap?: 'sm' | 'md' | 'lg';
  components: UIComponent[];
}

// Dify 返回的完整响应
export interface GenUIResponse {
  answer: string;
  ui_schema?: UISchema;
  actions?: Array<{
    type: string;
    params: Record<string, any>;
  }>;
  conversation_id?: string;
  message_id?: string;
}

// 组件渲染上下文
export interface RenderContext {
  onAction?: (action: string, params?: Record<string, any>) => void;
  theme?: 'light' | 'dark';
  loading?: boolean;
  agentType?: 'marketing' | 'finance' | 'service' | 'product' | 'manager';
}

// 组件注册表类型
export type ComponentRenderer<T extends ComponentType = ComponentType> = React.ComponentType<{
  props: T extends keyof ComponentPropsMap ? ComponentPropsMap[T] : Record<string, any>;
  context: RenderContext;
}>;

export type ComponentRegistry = {
  [K in ComponentType]?: ComponentRenderer<K>;
};
