import { request } from '../utils/request'

// 物流信息接口
export interface LogisticsInfo {
  expressCompany: string // 快递公司代码
  expressCompanyName: string // 快递公司名称
  expressNo: string // 快递单号
  status: string // 物流状态
  updateTime: string // 更新时间
  estimatedTime?: string // 预计送达时间
  currentLocation?: string // 当前位置
  senderInfo?: {
    name: string
    phone: string
    address: string
  }
  receiverInfo?: {
    name: string
    phone: string
    address: string
  }
}

// 物流跟踪记录
export interface LogisticsTrack {
  time: string
  location: string
  status: string
  description: string
  isCompleted: boolean
}

// 物流查询响应
export interface LogisticsResponse {
  success: boolean
  data: {
    logisticsInfo: LogisticsInfo
    trackList: LogisticsTrack[]
  }
  message?: string
}

// 支持的快递公司
export const EXPRESS_COMPANIES = {
  'SF': '顺丰速运',
  'YTO': '圆通速递',
  'ZTO': '中通快递',
  'STO': '申通快递',
  'YD': '韵达速递',
  'HTKY': '百世快递',
  'JD': '京东物流',
  'EMS': '中国邮政',
  'YUNDA': '韵达快递',
  'JTSD': '极兔速递',
  'DBKD': '德邦快递',
  'UC': '优速快递'
}

// 查询物流信息
export const queryLogistics = async (params: {
  orderId?: string
  expressNo: string
  expressCompany: string
}): Promise<LogisticsResponse> => {
  try {
    // 实际项目中这里应该调用真实的物流查询API
    // 例如：快递100、快递鸟、菜鸟裹裹等第三方物流查询服务
    
    // 模拟API调用
    const response = await request({
      url: '/api/logistics/query',
      method: 'POST',
      data: params
    })

    return response
  } catch (error) {
    console.error('查询物流信息失败:', error)
    
    // 返回模拟数据作为降级方案
    return getMockLogisticsData(params)
  }
}

// 获取模拟物流数据
const getMockLogisticsData = (params: {
  orderId?: string
  expressNo: string
  expressCompany: string
}): LogisticsResponse => {
  const now = new Date()
  const yesterday = new Date(now.getTime() - 24 * 60 * 60 * 1000)
  const twoDaysAgo = new Date(now.getTime() - 2 * 24 * 60 * 60 * 1000)
  const threeDaysAgo = new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000)

  const mockData: LogisticsResponse = {
    success: true,
    data: {
      logisticsInfo: {
        expressCompany: params.expressCompany,
        expressCompanyName: EXPRESS_COMPANIES[params.expressCompany] || params.expressCompany,
        expressNo: params.expressNo,
        status: '运输中',
        updateTime: now.toISOString(),
        estimatedTime: new Date(now.getTime() + 24 * 60 * 60 * 1000).toISOString(),
        currentLocation: '北京市朝阳区',
        senderInfo: {
          name: '商家仓库',
          phone: '400-123-4567',
          address: '上海市浦东新区张江高科技园区'
        },
        receiverInfo: {
          name: '张三',
          phone: '138****5678',
          address: '北京市朝阳区三里屯街道'
        }
      },
      trackList: [
        {
          time: now.toISOString(),
          location: '北京市朝阳区',
          status: '运输中',
          description: '快件正在运输途中，预计今日送达，请保持手机畅通',
          isCompleted: false
        },
        {
          time: new Date(now.getTime() - 2 * 60 * 60 * 1000).toISOString(),
          location: '北京转运中心',
          status: '已发出',
          description: '快件已从北京转运中心发出，正在派送途中',
          isCompleted: true
        },
        {
          time: new Date(now.getTime() - 6 * 60 * 60 * 1000).toISOString(),
          location: '北京转运中心',
          status: '已到达',
          description: '快件已到达北京转运中心，正在进行分拣',
          isCompleted: true
        },
        {
          time: yesterday.toISOString(),
          location: '天津转运中心',
          status: '已发出',
          description: '快件已从天津转运中心发出',
          isCompleted: true
        },
        {
          time: new Date(yesterday.getTime() - 4 * 60 * 60 * 1000).toISOString(),
          location: '天津转运中心',
          status: '已到达',
          description: '快件已到达天津转运中心',
          isCompleted: true
        },
        {
          time: twoDaysAgo.toISOString(),
          location: '上海转运中心',
          status: '已发出',
          description: '快件已从上海转运中心发出',
          isCompleted: true
        },
        {
          time: new Date(twoDaysAgo.getTime() - 2 * 60 * 60 * 1000).toISOString(),
          location: '上海转运中心',
          status: '已到达',
          description: '快件已到达上海转运中心，正在进行分拣',
          isCompleted: true
        },
        {
          time: threeDaysAgo.toISOString(),
          location: '上海市浦东新区',
          status: '已发货',
          description: '商家已发货，快件已交给快递公司',
          isCompleted: true
        }
      ]
    }
  }

  return mockData
}

// 获取快递公司列表
export const getExpressCompanies = () => {
  return Object.entries(EXPRESS_COMPANIES).map(([code, name]) => ({
    code,
    name
  }))
}

// 根据快递单号自动识别快递公司
export const detectExpressCompany = (expressNo: string): string => {
  // 这里可以根据快递单号的规则来自动识别快递公司
  // 实际项目中可以使用第三方服务的自动识别API
  
  if (expressNo.startsWith('SF')) return 'SF'
  if (expressNo.startsWith('YT')) return 'YTO'
  if (expressNo.startsWith('ZT')) return 'ZTO'
  if (expressNo.startsWith('ST')) return 'STO'
  if (expressNo.startsWith('YD')) return 'YD'
  if (expressNo.startsWith('JD')) return 'JD'
  if (expressNo.startsWith('EMS')) return 'EMS'
  
  // 默认返回顺丰
  return 'SF'
}

// 格式化物流状态
export const formatLogisticsStatus = (status: string): {
  text: string
  color: string
  icon: string
} => {
  const statusMap = {
    '已发货': { text: '已发货', color: '#1890ff', icon: '📦' },
    '运输中': { text: '运输中', color: '#52c41a', icon: '🚚' },
    '派送中': { text: '派送中', color: '#faad14', icon: '🏃' },
    '已签收': { text: '已签收', color: '#52c41a', icon: '✅' },
    '异常': { text: '异常', color: '#ff4d4f', icon: '⚠️' },
    '退回': { text: '退回', color: '#ff4d4f', icon: '↩️' }
  }

  return statusMap[status] || { text: status, color: '#666', icon: '📋' }
}

// 计算预计送达时间
export const calculateEstimatedTime = (
  sendTime: string,
  expressCompany: string,
  distance?: number
): string => {
  const sendDate = new Date(sendTime)
  let estimatedDays = 3 // 默认3天

  // 根据快递公司调整预计时间
  switch (expressCompany) {
    case 'SF':
      estimatedDays = 1 // 顺丰次日达
      break
    case 'JD':
      estimatedDays = 1 // 京东次日达
      break
    case 'YTO':
    case 'ZTO':
    case 'STO':
      estimatedDays = 2 // 三通一达2天
      break
    default:
      estimatedDays = 3
  }

  // 根据距离调整（如果有距离信息）
  if (distance) {
    if (distance > 2000) estimatedDays += 1 // 超过2000公里加1天
    if (distance > 3000) estimatedDays += 1 // 超过3000公里再加1天
  }

  const estimatedDate = new Date(sendDate.getTime() + estimatedDays * 24 * 60 * 60 * 1000)
  return estimatedDate.toISOString()
}
