package service

import "fmt"

// TrendResult 趋势返回结构
type TrendResult struct {
	Energy    []TrendPoint `json:"energy"`
	Comments  []TrendPoint `json:"comments"`
	Likes     []TrendPoint `json:"likes"`
	Favorites []TrendPoint `json:"favorites"`
}

// TrendsService Studio 趋势分析服务
type TrendsService struct {
	energyRepo    EnergyTrendStore
	commentRepo   CommentTrendStore
	likeRepo      LikeTrendStore
	favoriteRepo  FavoriteTrendStore
}

// NewTrendsService 构造函数
func NewTrendsService(energyRepo EnergyTrendStore, commentRepo CommentTrendStore, likeRepo LikeTrendStore, favoriteRepo FavoriteTrendStore) *TrendsService {
	return &TrendsService{
		energyRepo:   energyRepo,
		commentRepo:  commentRepo,
		likeRepo:     likeRepo,
		favoriteRepo: favoriteRepo,
	}
}

// GetTrends 获取指定时间范围的趋势数据
func (s *TrendsService) GetTrends(userID uint, since string) (*TrendResult, error) {
	energy, err := s.energyRepo.AggregateEnergyByAuthor(userID, since)
	if err != nil {
		return nil, fmt.Errorf("查询赋能趋势: %w", err)
	}

	comments, err := s.commentRepo.AggregateCommentsByAuthor(userID, since)
	if err != nil {
		return nil, fmt.Errorf("查询评论趋势: %w", err)
	}

	likes, err := s.likeRepo.AggregateLikesByAuthor(userID, since)
	if err != nil {
		return nil, fmt.Errorf("查询点赞趋势: %w", err)
	}

	favorites, err := s.favoriteRepo.AggregateFavoritesByAuthor(userID, since)
	if err != nil {
		return nil, fmt.Errorf("查询收藏趋势: %w", err)
	}

	return &TrendResult{
		Energy:    energy,
		Comments:  comments,
		Likes:     likes,
		Favorites: favorites,
	}, nil
}
