/**
 * IMAP-Provider Konfigurationen
 * Automatisch erkennen anhand der Email-Domain
 */
export interface ImapConfig {
  host: string;
  port: number;
  name: string;
}

const PROVIDER_MAP: Record<string, ImapConfig> = {
  "gmail.com": { host: "imap.gmail.com", port: 993, name: "Gmail" },
  "googlemail.com": { host: "imap.gmail.com", port: 993, name: "Gmail" },
  "icloud.com": { host: "imap.mail.me.com", port: 993, name: "iCloud" },
  "me.com": { host: "imap.mail.me.com", port: 993, name: "iCloud" },
  "mac.com": { host: "imap.mail.me.com", port: 993, name: "iCloud" },
  "outlook.com": { host: "outlook.office365.com", port: 993, name: "Outlook" },
  "hotmail.com": { host: "outlook.office365.com", port: 993, name: "Hotmail" },
  "hotmail.de": { host: "outlook.office365.com", port: 993, name: "Hotmail" },
  "live.com": { host: "outlook.office365.com", port: 993, name: "Live" },
  "live.de": { host: "outlook.office365.com", port: 993, name: "Live" },
  "yahoo.com": { host: "imap.mail.yahoo.com", port: 993, name: "Yahoo" },
  "yahoo.de": { host: "imap.mail.yahoo.com", port: 993, name: "Yahoo" },
  "gmx.de": { host: "imap.gmx.net", port: 993, name: "GMX" },
  "gmx.net": { host: "imap.gmx.net", port: 993, name: "GMX" },
  "gmx.at": { host: "imap.gmx.net", port: 993, name: "GMX" },
  "gmx.ch": { host: "imap.gmx.net", port: 993, name: "GMX" },
  "web.de": { host: "imap.web.de", port: 993, name: "Web.de" },
  "t-online.de": {
    host: "secureimap.t-online.de",
    port: 993,
    name: "T-Online",
  },
  "freenet.de": { host: "mx.freenet.de", port: 993, name: "Freenet" },
  "posteo.de": { host: "posteo.de", port: 993, name: "Posteo" },
};

export function detectImapProvider(email: string): ImapConfig {
  const domain = email.split("@")[1]?.toLowerCase() ?? "";
  return (
    PROVIDER_MAP[domain] ?? {
      host: `imap.${domain}`,
      port: 993,
      name: domain,
    }
  );
}

const SMTP_MAP: Record<string, { host: string; port: number }> = {
  "imap.gmail.com": { host: "smtp.gmail.com", port: 587 },
  "imap.mail.me.com": { host: "smtp.mail.me.com", port: 587 },
  "imap.gmx.net": { host: "mail.gmx.net", port: 587 },
  "imap.web.de": { host: "smtp.web.de", port: 587 },
  "outlook.office365.com": { host: "smtp-mail.outlook.com", port: 587 },
  "imap.mail.yahoo.com": { host: "smtp.mail.yahoo.com", port: 587 },
  "secureimap.t-online.de": { host: "securesmtp.t-online.de", port: 587 },
  "mx.freenet.de": { host: "mx.freenet.de", port: 587 },
  "posteo.de": { host: "posteo.de", port: 587 },
};

export function detectSmtpProvider(imapHost: string): { host: string; port: number } {
  return SMTP_MAP[imapHost] ?? { host: imapHost.replace("imap.", "smtp."), port: 587 };
}
