/**
 * [PLUGIN-004] AI运营中心功能模块
 * 
 * 示例: 展示如何定义一个完整的功能模块
 * 
 * @module plugins/features/AIOperationsFeature
 * @created 2026-03-21
 */

import {
  RobotOutlined,
  ThunderboltOutlined,
  ScheduleOutlined,
  FileSearchOutlined,
  ShopOutlined,
  AimOutlined,
  PlayCircleOutlined,
} from '@ant-design/icons';
import type { FeatureConfig } from '../FeatureTypes';
import { FEATURES } from '@/contexts/UserContext';

const AIOperationsFeature: FeatureConfig = {
  id: FEATURES.AI_OPERATIONS,
  name: 'AI运营中心',
  description: '智能运营助手，自动化执行日常运营任务，包括选品、定价、上下架等',
  version: '1.0.0',
  icon: <RobotOutlined />,
  category: 'ai',
  
  tier: ['basic', 'pro', 'enterprise'],
  status: 'active',

  dependencies: [],

  menus: [
    {
      key: 'ai-operations',
      label: 'AI运营中心',
      icon: <RobotOutlined />,
      children: [
        {
          key: '/dashboard/operation-agent',
          label: '运营Agent',
          icon: <RobotOutlined />,
          path: '/dashboard/operation-agent',
        },
        {
          key: '/dashboard/auto-pilot',
          label: '自动驾驶',
          icon: <ThunderboltOutlined />,
          path: '/dashboard/auto-pilot',
        },
        {
          key: '/dashboard/task-center',
          label: '任务中心',
          icon: <ScheduleOutlined />,
          path: '/dashboard/task-center',
        },
        {
          key: '/dashboard/ai-decision-log',
          label: 'AI决策日志',
          icon: <FileSearchOutlined />,
          path: '/dashboard/ai-decision-log',
        },
        {
          key: '/dashboard/strategy-marketplace',
          label: '策略市场',
          icon: <ShopOutlined />,
          path: '/dashboard/strategy-marketplace',
        },
        {
          key: '/dashboard/auto-product-selection',
          label: '自动选品',
          icon: <AimOutlined />,
          path: '/dashboard/auto-product-selection',
        },
        {
          key: '/dashboard/auto-execution',
          label: '自动执行',
          icon: <PlayCircleOutlined />,
          path: '/dashboard/auto-execution',
        },
      ],
    },
  ],

  permissions: [
    {
      key: 'ai:operations',
      label: 'AI操作',
      description: '使用AI运营功能',
      defaultRoles: ['ADMIN', 'MANAGER'],
    },
    {
      key: 'ai:config',
      label: 'AI配置',
      description: '配置AI运营策略',
      defaultRoles: ['ADMIN'],
    },
    {
      key: 'ai:approve',
      label: 'AI审批',
      description: '审批AI决策',
      defaultRoles: ['ADMIN', 'MANAGER'],
    },
  ],

  settings: [
    {
      key: 'autoApproveEnabled',
      type: 'boolean',
      label: '自动审批',
      description: '启用后，AI决策将自动执行，无需人工审批',
      defaultValue: false,
    },
    {
      key: 'maxDailyTasks',
      type: 'number',
      label: '每日最大任务数',
      description: 'AI每天最多执行的任务数量',
      defaultValue: 100,
      validation: {
        min: 1,
        max: 1000,
      },
    },
    {
      key: 'decisionThreshold',
      type: 'number',
      label: '决策置信度阈值',
      description: 'AI决策置信度低于此值时需要人工确认',
      defaultValue: 0.8,
      validation: {
        min: 0,
        max: 1,
      },
    },
    {
      key: 'notificationLevel',
      type: 'select',
      label: '通知级别',
      description: 'AI操作的通知频率',
      defaultValue: 'important',
      options: [
        { label: '全部通知', value: 'all' },
        { label: '仅重要', value: 'important' },
        { label: '仅错误', value: 'error' },
        { label: '关闭', value: 'none' },
      ],
    },
    {
      key: 'enabledModules',
      type: 'multiselect',
      label: '启用的模块',
      description: '选择要启用的AI运营模块',
      defaultValue: ['pricing', 'listing', 'inventory'],
      options: [
        { label: '智能定价', value: 'pricing' },
        { label: '自动上下架', value: 'listing' },
        { label: '库存预警', value: 'inventory' },
        { label: '竞品监控', value: 'competitor' },
        { label: '广告优化', value: 'advertising' },
      ],
    },
  ],

  hooks: {
    onEnable: async () => {
      console.log('[AI Operations] Feature enabled');
    },
    onDisable: async () => {
      console.log('[AI Operations] Feature disabled');
    },
    onUpgrade: async (fromVersion) => {
      console.log(`[AI Operations] Upgraded from ${fromVersion}`);
    },
  },

  i18n: {
    'zh-CN': {
      name: 'AI运营中心',
      description: '智能运营助手，自动化执行日常运营任务',
    },
    'en-US': {
      name: 'AI Operations Center',
      description: 'Intelligent operations assistant for automating daily tasks',
    },
  },
};

