import type { StakingConfig } from '@/api/staking'

/** 锁定月数（按 30 天/月向上取整，至少 1） */
export function lockMonthsFromConfig(config: StakingConfig): number {
  const days = config.lockDays ?? 0
  return Math.max(1, Math.ceil(days / 30))
}

/** 分批释放总月数；一次性释放返回 0 */
export function releaseMonthsFromConfig(config: StakingConfig): number {
  if (config.releaseType !== 1) {
    return 0
  }
  const rate = Number(config.releaseRate)
  const periodDays = config.releasePeriod ?? 1
  if (!rate || rate <= 0) {
    return Math.max(1, Math.ceil(periodDays / 30))
  }
  const batches = Math.ceil(1 / rate)
  const totalDays = batches * periodDays
  return Math.max(1, Math.ceil(totalDays / 30))
}

/** 预计开始释放时间 = 当前时间 + lockDays */
export function estimatedUnlockDate(config: StakingConfig): Date {
  const d = new Date()
  d.setDate(d.getDate() + (config.lockDays ?? 0))
  return d
}

export function formatUnlockDate(d: Date): string {
  const pad = (n: number) => String(n).padStart(2, '0')
  return `${d.getFullYear()}/${pad(d.getMonth() + 1)}/${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
}
