function positiveDays(value, fallback) {
  const days = Number(value);

  if (!Number.isInteger(days) || days <= 0) {
    return fallback;
  }

  return days;
}

function retentionPolicy(env = process.env) {
  return {
    raw_audio_days: positiveDays(env.COACHING_AUDIO_RETENTION_DAYS, 30),
    transcript_days: positiveDays(env.COACHING_TRANSCRIPT_RETENTION_DAYS, 365),
    session_memory_days: positiveDays(env.COACHING_MEMORY_RETENTION_DAYS, 730),
    backup_expiry_days: positiveDays(env.COACHING_BACKUP_EXPIRY_DAYS, 30),
  };
}

function cutoffDate(days, now = new Date()) {
  return new Date(now.getTime() - days * 24 * 60 * 60 * 1000);
}

module.exports = { retentionPolicy, cutoffDate };
