import { clsx, type ClassValue } from 'clsx'
import { twMerge } from 'tailwind-merge'

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs))
}

export function formatNumber(n: number | string | null | undefined): string {
  if (n === null || n === undefined || n === '') return '-'
  const num = typeof n === 'string' ? parseFloat(n) : n
  if (isNaN(num)) return '-'
  return num.toLocaleString('zh-CN', { maximumFractionDigits: 2 })
}

export function formatPercent(n: number | string | null | undefined): string {
  if (n === null || n === undefined || n === '') return '-'
  const num = typeof n === 'string' ? parseFloat(n) : n
  if (isNaN(num)) return '-'
  return `${num.toFixed(2)}%`
}

export function formatCurrency(n: number | string | null | undefined): string {
  if (n === null || n === undefined || n === '') return '-'
  const num = typeof n === 'string' ? parseFloat(n) : n
  if (isNaN(num)) return '-'
  return `¥${num.toLocaleString('zh-CN', { maximumFractionDigits: 2 })}`
}

export function priorityColor(priority: string): string {
  if (priority?.startsWith('P0')) return 'bg-red-100 text-red-700 border-red-300'
  if (priority?.startsWith('P1')) return 'bg-orange-100 text-orange-700 border-orange-300'
  if (priority?.startsWith('P2')) return 'bg-yellow-100 text-yellow-700 border-yellow-300'
  if (priority?.includes('标杆')) return 'bg-green-100 text-green-700 border-green-300'
  return 'bg-gray-100 text-gray-700 border-gray-300'
}

export function riskColor(level: string): string {
  if (level === '红色') return 'bg-red-500 text-white'
  if (level === '黄色') return 'bg-yellow-500 text-white'
  return 'bg-green-500 text-white'
}

export function statusColor(status: string): string {
  if (status === '待启动') return 'bg-gray-100 text-gray-600'
  if (status === '进行中') return 'bg-blue-100 text-blue-700'
  if (status === '已验收') return 'bg-green-100 text-green-700'
  if (status === '已回滚') return 'bg-red-100 text-red-700'
  return 'bg-gray-100 text-gray-600'
}

export function reviewResultColor(result: string): string {
  if (result === '达标') return 'bg-green-100 text-green-700 border border-green-300'
  if (result === '改善中') return 'bg-yellow-100 text-yellow-700 border border-yellow-300'
  if (result === '未改善') return 'bg-red-100 text-red-700 border border-red-300'
  return 'bg-gray-100 text-gray-600 border border-gray-300'
}
