// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1

package threads_account

import (
	"context"

	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetPublishSlotInsightsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetPublishSlotInsightsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPublishSlotInsightsLogic {
	return &GetPublishSlotInsightsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetPublishSlotInsightsLogic) GetPublishSlotInsights(req *types.ThreadsAccountPath) (resp *types.PublishSlotInsightsData, err error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	policy, _ := l.svcCtx.PublishInventory.Get(l.ctx, tenantID, uid, req.ID)
	timezone := "Asia/Taipei"
	if policy != nil && policy.Timezone != "" {
		timezone = policy.Timezone
	}
	items, err := l.svcCtx.PublishQueue.ListSlotInsights(l.ctx, tenantID, uid, req.ID, timezone)
	if err != nil {
		return nil, err
	}
	list := make([]types.PublishSlotInsightData, 0, len(items))
	for _, item := range items {
		list = append(list, types.PublishSlotInsightData{
			Weekday: item.Weekday, Time: item.Time, AvgLikes1h: item.AvgLikes1h, AvgReplies1h: item.AvgReplies1h,
			AvgLikes24h: item.AvgLikes24h, AvgReplies24h: item.AvgReplies24h,
			AvgLikes7d: item.AvgLikes7d, AvgReplies7d: item.AvgReplies7d,
			SampleSize: item.SampleSize, Recommendation: item.Recommendation,
		})
	}
	return &types.PublishSlotInsightsData{AccountID: req.ID, Timezone: timezone, Slots: list}, nil
}
