export const MAIL_SETTINGS_KEY = "mail_settings";

export type MailSettings = {
  smtp: {
    host: string;
    port: number;
    secure: boolean;
    username: string;
    password: string;
  };
  sender: {
    email: string;
    name: string;
  };
  recipients: {
    contact: string;
    test: string;
  };
};

export type MailSettingsFormValues = {
  smtp: {
    host: string;
    port: number;
    secure: boolean;
    username: string;
    password: string;
    hasPassword: boolean;
  };
  sender: {
    email: string;
    name: string;
  };
  recipients: {
    contact: string;
    test: string;
  };
};

export function buildDefaultMailSettings(): MailSettings {
  return {
    smtp: {
      host: "",
      port: 587,
      secure: false,
      username: "",
      password: "",
    },
    sender: {
      email: "",
      name: "",
    },
    recipients: {
      contact: "",
      test: "",
    },
  };
}

function normalizeMailSettings(input: unknown): MailSettings {
  const value = input && typeof input === "object" ? (input as Record<string, unknown>) : {};
  const smtp = value.smtp && typeof value.smtp === "object"
    ? (value.smtp as Record<string, unknown>)
    : {};
  const sender = value.sender && typeof value.sender === "object"
    ? (value.sender as Record<string, unknown>)
    : {};
  const recipients = value.recipients && typeof value.recipients === "object"
    ? (value.recipients as Record<string, unknown>)
    : {};
  const defaults = buildDefaultMailSettings();
  const parsedPort =
    typeof smtp.port === "number"
      ? smtp.port
      : typeof smtp.port === "string"
        ? Number.parseInt(smtp.port, 10)
        : defaults.smtp.port;

  return {
    smtp: {
      host: typeof smtp.host === "string" ? smtp.host.trim() : defaults.smtp.host,
      port: Number.isFinite(parsedPort) && parsedPort > 0 ? parsedPort : defaults.smtp.port,
      secure: Boolean(smtp.secure),
      username: typeof smtp.username === "string" ? smtp.username.trim() : defaults.smtp.username,
      password: typeof smtp.password === "string" ? smtp.password : defaults.smtp.password,
    },
    sender: {
      email: typeof sender.email === "string" ? sender.email.trim() : defaults.sender.email,
      name: typeof sender.name === "string" ? sender.name.trim() : defaults.sender.name,
    },
    recipients: {
      contact:
        typeof recipients.contact === "string"
          ? recipients.contact.trim()
          : defaults.recipients.contact,
      test:
        typeof recipients.test === "string" ? recipients.test.trim() : defaults.recipients.test,
    },
  };
}

export function parseMailSettingsValue(rawValue: string | null | undefined): MailSettings {
  if (!rawValue) {
    return buildDefaultMailSettings();
  }

  try {
    return normalizeMailSettings(JSON.parse(rawValue));
  } catch {
    return buildDefaultMailSettings();
  }
}

export function toMailSettingsFormValues(settings: MailSettings): MailSettingsFormValues {
  return {
    smtp: {
      host: settings.smtp.host,
      port: settings.smtp.port,
      secure: settings.smtp.secure,
      username: settings.smtp.username,
      password: "",
      hasPassword: Boolean(settings.smtp.password),
    },
    sender: {
      email: settings.sender.email,
      name: settings.sender.name,
    },
    recipients: {
      contact: settings.recipients.contact,
      test: settings.recipients.test,
    },
  };
}
