export const formatTime = (date: Date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return (
    [year, month, day].map(formatNumber).join('/') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  )
}

const formatNumber = (n: number) => {
  const s = n.toString()
  return s[1] ? s : '0' + s
}

// 格式化日期
export const formatDate = (dateStr: string) => {
  if (!dateStr) return '-'
  
  // 修复iOS兼容性：将 "2023-12-05 11:00:00" 格式转换为 "2023-12-05T11:00:00"
  const iosCompatibleDateStr = dateStr.replace(' ', 'T')
  
  const date = new Date(iosCompatibleDateStr)
  const now = new Date()
  const diff = now.getTime() - date.getTime()
  const days = Math.floor(diff / (1000 * 60 * 60 * 24))
  
  if (days === 0) {
    const hours = Math.floor(diff / (1000 * 60 * 60))
    if (hours === 0) {
      const minutes = Math.floor(diff / (1000 * 60))
      return minutes <= 0 ? '刚刚' : `${minutes}分钟前`
    }
    return `${hours}小时前`
  } else if (days === 1) {
    return '昨天'
  } else if (days < 7) {
    return `${days}天前`
  } else {
    return dateStr.slice(5, 10) // MM-DD
  }
}

// 获取状态信息
export const getStatusInfo = (status: string) => {
  const statusMap: Record<string, { text: string; class: string }> = {
    'topic': { text: '话题', class: 'status-topic' },
    'cover_image': { text: '封面图', class: 'status-cover_image' },
    'generate': { text: '生成中', class: 'status-generate' },
    'generate_failed': { text: '生成失败', class: 'status-generate_failed' },
    'draft': { text: '草稿', class: 'status-draft' },
    'pending_review': { text: '待审核', class: 'status-pending_review' },
    'approved': { text: '已通过', class: 'status-approved' },
    'rejected': { text: '已驳回', class: 'status-rejected' },
    'published_review': { text: '发布审核中', class: 'status-published_review' },
    'published': { text: '已发布', class: 'status-published' },
    'failed': { text: '失败', class: 'status-failed' }
  }
  return statusMap[status] || { text: status, class: 'status-draft' }
}

// 获取渠道信息
export const getChannelInfo = (channel: number) => {
  const channelMap: Record<number, { text: string; class: string }> = {
    1: { text: '百度', class: 'channel-baidu' },
    2: { text: '头条', class: 'channel-toutiao' },
    3: { text: '微信', class: 'channel-weixin' }
  }
  return channelMap[channel] || { text: '未知', class: '' }
}

// 获取封面颜色
export const getCoverColor = (topicTypeId: number) => {
  const colors = [
    '#FFB6C1', '#FFD700', '#98FB98', '#DDA0DD',
    '#87CEEB', '#F0E68C', '#FFA07A', '#B0C4DE',
    '#8B4513', '#32CD32', '#FF69B4', '#4169E1'
  ]
  return colors[topicTypeId % colors.length]
}

// 获取封面图标
export const getCoverIcon = (topicTypeId: number) => {
  const icons = [
    '☀️', '🍽️', '🏠', '👔',
    '⛺', '💪', '💆', '📚',
    '☕', '💰', '❤️', '⭐'
  ]
  return icons[topicTypeId % icons.length]
}
