package connector

import (
	"fmt"

	"go.mau.fi/util/configupgrade"
)

const (
	ProviderGmail      = "gmail"
	ProviderICloud     = "icloud"
	ProviderCustomIMAP = "custom"
)

type IMAPAccount struct {
	Email        string `yaml:"email"`
	DisplayName  string `yaml:"display_name,omitempty"`
	Provider     string `yaml:"provider"`
	IMAPServer   string `yaml:"imap_server,omitempty"`
	IMAPPort     int    `yaml:"imap_port,omitempty"`
	IMAPTLS      *bool  `yaml:"imap_tls,omitempty"`
	SMTPServer   string `yaml:"smtp_server,omitempty"`
	SMTPPort     int    `yaml:"smtp_port,omitempty"`
	SMTPTLS      *bool  `yaml:"smtp_tls,omitempty"`
	Username     string `yaml:"username,omitempty"`
	Password     string `yaml:"password"`
	Folder       string `yaml:"folder,omitempty"`
	PollInterval int    `yaml:"poll_interval_seconds,omitempty"`
}

type NetworkConfig struct {
	Accounts []IMAPAccount `yaml:"accounts"`

	// AvatarBrandAliases maps a mail domain to the domain that actually hosts
	// the brand assets, for providers whose mail domain is a separate DNS
	// registration from the company. Example:
	//     avatar_brand_aliases:
	//         luma-mail.com: luma.com
	// It is only consulted when nothing was found for the mail domain itself,
	// its registrable domain, Google s2 or DuckDuckGo. Entries are never guessed
	// automatically, because loading a random other company's logo would be worse
	// than showing a generated placeholder.
	AvatarBrandAliases map[string]string `yaml:"avatar_brand_aliases,omitempty"`
}

func (c *NetworkConfig) GetIMAPAddr(acct IMAPAccount) string {
	addr := acct.IMAPServer
	port := acct.IMAPPort
	tls := true
	if acct.IMAPTLS != nil {
		tls = *acct.IMAPTLS
	}

	switch acct.Provider {
	case ProviderGmail:
		if addr == "" {
			addr = "imap.gmail.com"
		}
		if port == 0 {
			port = 993
		}
	case ProviderICloud:
		if addr == "" {
			addr = "imap.mail.me.com"
		}
		if port == 0 {
			port = 993
		}
	default:
		if port == 0 {
			if tls {
				port = 993
			} else {
				port = 143
			}
		}
	}

	_ = tls
	return fmt.Sprintf("%s:%d", addr, port)
}

func (c *NetworkConfig) GetSMTPAddr(acct IMAPAccount) string {
	addr := acct.SMTPServer
	port := acct.SMTPPort

	switch acct.Provider {
	case ProviderGmail:
		if addr == "" {
			addr = "smtp.gmail.com"
		}
		if port == 0 {
			port = 587
		}
	case ProviderICloud:
		if addr == "" {
			addr = "smtp.mail.me.com"
		}
		if port == 0 {
			port = 587
		}
	default:
		if port == 0 {
			port = 587
		}
	}

	return fmt.Sprintf("%s:%d", addr, port)
}

func (c *NetworkConfig) GetFolder(acct IMAPAccount) string {
	if acct.Folder != "" {
		return acct.Folder
	}
	return "INBOX"
}

func (c *NetworkConfig) GetPollInterval(acct IMAPAccount) int {
	if acct.PollInterval > 0 {
		return acct.PollInterval
	}
	return 30
}

func (c *NetworkConfig) GetUsername(acct IMAPAccount) string {
	if acct.Username != "" {
		return acct.Username
	}
	return acct.Email
}

const exampleConfig = `# Email accounts to bridge.
# Each account gets its own set of rooms, one room per sender domain
# (e.g. bewerbung5@messe-jobs.de and bewerbung6@messe-jobs.de land in the
# same room).
#
# For Gmail use an app password, not your normal account password.
# For iCloud use an app-specific password.
#
# Provider can be one of: gmail, icloud, custom
accounts:
    - email: user@gmail.com
      provider: gmail
      password: "your-app-password"
      # display_name: "Gmail Account"
      # poll_interval_seconds: 30
      # folder: INBOX
    - email: user@icloud.com
      provider: icloud
      password: "your-app-specific-password"
    - email: user@example.com
      provider: custom
      imap_server: mail.example.com
      imap_port: 993
      smtp_server: smtp.example.com
      smtp_port: 587
      password: "your-password"
`

func (c *NetworkConfig) DoUpgrade(helper configupgrade.Helper) {}
