package smtp

import (
	"context"
	"fmt"
	"log"
	"sync/atomic"

	"github.com/emersion/go-smtp"

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

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

	// Session IDs
	sid uint64
}

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

	out.server = smtp.NewServer(out)
	out.server.Domain = datastore.Domain()
	out.server.TLSConfig = datastore.TLSConfig()
	out.server.Addr = ":587"

	return out
}

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

	m.cancel()
}

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

	return nil
}

// Backend implementation for go-smtp after this point

func (m *msa) Login(user, pass string) (smtp.User, error) {
	account, err := m.store.FindAccountWithPassword(user, pass)
	if err != nil {
		// Log the real error, but don't show it to the end user
		log.Printf("Beginning submission session for %s failed: %s", user, err)
		return nil, fmt.Errorf("Login failed")
	}

	sid := atomic.AddUint64(&m.sid, uint64(1))
	session := &Session{
		ID:      fmt.Sprintf("submission:%d", sid),
		Handler: &sender{msa: m, account: account},
	}

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

	return session, nil
}

func (m *msa) AnonymousLogin() (smtp.User, error) {
	return nil, smtp.AuthRequiredErr
}
