import {format, isBefore, isSameDay} from 'date-fns'
import {Badge} from '@/components/ui/badge'
import {Box, Timer} from 'lucide-react'

// 类型守卫函数
export function isValidResourceType(type: string | null): type is 'expire' | 'quota' | 'all' {
  return type === 'expire' || type === 'quota' || type === 'all'
}

// 资源类型徽章
export function ResourceTypeBadge({type}: {type: number}) {
  if (type === 1) {
    return (
      <div className="flex gap-2 items-center bg-green-50 w-fit px-2 py-1 rounded-md">
        <Timer size={20}/>
        <span>包时</span>
      </div>
    )
  }
  if (type === 2) {
    return (
      <div className="flex gap-2 items-center bg-blue-50 w-fit px-2 py-1 rounded-md">
        <Box size={20}/>
        <span>包量</span>
      </div>
    )
  }
  return null
}

// 过期徽章
export function ExpireBadge({expireAt}: {expireAt: Date}) {
  if (isBefore(expireAt, new Date())) {
    return <Badge variant="destructive">过期</Badge>
  }
  return null
}

// 格式化日期
export function formatDateTime(date: Date | null | undefined) {
  if (!date) return '-'
  return format(date, 'yyyy-MM-dd HH:mm')
}

// 计算今日使用量
export function getTodayUsage(lastAt: Date | null | undefined, daily: number) {
  if (lastAt && isSameDay(lastAt, new Date())) {
    return daily
  }
  return 0
}
