package mail

import (
	"fmt"
	"time"
)

// SendLicense delivers the blob inline. It is signed public data, not a secret —
// it is useless on any instance other than the one it names.
func (s Sender) SendLicense(to, instanceName, blob string) error {
	return s.sendTemplate(to, "", "license", struct {
		InstanceName string
		Blob         string
	}{instanceName, blob})
}

// SendInstanceReady tells a customer their cloud instance exists, where it is,
// and when its licence runs out.
//
// The expiry is stated here rather than only in a later reminder: a Free licence
// that quietly expires in a month is a surprise, and the first email is the one
// people keep.
func (s Sender) SendInstanceReady(to, instanceName, loginURL string, expires time.Time) error {
	return s.sendTemplate(to, "", "instanceready", struct {
		InstanceName string
		LoginURL     string
		Expires      time.Time
	}{instanceName, loginURL, expires})
}

// SendRenewed confirms a renewal and states the new date.
func (s Sender) SendRenewed(to, instanceName string, expires time.Time) error {
	return s.sendTemplate(to, "", "renewed", struct {
		InstanceName string
		Expires      time.Time
	}{instanceName, expires})
}

// SendExpiring is the renew-now nudge, seven days out.
func (s Sender) SendExpiring(to, instanceName, portalURL string, expires time.Time) error {
	return s.sendTemplate(to, "", "expiring", struct {
		InstanceName string
		PortalURL    string
		Expires      time.Time
	}{instanceName, portalURL, expires})
}

// SendExpired states plainly what has stopped and what happens next.
//
// It names the deletion date rather than a vague warning: the whole point of the
// sequence is that nobody loses an instance without having been told a date. A
// zero deleteOn means the reaper is disabled, and then no date is claimed.
func (s Sender) SendExpired(to, instanceName, portalURL string, deleteOn time.Time) error {
	return s.sendTemplate(to, "", "expired", struct {
		InstanceName string
		PortalURL    string
		DeleteOn     time.Time
	}{instanceName, portalURL, deleteOn})
}

// SendDeletionWarning is the final countdown, sent at seven days and one day.
func (s Sender) SendDeletionWarning(to, instanceName, portalURL string, deleteOn time.Time, daysLeft int) error {
	when := fmt.Sprintf("in %d days", daysLeft)
	if daysLeft <= 1 {
		when = "tomorrow"
	}
	return s.sendTemplate(to, "", "deletionwarning", struct {
		InstanceName string
		PortalURL    string
		When         string
		DeleteOn     time.Time
	}{instanceName, portalURL, when, deleteOn})
}

// SendLinkReminder chases a self-hosted customer who paid but never linked.
func (s Sender) SendLinkReminder(to, instanceName string) error {
	return s.sendTemplate(to, "", "linkreminder", struct {
		InstanceName string
		PortalURL    string
	}{instanceName, s.PublicURL})
}
