import { AuthStorage } from "@/data/storage/auth/auth_storage";
import type { IAuthStorage } from "@/data/storage/auth";

export async function hasBusinessLoginToken(): Promise<boolean> {
  return hasCompleteBusinessAuthSession(AuthStorage.getInstance());
}

export async function hasCompleteBusinessAuthSession(
  storage: IAuthStorage,
): Promise<boolean> {
  const loginTokenResult = await storage.getLoginToken();
  if (!loginTokenResult.success || !loginTokenResult.data) return false;

  const refreshTokenResult = await storage.getRefreshToken();
  return refreshTokenResult.success && Boolean(refreshTokenResult.data);
}

/** 返回当前业务会话 token；真实用户优先，游客作为通话降级身份。 */
export async function getSessionAccessToken(
  storage: IAuthStorage = AuthStorage.getInstance(),
): Promise<string | null> {
  const loginTokenResult = await storage.getLoginToken();
  if (loginTokenResult.success && loginTokenResult.data) {
    return loginTokenResult.data;
  }
  const guestTokenResult = await storage.getGuestToken();
  return guestTokenResult.success ? guestTokenResult.data : null;
}
