package model

import (
	"strings"

	"github.com/tech/sendico/pkg/merrors"
)

// DemoRequest represents a request submitted from the marketing site to request a demo.
type DemoRequest struct {
	Name             string `json:"name"`
	OrganizationName string `json:"organizationName"`
	Phone            string `json:"phone"`
	WorkEmail        string `json:"workEmail"`
	PayoutVolume     string `json:"payoutVolume"`
	Comment          string `json:"comment,omitempty"`
}

// Normalize trims whitespace from all string fields.
func (dr *DemoRequest) Normalize() {
	if dr == nil {
		return
	}
	dr.Name = strings.TrimSpace(dr.Name)
	dr.OrganizationName = strings.TrimSpace(dr.OrganizationName)
	dr.Phone = strings.TrimSpace(dr.Phone)
	dr.WorkEmail = strings.TrimSpace(dr.WorkEmail)
	dr.PayoutVolume = strings.TrimSpace(dr.PayoutVolume)
	dr.Comment = strings.TrimSpace(dr.Comment)
}

// Validate ensures that all required fields are present.
func (dr *DemoRequest) Validate() error {
	if dr == nil {
		return merrors.InvalidArgument("request payload is empty", "request")
	}
	if (dr.WorkEmail == "") && (dr.Phone == "") {
		return merrors.InvalidArgument("work email or phone must not be empty", "request.workEmail", "request.phone")
	}
	return nil
}
