import { fetchAdminMeDeduped } from "@/lib/admin-fetch-me";
import { isAdminAuthRejected } from "@/lib/admin-auth-reject";
import { getAdminSessionState } from "@/stores/admin-session";

/**
 * 用 `/auth/me` 校验 HttpOnly Cookie 会话是否仍有效；失败时清会话（不跳转，由调用方决定）。
 */
export async function verifyStoredAdminSession(): Promise<boolean> {
  const session = getAdminSessionState();
  session.setBearerToken("__cookie__");

  try {
    const result = await fetchAdminMeDeduped();
    session.setAdminProfile(result.admin);
    return true;
  } catch (err) {
    if (isAdminAuthRejected(err)) {
      session.clearSession();
    }
    return false;
  }
}
