import { getImapProxyAccounts, getMailConnections } from "../../db/mail";

export default defineEventHandler(async (event) => {
  const user = await requireUser(event);

  const [accounts, connections] = await Promise.all([
    getImapProxyAccounts(user.id),
    getMailConnections(user.id),
  ]);

  if (accounts.length === 0) {
    return { configured: false, accounts: [] };
  }

  // Enrich with real email address from connection
  const connMap = new Map(connections.map((c) => [c.id, c.email]));

  return {
    configured: true,
    host: "imap.rebreak.org",
    port: 993,
    accounts: accounts.map((a) => ({
      username: a.proxyUsername,
      realEmail: connMap.get(a.connectionId) ?? null,
    })),
  };
});
