import { redis, REDIS_CACHE_HOUR, xcxApi } from '../utils/server';

const getKey = (uid: string) => `my-kaiqiuwang:uid-score:${uid}`;

const TIMEOUT = 60 * 60 * REDIS_CACHE_HOUR; // 24h;

export const getUidScore = async (uids: string[]): Promise<Record<string, string>> => {
  const jobs = uids.map(async uid => {
    const key = getKey(uid);
    const value = await redis.get(key);
    if (value) {
      return [uid, value];
    }
    const profile = await xcxApi.getAdvProfile(uid);
    await redis.setex(key, TIMEOUT, profile?.score ?? '');
    return [uid, profile?.score ?? ''];
  });
  return Object.fromEntries(await Promise.all(jobs));
}