package smtp

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

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

type Session struct {
	ID      string
	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("session=%d from=%s to=%s r=<<EOF\n%s\nEOF", s.ID, from, to, string(data))

	// TODO: a chain of middlewares here
	return s.Handler.ServeSMTP(from, to, bytes.NewReader(data))
}

func (s *Session) Logout() error {
	log.Printf("Ending session %d", s.ID)

	return nil
}
