package autoconfig

import "strings"

type providerPreset struct {
	id           string
	name         string
	domains      []string
	mxSubstrings []string
	imapHost     string
	imapPort     int
	imapTLS      bool
	smtpHost     string
	smtpPort     int
	smtpTLS      bool
	usernameHint string
	authMethods  []string
	notes        []string
}

// Top consumer mail providers (IMAP/SMTP). OAuth-capable providers still list password for a later wave.
var knownProviders = []providerPreset{
	{
		id: "gmail", name: "Gmail",
		domains: []string{"gmail.com", "googlemail.com"},
		mxSubstrings: []string{".google.com", "googlemail.com"},
		imapHost: "imap.gmail.com", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.gmail.com", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password", "oauth2"},
		notes:        []string{"Si la validation à deux facteurs est activée, utilisez un mot de passe d'application Google."},
	},
	{
		id: "google_workspace", name: "Google Workspace",
		mxSubstrings: []string{"aspmx.l.google.com", ".google.com"},
		imapHost: "imap.gmail.com", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.gmail.com", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password", "oauth2"},
		notes:        []string{"Domaine personnalisé hébergé chez Google. Mot de passe d'application si 2FA."},
	},
	{
		id: "outlook", name: "Outlook.com",
		domains: []string{
			"outlook.com", "outlook.fr", "hotmail.com", "hotmail.fr",
			"live.com", "live.fr", "msn.com",
		},
		mxSubstrings: []string{"outlook.com", "hotmail.com"},
		imapHost: "outlook.office365.com", imapPort: 993, imapTLS: true,
		smtpHost: "smtp-mail.outlook.com", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password", "oauth2"},
		notes:        []string{"Activez IMAP dans les paramètres du compte Microsoft si nécessaire."},
	},
	{
		id: "microsoft365", name: "Microsoft 365",
		domains:      []string{"onmicrosoft.com"},
		mxSubstrings: []string{"protection.outlook.com", "mail.protection.outlook.com", "outlook.com"},
		imapHost: "outlook.office365.com", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.office365.com", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password", "oauth2"},
		notes:        []string{"L'authentification moderne (OAuth) peut être requise par votre organisation."},
	},
	{
		id: "yahoo", name: "Yahoo Mail",
		domains: []string{
			"yahoo.com", "yahoo.fr", "yahoo.co.uk", "yahoo.de", "yahoo.es",
			"yahoo.it", "ymail.com", "rocketmail.com",
		},
		mxSubstrings: []string{"yahoodns.net", "yahoo.com"},
		imapHost: "imap.mail.yahoo.com", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.mail.yahoo.com", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
		notes:        []string{"Générez un mot de passe d'application Yahoo si la 2FA est activée."},
	},
	{
		id: "icloud", name: "iCloud Mail",
		domains: []string{"icloud.com", "me.com", "mac.com"},
		mxSubstrings: []string{"icloud.com", "apple.com", "mail.me.com"},
		imapHost: "imap.mail.me.com", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.mail.me.com", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
		notes:        []string{"Mot de passe spécifique aux applications requis (compte Apple)."},
	},
	{
		id: "aol", name: "AOL Mail",
		domains: []string{"aol.com", "aim.com"},
		mxSubstrings: []string{"aol.com", "yahoodns.net"},
		imapHost: "imap.aol.com", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.aol.com", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
	},
	{
		id: "zoho", name: "Zoho Mail",
		domains: []string{"zoho.com", "zohomail.com", "zoho.eu", "zoho.in"},
		mxSubstrings: []string{"zoho.com", "zohomail.com"},
		imapHost: "imap.zoho.com", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.zoho.com", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
	},
	{
		id: "fastmail", name: "Fastmail",
		domains: []string{"fastmail.com", "fastmail.fm"},
		mxSubstrings: []string{"messagingengine.com", "fastmail"},
		imapHost: "imap.fastmail.com", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.fastmail.com", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
	},
	{
		id: "gmx", name: "GMX",
		domains: []string{"gmx.com", "gmx.de", "gmx.net", "gmx.fr", "gmx.at", "gmx.ch"},
		mxSubstrings: []string{"gmx.net", "gmx.com"},
		imapHost: "imap.gmx.net", imapPort: 993, imapTLS: true,
		smtpHost: "mail.gmx.net", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
	},
	{
		id: "mailcom", name: "Mail.com",
		domains: []string{"mail.com", "email.com", "usa.com"},
		mxSubstrings: []string{"mail.com"},
		imapHost: "imap.mail.com", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.mail.com", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
	},
	{
		id: "yandex", name: "Yandex Mail",
		domains: []string{"yandex.com", "yandex.ru", "ya.ru"},
		mxSubstrings: []string{"yandex"},
		imapHost: "imap.yandex.com", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.yandex.com", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
		notes:        []string{"Activez l'accès IMAP dans les paramètres Yandex."},
	},
	{
		id: "mailru", name: "Mail.ru",
		domains: []string{"mail.ru", "inbox.ru", "bk.ru", "list.ru", "internet.ru"},
		mxSubstrings: []string{"mail.ru"},
		imapHost: "imap.mail.ru", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.mail.ru", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
	},
	{
		id: "proton", name: "Proton Mail",
		domains: []string{"proton.me", "protonmail.com", "pm.me"},
		mxSubstrings: []string{"protonmail.ch", "proton.me"},
		imapHost: "127.0.0.1", imapPort: 1143, imapTLS: true,
		smtpHost: "127.0.0.1", smtpPort: 1025, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
		notes: []string{
			"Proton Mail nécessite Proton Mail Bridge sur votre machine pour IMAP/SMTP.",
		},
	},
	{
		id: "orange", name: "Orange",
		domains: []string{"orange.fr", "wanadoo.fr"},
		mxSubstrings: []string{"orange.fr", "wanadoo.fr"},
		imapHost: "imap.orange.fr", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.orange.fr", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
	},
	{
		id: "free", name: "Free",
		domains: []string{"free.fr"},
		mxSubstrings: []string{"free.fr"},
		imapHost: "imap.free.fr", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.free.fr", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
	},
	{
		id: "ovh", name: "OVH Mail",
		mxSubstrings: []string{"ovh.net", "mail.ovh"},
		imapHost: "ssl0.ovh.net", imapPort: 993, imapTLS: true,
		smtpHost: "ssl0.ovh.net", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
	},
	{
		id: "ionos", name: "IONOS",
		domains: []string{"ionos.com", "ionos.fr", "1and1.com"},
		mxSubstrings: []string{"ionos", "1and1"},
		imapHost: "imap.ionos.com", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.ionos.com", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
	},
	{
		id: "comcast", name: "Xfinity / Comcast",
		domains: []string{"comcast.net", "xfinity.com"},
		mxSubstrings: []string{"comcast.net"},
		imapHost: "imap.comcast.net", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.comcast.net", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
	},
	{
		id: "seznam", name: "Seznam",
		domains: []string{"seznam.cz", "email.cz"},
		mxSubstrings: []string{"seznam.cz"},
		imapHost: "imap.seznam.cz", imapPort: 993, imapTLS: true,
		smtpHost: "smtp.seznam.cz", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"password"},
	},
	{
		id: "tutanota", name: "Tuta",
		domains: []string{"tuta.io", "tutanota.com", "tutanota.de"},
		mxSubstrings: []string{"tutanota", "tuta.io"},
		imapHost: "", imapPort: 993, imapTLS: true,
		smtpHost: "", smtpPort: 587, smtpTLS: true,
		usernameHint: "email",
		authMethods:  []string{"oauth2"},
		notes: []string{
			"Tuta ne propose pas IMAP/SMTP standard.",
			"Connexion OAuth : prochainement.",
		},
	},
}

