package smtp

import (
	"context"
	"fmt"
	"io"
	"log"
	"net/mail"
	"sync/atomic"

	"github.com/emersion/go-smtp"

	"ur.gs/crockery/internal/store"
)

type mta struct {
	cancel context.CancelFunc
	store  store.Interface
	server *smtp.Server

	// Session IDs
	sid uint64
}

func NewMTA(cancel context.CancelFunc, datastore store.Interface) Server {
	out := &mta{
		cancel: cancel,
		store:  datastore,
	}

	out.server = smtp.NewServer(out)
	out.server.Domain = datastore.Domain()
	out.server.TLSConfig = datastore.TLSConfig()
	out.server.Addr = ":25"
	out.server.AuthDisabled = true // Don't allow login on the MTA port

	return out
}

func (m *mta) Run() {
	if err := m.server.ListenAndServe(); err != nil {
		log.Printf("Error serving SMTP %s: %v", m.server.Addr, err)
	} else {
		log.Printf("Stopped listening on SMTP %s", m.server.Addr)
	}

	m.cancel()
}

func (m *mta) Close() error {
	m.cancel() // FIXME: this doesn't touch the server

	return nil
}

// Backend implementation for go-smtp after this point

func (m *mta) Login(user, pass string) (smtp.User, error) {
	return nil, fmt.Errorf("Submission to this MTA is not permitted.")
}

func (m *mta) AnonymousLogin() (smtp.User, error) {
	sid := atomic.AddUint64(&m.sid, uint64(1))
	session := &Session{
		ID:      fmt.Sprintf("smtp:%d", sid),
		Handler: m,
	}

	log.Printf("Beginning session %d", session.ID)
	// FIXME: TODO: Track ongoing sessions for termination or notifications

	return session, nil
}

// User implementation for go-smtp after this point
// Since only anonymous login is permitted, there's no need to introduce an
// intermediary to track the logged-in user.
//
// TODO: wildcard address support. For now, just dump everything directly
// into a single hardcoded mailbox per-account
func (m *mta) ServeSMTP(from string, to []string, r io.Reader) error {
	emails := make([]string, len(to))

	for i, entry := range to {
		addr, err := mail.ParseAddress(entry) // FIXME: should this be AddressList?
		if err != nil {
			return err
		}
		emails[i] = addr.Address
	}

	log.Printf("emails: %#v", emails)

	recipients, err := m.store.FindAccounts(emails...)
	if err != nil {
		return err
	}

	if len(recipients) != len(to) {
		return fmt.Errorf("At least one recipient is not known locally")
	}

	return m.store.SpoolMessage(recipients, r)
}
