package smtp

import (
	"bytes"
	"io"
	"io/ioutil"
	"log"

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

type Handler interface {
	Name() string
	Send(from string, to []string, r io.Reader) error
}

// type Session implements the User interface for emersion/go-smtp
type Session struct {
	ID      uint64
	Account *store.Account
	Handler Handler
}

func (s *Session) Send(from string, to []string, r io.Reader) error {
	// FIXME: TODO: don't keep this lot forever, it's for debugging purposes
	data, err := ioutil.ReadAll(r)
	if err != nil {
		return err
	}

	log.Printf("%s session %d for %s: from=%s, to=%s, r=<<EOF\n%s\nEOF", s.Handler.Name(), s.ID, s.Account.Username, from, to, string(data))

	memR := bytes.NewReader(data)
	return s.Handler.Send(from, to, memR)
}

func (s *Session) Logout() error {
	log.Printf("Ending %s session %d for %s", s.Handler.Name(), s.ID, s.Account.Username)

	return nil
}
