package infra

// EmailWithAttachment returns a copy of msg with the given attachment appended.
// Does not mutate the original message — always returns a new EmailMessage.
func EmailWithAttachment(msg EmailMessage, att EmailAttachment) EmailMessage {
	newAtts := make([]EmailAttachment, len(msg.Attachments)+1)
	copy(newAtts, msg.Attachments)
	newAtts[len(msg.Attachments)] = att

	toSlice := make([]string, len(msg.To))
	copy(toSlice, msg.To)

	ccSlice := make([]string, len(msg.CC))
	copy(ccSlice, msg.CC)

	bccSlice := make([]string, len(msg.BCC))
	copy(bccSlice, msg.BCC)

	var headers map[string]string
	if len(msg.Headers) > 0 {
		headers = make(map[string]string, len(msg.Headers))
		for k, v := range msg.Headers {
			headers[k] = v
		}
	}

	return EmailMessage{
		From:        msg.From,
		To:          toSlice,
		CC:          ccSlice,
		BCC:         bccSlice,
		Subject:     msg.Subject,
		BodyHTML:    msg.BodyHTML,
		BodyText:    msg.BodyText,
		Attachments: newAtts,
		Headers:     headers,
	}
}
