import 'dotenv/config';
import {
    getSmtpConfig,
    isSmtpEnabled,
    type SmtpConfig,
} from '../../services/smtpConfig.service.js';

export type EmailConfig = Pick<SmtpConfig, 'host' | 'port' | 'secure' | 'auth' | 'from'> & {
    enabled: boolean;
};

/** Resolved after `initializeSmtpConfig()` in server startup */
export function getEmailConfig(): EmailConfig {
    const smtp = getSmtpConfig();
    return {
        enabled: smtp.enabled,
        host: smtp.host,
        port: smtp.port,
        secure: smtp.secure,
        auth: smtp.auth,
        from: smtp.from,
    };
}

export { isSmtpEnabled };

/** @deprecated Prefer getEmailConfig() after startup initialization */
const legacyConfig: EmailConfig = {
    get enabled() {
        return isSmtpEnabled();
    },
    get host() {
        return getEmailConfig().host;
    },
    get port() {
        return getEmailConfig().port;
    },
    get secure() {
        return getEmailConfig().secure;
    },
    get auth() {
        return getEmailConfig().auth;
    },
    get from() {
        return getEmailConfig().from;
    },
};

export default legacyConfig;
