import { MAX_PORT, MIN_PORT } from "../constants/ports";
import { SCHEMA_MAX_AUTOMATION_TIMEOUT_SECONDS } from "../constants/schema-limits";
import type { EmailTransportSettings } from "../types/settings";
import { isValidEmail } from "./validation";

/**
 * Returns whether a persisted outbound email transport configuration is complete enough to send mail.
 *
 * @param settings - Persisted transport settings.
 * @param hasPassword - Whether a secret password is stored alongside the public settings.
 * @returns `true` when the transport can be used for delivery.
 */
export function isEmailTransportConfigured(
  settings: EmailTransportSettings | null | undefined,
  hasPassword: boolean,
): boolean {
  if (!settings) {
    return false;
  }

  const host = settings.host.trim();
  const fromEmail = settings.fromEmail.trim();
  const username = settings.username.trim();
  const port = Math.trunc(settings.port);
  const timeout = Math.trunc(settings.connectionTimeoutSeconds);

  if (host.length === 0 || !isValidEmail(fromEmail)) {
    return false;
  }

  if (!Number.isFinite(port) || port < MIN_PORT || port > MAX_PORT) {
    return false;
  }

  if (!Number.isFinite(timeout) || timeout < 1 || timeout > SCHEMA_MAX_AUTOMATION_TIMEOUT_SECONDS) {
    return false;
  }

  if (username.length === 0) {
    return !hasPassword;
  }

  return hasPassword;
}
