import Taro from "@tarojs/taro";

const ADMIN_TOKEN_KEY = "xlh_admin_token";
const ADMIN_REFRESH_KEY = "xlh_admin_refresh";
const ADMIN_USER_KEY = "xlh_admin_user";

export function getAdminToken() {
  return Taro.getStorageSync(ADMIN_TOKEN_KEY) || "";
}

export function getAdminRefreshToken() {
  return Taro.getStorageSync(ADMIN_REFRESH_KEY) || "";
}

export function setAdminTokens(token: string, refreshToken: string) {
  Taro.setStorageSync(ADMIN_TOKEN_KEY, token);
  Taro.setStorageSync(ADMIN_REFRESH_KEY, refreshToken);
}

export function clearAdminTokens() {
  Taro.removeStorageSync(ADMIN_TOKEN_KEY);
  Taro.removeStorageSync(ADMIN_REFRESH_KEY);
  Taro.removeStorageSync(ADMIN_USER_KEY);
}

export function isLoggedInAsAdmin() {
  return !!Taro.getStorageSync(ADMIN_TOKEN_KEY);
}

export function setAdminUser(user: any) {
  Taro.setStorageSync(ADMIN_USER_KEY, JSON.stringify(user));
}

export function getAdminUser() {
  try {
    const raw = Taro.getStorageSync(ADMIN_USER_KEY);
    return raw ? JSON.parse(raw) : null;
  } catch {
    return null;
  }
}
