import { getProfile } from "../../db/profile";

/** GET /api/admin/lyra-profile?author=lyra|rebreak — gibt Nickname und Avatar des Bot-Accounts zurück */
export default defineEventHandler(async (event) => {
  const config = useRuntimeConfig();
  const adminSecret = getHeader(event, "x-admin-secret");

  if (!config.adminSecret || adminSecret !== config.adminSecret) {
    throw createError({ statusCode: 401, message: "Unauthorized" });
  }

  const query = getQuery(event);
  const author = query.author === "rebreak" ? "rebreak" : "lyra";

  const userId =
    author === "rebreak" ? config.rebreakBotUserId : config.lyraBotUserId;

  if (!userId) {
    return {
      nickname: author === "rebreak" ? "ReBreak" : "Lyra",
      avatar: null,
    };
  }

  const profile = await getProfile(userId);
  return {
    nickname: profile?.nickname ?? (author === "rebreak" ? "ReBreak" : "Lyra"),
    avatar: profile?.avatar ?? null,
  };
});
