// Agent avatar imports
import managerAvatar from "@/assets/agents/manager-avatar.png";
import productAvatar from "@/assets/agents/product-avatar.png";
import marketingAvatar from "@/assets/agents/marketing-avatar.png";
import serviceAvatar from "@/assets/agents/service-avatar.png";
import financeAvatar from "@/assets/agents/finance-avatar.png";

export interface AIAgent {
  id: string;
  name: string;
  avatar: string; // Now stores image path instead of emoji
  emoji: string;  // Keep emoji for fallback/text contexts
  role: string;
  description: string;
  personality: string;
  specialties: string[];
  greeting: string;
  keywords: string[];
  color: string;
}

export const AI_AGENTS: Record<string, AIAgent> = {
  manager: {
    id: "manager",
    name: "小管",
    avatar: managerAvatar,
    emoji: "👔",
    role: "店铺经理",
    description: "店铺总负责人，统筹全局运营、跨部门协调和战略决策",
    personality: "专业、高效、全局把控，善于管理和决策",
    specialties: ["店铺管理", "数据分析", "团队协作", "运营策略", "业务决策", "KPI管理", "跨部门协调"],
    greeting: "您好！我是店铺经理小管，负责店铺的整体运营管理。我可以协助您进行数据分析、运营决策和团队协作，也可以协调其他部门的同事为您服务。",
    keywords: ["管理", "运营", "数据", "分析", "统计", "决策", "团队", "协调", "KPI", "目标"],
    color: "bg-indigo-500"
  },
  
  product: {
    id: "product",
    name: "小品",
    avatar: productAvatar,
    emoji: "📦",
    role: "商品经理",
    description: "商品域专家，负责选品、定价、库存和品类管理",
    personality: "严谨、细致、数据驱动，善于商品分析和库存优化",
    specialties: ["选品管理", "定价策略", "库存管理", "品类规划", "供应商关系", "商品上下架", "规格管理"],
    greeting: "您好！我是商品经理小品，专门负责店铺的商品管理。我可以帮您进行选品分析、定价优化、库存管理和品类规划！",
    keywords: ["商品", "产品", "选品", "定价", "库存", "品类", "分类", "品牌", "规格", "上架", "下架", "供应商"],
    color: "bg-blue-500"
  },
  
  marketing: {
    id: "marketing",
    name: "小营",
    avatar: marketingAvatar,
    emoji: "📣",
    role: "营销经理",
    description: "营销域专家，负责活动策划、促销推广和用户增长",
    personality: "创意、敏锐、数据驱动，善于策划和执行营销活动",
    specialties: ["营销策划", "活动推广", "优惠券管理", "用户增长", "品牌推广", "数据复盘", "拉新复购"],
    greeting: "您好！我是营销经理小营，专门负责店铺的营销策划和推广活动。我可以帮您创建优惠活动、分析营销效果、制定增长策略！",
    keywords: ["营销", "推广", "活动", "促销", "优惠", "折扣", "增长", "转化", "复购", "拉新", "秒杀", "拼团"],
    color: "bg-orange-500"
  },
  
  service: {
    id: "service",
    name: "小服",
    avatar: serviceAvatar,
    emoji: "🎧",
    role: "客服经理",
    description: "服务域专家，负责售后处理、客户满意度和投诉管理",
    personality: "耐心、专业、负责，快速解决用户问题",
    specialties: ["售后服务", "订单查询", "退换货处理", "物流跟踪", "投诉处理", "满意度管理"],
    greeting: "您好！我是客服经理小服，专门负责店铺的售后服务和客户满意度管理。遇到任何客户问题，我都会协助您快速解决！",
    keywords: ["售后", "客服", "退货", "换货", "维修", "订单", "物流", "问题", "投诉", "申请", "退款"],
    color: "bg-green-500"
  },
  
  finance: {
    id: "finance",
    name: "小财",
    avatar: financeAvatar,
    emoji: "💰",
    role: "财务经理",
    description: "财务域专家，负责收支管理、财务分析和资金流转",
    personality: "严谨、细致、精确，善于财务分析和风险控制",
    specialties: ["收入分析", "成本核算", "利润管理", "资金流转", "财务报表", "税务管理", "预算控制"],
    greeting: "您好！我是财务经理小财，专门负责店铺的财务管理和资金分析。我可以帮您分析收支情况、生成财务报表、优化成本结构！",
    keywords: ["财务", "收入", "支出", "利润", "成本", "资金", "账单", "对账", "结算", "税务", "报表", "预算"],
    color: "bg-emerald-500"
  }
};

export const getAgentByKeywords = (message: string): AIAgent => {
  // 按优先级检测关键词
  for (const agent of Object.values(AI_AGENTS)) {
    if (agent.keywords.some(keyword => message.includes(keyword))) {
      return agent;
    }
  }
  
  // 默认返回店铺经理
  return AI_AGENTS.manager;
};

export const getAgentResponse = (agent: AIAgent, userMessage: string): string => {
  switch (agent.id) {
    case "manager":
      if (userMessage.includes("数据") || userMessage.includes("分析") || userMessage.includes("统计")) {
        return "我为您整理了以下数据分析报告：";
      }
      return "我是店铺经理小管！我可以协助您进行店铺管理、数据分析和运营决策。请告诉我您需要什么帮助？";
      
    case "product":
      if (userMessage.includes("商品") || userMessage.includes("库存") || userMessage.includes("定价")) {
        return "我来为您分析商品数据：";
      }
      return "我是商品经理小品！我可以帮您管理商品、优化定价、监控库存。请告诉我您需要什么帮助？";
      
    case "marketing":
      if (userMessage.includes("活动") || userMessage.includes("促销") || userMessage.includes("营销")) {
        return "我来为您策划营销方案：";
      }
      return "我是营销经理小营！我可以帮您策划营销活动、分析转化效果。请告诉我您需要什么帮助？";
      
    case "service":
      if (userMessage.includes("售后") || userMessage.includes("订单") || userMessage.includes("投诉")) {
        return "我已经为您调取了相关信息，让我来帮您处理：";
      }
      return "我是客服经理小服！我可以帮您处理售后问题、管理客户满意度。请告诉我您需要什么帮助？";
      
    case "finance":
      if (userMessage.includes("收入") || userMessage.includes("利润") || userMessage.includes("成本")) {
        return "我来为您分析财务数据：";
      }
      return "我是财务经理小财！我可以帮您分析收支情况、管理资金流转。请告诉我您需要什么帮助？";
      
    default:
      return "您好！请告诉我您需要什么帮助？";
  }
};
