// Package exchange 提供换货用例的可靠通知编排。
package exchange

import (
	"context"

	"gorm.io/gorm"

	"github.com/break/junhong_cmp_fiber/pkg/errors"
)

// ShippingCreatedEvent 是物流换货单创建后通知个人客户的稳定业务事件。
type ShippingCreatedEvent struct {
	ExchangeID      uint
	ExchangeNo      string
	CustomerID      uint
	AssetType       string
	AssetID         uint
	AssetIdentifier string
	RequestID       string
	CorrelationID   string
}

// ShippingCreatedEventWriter 将物流换货创建通知写入可靠事件基础设施。
type ShippingCreatedEventWriter interface {
	Append(ctx context.Context, tx *gorm.DB, event ShippingCreatedEvent) error
}

// ShippingCreatedNotifier 在换货业务事务内编排个人客户通知事件。
type ShippingCreatedNotifier struct {
	writer ShippingCreatedEventWriter
}

// NewShippingCreatedNotifier 创建物流换货通知用例。
func NewShippingCreatedNotifier(writer ShippingCreatedEventWriter) *ShippingCreatedNotifier {
	return &ShippingCreatedNotifier{writer: writer}
}

// Notify 在换货单事务内追加指定个人客户的可靠通知事件。
func (n *ShippingCreatedNotifier) Notify(ctx context.Context, tx *gorm.DB, event ShippingCreatedEvent) error {
	if n == nil || n.writer == nil {
		return errors.New(errors.CodeInternalError, "物流换货通知事件 Writer 未配置")
	}
	if tx == nil || event.ExchangeID == 0 || event.ExchangeNo == "" || event.CustomerID == 0 ||
		event.AssetType == "" || event.AssetID == 0 || event.AssetIdentifier == "" {
		return errors.New(errors.CodeInvalidParam, "物流换货通知事件参数不完整")
	}
	return n.writer.Append(ctx, tx, event)
}
