// Package packageexpiry 编排套餐临期节点提醒用例。
package packageexpiry

import (
	"context"

	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
)

// ReminderScanner 查询当天命中临期节点的资产。
type ReminderScanner interface {
	ReminderCandidates(ctx context.Context) ([]dto.ExpiringAssetItem, error)
}

// ReminderPublisher 批量发布个人客户临期通知。
type ReminderPublisher interface {
	Publish(ctx context.Context, candidates []dto.ExpiringAssetItem) error
}

// ReminderService 扫描并发布每日套餐临期节点提醒。
type ReminderService struct {
	scanner   ReminderScanner
	publisher ReminderPublisher
}

// NewReminderService 创建套餐临期提醒用例。
func NewReminderService(scanner ReminderScanner, publisher ReminderPublisher) *ReminderService {
	return &ReminderService{scanner: scanner, publisher: publisher}
}

// Run 执行当天 15、7、3 天节点扫描并可靠发布通知。
func (s *ReminderService) Run(ctx context.Context) error {
	if s == nil || s.scanner == nil || s.publisher == nil {
		return errors.New(errors.CodeInternalError, "套餐临期提醒用例未配置")
	}
	candidates, err := s.scanner.ReminderCandidates(ctx)
	if err != nil {
		return err
	}
	if len(candidates) == 0 {
		return nil
	}
	return s.publisher.Publish(ctx, candidates)
}