func matchDomain(domain string) (*providerPreset, bool) {
	domain = strings.ToLower(strings.TrimSpace(domain))
	for i := range knownProviders {
		p := &knownProviders[i]
		for _, d := range p.domains {
			if domain == d || strings.HasSuffix(domain, "."+d) {
				return p, true
			}
		}
	}
	return nil, false
}

func matchMX(mxHost string) (*providerPreset, bool) {
	mxHost = strings.ToLower(mxHost)
	for i := range knownProviders {
		p := &knownProviders[i]
		for _, sub := range p.mxSubstrings {
			if sub != "" && strings.Contains(mxHost, sub) {
				return p, true
			}
		}
	}
	return nil, false
}

func (p *providerPreset) toResult(email, domain, source, confidence string) Result {
	auth := p.authMethods
	if len(auth) == 0 {
		auth = []string{"password"}
	}
	return Result{
		Email:        email,
		Domain:       domain,
		ProviderID:   p.id,
		ProviderName: p.name,
		Source:       source,
		Confidence:   confidence,
		IMAPHost:     p.imapHost,
		IMAPPort:     p.imapPort,
		IMAPTLS:      p.imapTLS,
		SMTPHost:     p.smtpHost,
		SMTPPort:     p.smtpPort,
		SMTPTLS:      p.smtpTLS,
		UsernameHint: p.usernameHint,
		AuthMethods:  auth,
		Notes:        append([]string(nil), p.notes...),
	}
}