export default AIOperationsFeature;

export const AutoPricingFeature: FeatureConfig = {
  id: FEATURES.AUTO_PRICING,
  name: 'AI自动定价',
  description: '基于市场分析和利润目标的智能定价系统',
  version: '1.0.0',
  icon: <ThunderboltOutlined />,
  category: 'ai',

  tier: ['basic', 'pro', 'enterprise'],
  status: 'active',

  dependencies: [
    { featureId: FEATURES.AI_OPERATIONS, required: true },
  ],

  menus: [
    {
      key: '/dashboard/product/ai-pricing',
      label: 'AI定价',
      icon: <ThunderboltOutlined />,
      path: '/dashboard/product/ai-pricing',
    },
    {
      key: '/dashboard/dynamic-pricing',
      label: '动态定价',
      icon: <ThunderboltOutlined />,
      path: '/dashboard/dynamic-pricing',
    },
  ],

  settings: [
    {
      key: 'minProfitMargin',
      type: 'number',
      label: '最低利润率',
      description: 'B2C订单最低利润率要求',
      defaultValue: 20,
      validation: { min: 0, max: 100 },
    },
    {
      key: 'b2bMinProfitMargin',
      type: 'number',
      label: 'B2B最低利润率',
      description: 'B2B订单最低利润率要求',
      defaultValue: 15,
      validation: { min: 0, max: 100 },
    },
    {
      key: 'priceAdjustFrequency',
      type: 'select',
      label: '调价频率',
      defaultValue: 'daily',
      options: [
        { label: '实时', value: 'realtime' },
        { label: '每小时', value: 'hourly' },
        { label: '每天', value: 'daily' },
        { label: '每周', value: 'weekly' },
      ],
    },
  ],
};

export const MultiShopFeature: FeatureConfig = {
  id: FEATURES.MULTI_SHOP,
  name: '多店铺管理',
  description: '统一管理多个电商平台的店铺',
  version: '1.0.0',
  icon: <ShopOutlined />,
  category: 'product',

  tier: ['basic', 'pro', 'enterprise'],
  status: 'active',

  menus: [
    {
      key: '/dashboard/product/cross-platform',
      label: '跨平台管理',
      icon: <ShopOutlined />,
      path: '/dashboard/product/cross-platform',
    },
    {
      key: '/dashboard/multi-shop-report',
      label: '多店报表',
      icon: <ShopOutlined />,
      path: '/dashboard/multi-shop-report',
    },
  ],

  settings: [
    {
      key: 'maxShops',
      type: 'number',
      label: '最大店铺数',
      defaultValue: 5,
      validation: { min: 1, max: 50 },
    },
    {
      key: 'syncInterval',
      type: 'number',
      label: '同步间隔(分钟)',
      defaultValue: 30,
      validation: { min: 5, max: 1440 },
    },
  ],
};

