import { decryptPassword } from '../../utils/password.js';

/**
 * Get JMAP configuration for user (legacy - for backward compatibility)
 */
export const getJmapConfig = (user) => {
  if (!user.email || !user.emailPassword || !user.jmapAccountId) {
    throw new Error('Používateľ nemá nastavený email účet');
  }

  // Decrypt email password for JMAP API
  const decryptedPassword = decryptPassword(user.emailPassword);

  return {
    server: process.env.JMAP_SERVER || 'https://mail.truemail.sk/jmap/',
    username: user.email,
    password: decryptedPassword,
    accountId: user.jmapAccountId,
  };
};

/**
 * Get JMAP configuration from email account object
 * NOTE: Expects emailPassword to be already decrypted (from getEmailAccountWithCredentials)
 */
export const getJmapConfigFromAccount = (emailAccount) => {
  if (!emailAccount.email || !emailAccount.emailPassword || !emailAccount.jmapAccountId) {
    throw new Error('Email účet je neúplný');
  }

  // Password is already decrypted by getEmailAccountWithCredentials
  return {
    server: process.env.JMAP_SERVER || 'https://mail.truemail.sk/jmap/',
    username: emailAccount.email,
    password: emailAccount.emailPassword,
    accountId: emailAccount.jmapAccountId,
  };
};
