export function formatSubscriptionDuration(plan, t) {
  const unit = plan?.duration_unit || 'month';
  const value = plan?.duration_value || 1;
  const unitLabels = {
    year: t('年'),
    month: t('个月'),
    day: t('天'),
    hour: t('小时'),
    custom: t('自定义'),
  };
  if (unit === 'custom') {
    const seconds = Number(plan?.custom_seconds || 0);
    if (seconds >= 86400) return `${Math.floor(seconds / 86400)} ${t('天')}`;
    if (seconds >= 3600) return `${Math.floor(seconds / 3600)} ${t('小时')}`;
    if (seconds >= 60) return `${Math.floor(seconds / 60)} ${t('分钟')}`;
    return `${seconds} ${t('秒')}`;
  }
  return `${value} ${unitLabels[unit] || unit}`;
}

export function formatSubscriptionResetPeriod(plan, t) {
  const period = plan?.quota_reset_period || 'never';
  if (period === 'never') return t('不重置');
  if (period === 'daily') return t('每天');
  if (period === 'weekly') return t('每周');
  if (period === 'monthly') return t('每月');
  if (period === 'custom') {
    const seconds = Number(plan?.quota_reset_custom_seconds || 0);
    if (seconds >= 86400) return `${Math.floor(seconds / 86400)} ${t('天')}`;
    if (seconds >= 3600) return `${Math.floor(seconds / 3600)} ${t('小时')}`;
    if (seconds >= 60) return `${Math.floor(seconds / 60)} ${t('分钟')}`;
    return `${seconds} ${t('秒')}`;
  }
  return t('不重置');
}

export function getSubscriptionEndDate(startDate, plan) {
  if (!startDate || !plan) return null;
  const start = new Date(startDate);
  if (Number.isNaN(start.getTime())) return null;
  const unit = plan?.duration_unit || 'month';
  const value = Number(plan?.duration_value || 1);
  const customSeconds = Number(plan?.custom_seconds || 0);
  const end = new Date(start.getTime());
  switch (unit) {
    case 'year':
      end.setFullYear(end.getFullYear() + value);
      break;
    case 'month':
      end.setMonth(end.getMonth() + value);
      break;
    case 'day':
      end.setDate(end.getDate() + value);
      break;
    case 'hour':
      end.setHours(end.getHours() + value);
      break;
    case 'custom':
      end.setSeconds(end.getSeconds() + customSeconds);
      break;
    default:
      return null;
  }
  return end;
}

export function getResetCycleCount(plan, startDate, endDate) {
  if (!plan || !startDate || !endDate) return 1;
  const period = plan?.quota_reset_period || 'never';
  if (period === 'never') return 1;

  const start = new Date(startDate);
  const end = new Date(endDate);
  if (Number.isNaN(start.getTime()) || Number.isNaN(end.getTime())) return 1;
  if (end <= start) return 1;

  const nextReset = (current) => {
    const next = new Date(current.getTime());
    switch (period) {
      case 'daily':
        next.setDate(next.getDate() + 1);
        return next;
      case 'weekly':
        next.setDate(next.getDate() + 7);
        return next;
      case 'monthly':
        next.setMonth(next.getMonth() + 1);
        return next;
      case 'custom': {
        const seconds = Number(plan?.quota_reset_custom_seconds || 0);
        if (seconds <= 0) return null;
        next.setSeconds(next.getSeconds() + seconds);
        return next;
      }
      default:
        return null;
    }
  };

  let cycles = 1;
  let current = start;
  for (let i = 0; i < 10000; i += 1) {
    const next = nextReset(current);
    if (!next || next >= end) break;
    cycles += 1;
    current = next;
  }
  return cycles;
}
