/**
 * 节点类型 Mock 数据
 * 模拟从后端 /api/node-types 获取的节点类型元数据
 * 符合 docs/04-数据模型设计.md 规范
 */

import type { NodeTypeMetadata, NodeCategory } from '#/types/workflow'

/**
 * 节点分类配置
 */
export const NODE_CATEGORIES = [
  {
    category: 'api' as NodeCategory,
    label: 'API 调用',
    icon: 'ApiOutlined',
  },
  {
    category: 'database' as NodeCategory,
    label: '数据库',
    icon: 'DatabaseOutlined',
  },
  {
    category: 'logic' as NodeCategory,
    label: '逻辑控制',
    icon: 'BranchesOutlined',
  },
  {
    category: 'notification' as NodeCategory,
    label: '通知',
    icon: 'BellOutlined',
  },
  {
    category: 'transform' as NodeCategory,
    label: '数据转换',
    icon: 'FunctionOutlined',
  },
]

/**
 * 预定义节点类型（符合数据模型设计文档）
 */
export const MOCK_NODE_TYPES: NodeTypeMetadata[] = [
  // ========== API 调用节点 ==========
  {
    id: 'http_request',
    name: 'httpRequest',
    displayName: 'HTTP Request',
    category: 'api',
    icon: 'ApiOutlined',
    description: '发送 HTTP 请求',
    enabled: true,
    fields: [
      {
        name: 'url',
        label: 'URL',
        type: 'text',
        required: true,
        supportsExpression: true,
        placeholder: 'https://api.example.com/users/1',
      },
      {
        name: 'method',
        label: 'Method',
        type: 'select',
        options: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
        defaultValue: 'GET',
      },
      {
        name: 'headers',
        label: 'Headers',
        type: 'key_value',
        supportsFieldMapping: true,
      },
      {
        name: 'body',
        label: 'Body',
        type: 'code',
        language: 'json',
        supportsExpression: true,
        placeholder: '{\n  "key": "value"\n}',
      },
      {
        name: 'timeout',
        label: 'Timeout(ms)',
        type: 'number',
        defaultValue: 30000,
      },
    ],
    outputSchema: {
      type: 'object',
      properties: {
        statusCode: {
          type: 'number',
          description: 'HTTP 状态码',
        },
        body: {
          type: 'object',
          description: '响应体',
          properties: {
            id: {
              type: 'number',
            },
            name: {
              type: 'string',
            },
            email: {
              type: 'string',
            },
            data: {
              type: 'array',
              items: {
                type: 'object',
              },
            },
          },
        },
        headers: {
          type: 'object',
          description: '响应头',
        },
        elapsed: {
          type: 'number',
          description: '请求耗时（毫秒）',
        },
      },
    },
  },

  // ========== 数据库节点 ==========
  {
    id: 'database_query',
    name: 'databaseQuery',
    displayName: 'Database Query',
    category: 'database',
    icon: 'DatabaseOutlined',
    description: '执行数据库查询',
    enabled: true,
    fields: [
      {
        name: 'connection',
        label: 'Connection',
        type: 'select',
        required: true,
        options: ['prod_db', 'test_db', 'dev_db'],
        defaultValue: 'dev_db',
      },
      {
        name: 'query',
        label: 'SQL Query',
        type: 'code',
        language: 'sql',
        required: true,
        supportsExpression: true,
        placeholder: 'SELECT * FROM users WHERE email = ?',
      },
      {
        name: 'parameters',
        label: 'Parameters',
        type: 'key_value',
        supportsFieldMapping: true,
      },
    ],
    outputSchema: {
      type: 'object',
      properties: {
        rows: {
          type: 'array',
          description: '查询结果行',
          items: {
            type: 'object',
          },
        },
        rowCount: {
          type: 'number',
          description: '返回行数',
        },
        affectedRows: {
          type: 'number',
          description: '影响行数',
        },
      },
    },
  },

  // ========== 逻辑控制节点 ==========
  {
    id: 'condition',
    name: 'condition',
    displayName: 'Condition',
    category: 'logic',
    icon: 'BranchesOutlined',
    description: '条件判断分支',
    enabled: true,
    fields: [
      {
        name: 'condition',
        label: 'Condition',
        type: 'text',
        required: true,
        supportsExpression: true,
        placeholder: '${nodes.n1.output.statusCode} == 200',
      },
      {
        name: 'description',
        label: 'Description',
        type: 'textarea',
        placeholder: '描述此条件的业务逻辑',
      },
    ],
    outputSchema: {
      type: 'object',
      properties: {
        result: {
          type: 'boolean',
          description: '条件判断结果',
        },
        branchTaken: {
          type: 'string',
          description: '执行的分支（true/false）',
        },
      },
    },
  },

  // ========== 通知节点 ==========
  {
    id: 'send_mail',
    name: 'sendMail',
    displayName: 'Send Email',
    category: 'notification',
    icon: 'MailOutlined',
    description: '发送电子邮件',
    enabled: true,
    fields: [
      {
        name: 'to',
        label: 'To',
        type: 'text',
        required: true,
        supportsFieldMapping: true,
        placeholder: 'user@example.com',
      },
      {
        name: 'cc',
        label: 'CC',
        type: 'text',
        supportsFieldMapping: true,
      },
      {
        name: 'subject',
        label: 'Subject',
        type: 'text',
        required: true,
        supportsExpression: true,
        placeholder: '审批通知',
      },
      {
        name: 'content',
        label: 'Content',
        type: 'textarea',
        required: true,
        supportsExpression: true,
        placeholder: '邮件内容...',
      },
      {
        name: 'attachments',
        label: 'Attachments',
        type: 'text',
        supportsFieldMapping: true,
      },
    ],
    outputSchema: {
      type: 'object',
      properties: {
        messageId: {
          type: 'string',
          description: '消息 ID',
        },
        success: {
          type: 'boolean',
          description: '是否发送成功',
        },
        sentAt: {
          type: 'string',
          description: '发送时间',
        },
      },
    },
  },

  // ========== 数据转换节点 ==========
  {
    id: 'transform',
    name: 'transform',
    displayName: 'Data Transform',
    category: 'transform',
    icon: 'FunctionOutlined',
    description: '数据转换和处理',
    enabled: true,
    fields: [
      {
        name: 'operation',
        label: 'Operation',
        type: 'select',
        required: true,
        options: ['map', 'filter', 'reduce', 'custom'],
        defaultValue: 'map',
      },
      {
        name: 'script',
        label: 'Script',
        type: 'code',
        language: 'javascript',
        required: true,
        placeholder: '// 数据转换逻辑\nreturn data.map(item => ({ ...item, processed: true }))',
      },
      {
        name: 'input',
        label: 'Input Data',
        type: 'text',
        supportsFieldMapping: true,
        placeholder: '${nodes.n1.output.body.data}',
      },
    ],
    outputSchema: {
      type: 'object',
      properties: {
        result: {
          type: 'object',
          description: '转换后的数据',
        },
        metadata: {
          type: 'object',
          description: '元数据',
          properties: {
            recordCount: {
              type: 'number',
              description: '记录数量',
            },
            timestamp: {
              type: 'string',
              description: '处理时间',
            },
          },
        },
      },
    },
  },

  // ========== 审批节点 ==========
  {
    id: 'approval',
    name: 'approval',
    displayName: 'Approval',
    category: 'logic',
    icon: 'CheckCircleOutlined',
    description: '人工审批节点',
    enabled: true,
    fields: [
      {
        name: 'assignee',
        label: 'Assignee',
        type: 'text',
        required: true,
        supportsExpression: true,
        placeholder: '${workflow.input.approver}',
      },
      {
        name: 'title',
        label: 'Title',
        type: 'text',
        required: true,
        defaultValue: '审批申请',
      },
      {
        name: 'description',
        label: 'Description',
        type: 'textarea',
        supportsExpression: true,
      },
      {
        name: 'formFields',
        label: 'Form Fields',
        type: 'code',
        language: 'json',
        placeholder: '[{"id": "approved", "label": "同意?", "type": "boolean", "required": true}]',
      },
    ],
    outputSchema: {
      type: 'object',
      properties: {
        approved: {
          type: 'boolean',
          description: '是否批准',
        },
        approver: {
          type: 'string',
          description: '审批人',
        },
        approvedAt: {
          type: 'string',
          description: '审批时间',
        },
        comment: {
          type: 'string',
          description: '审批意见',
        },
      },
    },
  },
]

/**
 * 根据 ID 获取节点类型元数据
 */
export function getNodeTypeMetadata(id: string): NodeTypeMetadata | undefined {
  return MOCK_NODE_TYPES.find((t) => t.id === id)
}

/**
 * 根据分类获取节点类型
 */
export function getNodeTypesByCategory(category: NodeCategory): NodeTypeMetadata[] {
  return MOCK_NODE_TYPES.filter((t) => t.category === category)
}

/**
 * 获取所有节点类型分组
 */
export function getNodeTypesGrouped() {
  const grouped: Record<NodeCategory, NodeTypeMetadata[]> = {} as any
  
  MOCK_NODE_TYPES.forEach((nodeType) => {
    if (!grouped[nodeType.category]) {
      grouped[nodeType.category] = []
    }
    grouped[nodeType.category].push(nodeType)
  })
  
  return grouped
}
