package domain

import "fmt"

const (
	ReplyPublicComment = "public_comment"
	ReplyDM            = "dm"
	ReplyNoSales       = "no_sales"
	ReplyProfessional  = "professional"
	ReplyHumorous      = "humorous"

	SentOutbox     = "outbox"
	SentManualCopy = "manual_copy"
)

// ReplyVariant is one AI-generated reply draft for an opportunity.
type ReplyVariant struct {
	ID            string `bson:"_id" json:"id"`
	OwnerUID      int64  `bson:"owner_uid" json:"owner_uid"`
	OpportunityID string `bson:"opportunity_id" json:"opportunity_id"`
	Variant       string `bson:"variant" json:"variant"`
	Text          string `bson:"text" json:"text"`
	UsedAt        int64  `bson:"used_at,omitempty" json:"used_at,omitempty"`
	SentChannel   string `bson:"sent_channel,omitempty" json:"sent_channel,omitempty"`
	// OutboxID 只在 sent_channel=outbox 且真的排入既有 Outbox 佇列時才有值（T550 真送出）。
	OutboxID  string `bson:"outbox_id,omitempty" json:"outbox_id,omitempty"`
	CreatedAt int64  `bson:"created_at" json:"created_at"`
}

func IsReplyVariant(s string) bool {
	switch s {
	case ReplyPublicComment, ReplyDM, ReplyNoSales, ReplyProfessional, ReplyHumorous:
		return true
	}
	return false
}

func (r *ReplyVariant) Normalize() error {
	if r.OwnerUID <= 0 || r.OpportunityID == "" {
		return fmt.Errorf("%w: owner_uid and opportunity_id required", ErrValidation)
	}
	if !IsReplyVariant(r.Variant) {
		return fmt.Errorf("%w: unknown reply variant %q", ErrValidation, r.Variant)
	}
	if r.Text == "" {
		return fmt.Errorf("%w: reply text required", ErrValidation)
	}
	return nil
}