export const B2BTradeFeature: FeatureConfig = {
  id: FEATURES.B2B_TRADE,
  name: 'B2B贸易',
  description: '企业级批发贸易管理',
  version: '1.0.0',
  icon: <ShopOutlined />,
  category: 'b2b',

  tier: ['basic', 'pro', 'enterprise'],
  status: 'active',

  menus: [
    {
      key: 'b2b-trade',
      label: 'B2B贸易',
      icon: <ShopOutlined />,
      children: [
        {
          key: '/dashboard/merchant',
          label: '商户管理',
          path: '/dashboard/merchant',
        },
        {
          key: '/dashboard/merchant/orders',
          label: '商户订单',
          path: '/dashboard/merchant/orders',
        },
        {
          key: '/dashboard/b2b',
          label: 'B2B贸易',
          path: '/dashboard/b2b',
        },
        {
          key: '/dashboard/b2b-trade',
          label: 'B2B交易',
          path: '/dashboard/b2b-trade',
        },
        {
          key: '/dashboard/suppliers',
          label: '供应商',
          path: '/dashboard/suppliers',
        },
      ],
    },
  ],

  permissions: [
    {
      key: 'b2b:read',
      label: 'B2B查看',
      description: '查看B2B贸易数据',
      defaultRoles: ['ADMIN', 'MANAGER', 'SOURCING'],
    },
    {
      key: 'b2b:write',
      label: 'B2B编辑',
      description: '编辑B2B贸易数据',
      defaultRoles: ['ADMIN', 'MANAGER'],
    },
    {
      key: 'b2b:approve',
      label: 'B2B审批',
      description: '审批B2B订单和合同',
      defaultRoles: ['ADMIN', 'MANAGER'],
    },
  ],

  settings: [
    {
      key: 'minOrderAmount',
      type: 'number',
      label: '最低订单金额',
      defaultValue: 500,
      validation: { min: 0 },
    },
    {
      key: 'creditLimit',
      type: 'number',
      label: '默认信用额度',
      defaultValue: 10000,
      validation: { min: 0 },
    },
  ],
};

export const IndependentSiteFeature: FeatureConfig = {
  id: FEATURES.INDEPENDENT_SITE,
  name: '独立站',
  description: '独立电商网站建设和管理',
  version: '1.0.0',
  icon: <ShopOutlined />,
  category: 'independent_site',

  tier: ['pro', 'enterprise'],
  status: 'active',

  menus: [
    {
      key: 'independent-site',
      label: '独立站',
      icon: <ShopOutlined />,
      children: [
        {
          key: '/dashboard/independent-site',
          label: '站点列表',
          path: '/dashboard/independent-site',
        },
        {
          key: '/dashboard/independent-site/create',
          label: '创建站点',
          path: '/dashboard/independent-site/create',
        },
      ],
    },
  ],

  settings: [
    {
      key: 'maxSites',
      type: 'number',
      label: '最大站点数',
      defaultValue: 3,
      validation: { min: 1, max: 20 },
    },
    {
      key: 'sslEnabled',
      type: 'boolean',
      label: '启用SSL',
      defaultValue: true,
    },
  ],
};

export const AdvancedAnalyticsFeature: FeatureConfig = {
  id: FEATURES.ADVANCED_ANALYTICS,
  name: '高级数据分析',
  description: '深度数据分析和可视化报表',
  version: '1.0.0',
  icon: <FileSearchOutlined />,
  category: 'analytics',

  tier: ['pro', 'enterprise'],
  status: 'active',

  menus: [
    {
      key: '/dashboard/analytics',
      label: '数据分析',
      icon: <FileSearchOutlined />,
      path: '/dashboard/analytics',
    },
  ],

  settings: [
    {
      key: 'retentionDays',
      type: 'number',
      label: '数据保留天数',
      defaultValue: 365,
      validation: { min: 30, max: 1095 },
    },
    {
      key: 'exportFormats',
      type: 'multiselect',
      label: '导出格式',
      defaultValue: ['csv', 'excel'],
      options: [
        { label: 'CSV', value: 'csv' },
        { label: 'Excel', value: 'excel' },
        { label: 'PDF', value: 'pdf' },
        { label: 'JSON', value: 'json' },
      ],
    },
  ],
};

export const APIAccessFeature: FeatureConfig = {
  id: FEATURES.API_ACCESS,
  name: 'API访问',
  description: '开放API接口访问权限',
  version: '1.0.0',
  icon: <PlayCircleOutlined />,
  category: 'system',

  tier: ['pro', 'enterprise'],
  status: 'active',

  settings: [
    {
      key: 'rateLimit',
      type: 'number',
      label: '每分钟请求限制',
      defaultValue: 100,
      validation: { min: 10, max: 1000 },
    },
    {
      key: 'allowedIPs',
      type: 'string',
      label: 'IP白名单',
      description: '多个IP用逗号分隔，留空则不限制',
      defaultValue: '',
    },
  ],

  permissions: [
    {
      key: 'api:read',
      label: 'API读取',
      description: '通过API读取数据',
      defaultRoles: ['ADMIN', 'ANALYST'],
    },
    {
      key: 'api:write',
      label: 'API写入',
      description: '通过API写入数据',
      defaultRoles: ['ADMIN'],
    },
  ],
};
