package card_info_walmart

import (
	"context"

	v1 "kami/api/card_info_walmart/v1"
	"kami/internal/consts"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/service"

	"github.com/duke-git/lancet/v2/pointer"
	"github.com/gogf/gf/v2/errors/gcode"
)

func (c *ControllerV1) StatsOverview(ctx context.Context, req *v1.StatsOverviewReq) (res *v1.StatsOverviewRes, err error) {
	userInfo, err := service.SysAuth().LoginWithIFrameAndLogin(ctx)
	if err != nil || pointer.IsNil(userInfo) {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}

	result, err := service.CardRedeemAccount().GroupStatsOverview(ctx, &model.RedeemAccountGroupStatsOverviewReq{
		GroupId:   req.GroupId,
		UserId:    userInfo.Id,
		AccountId: req.AccountId,
		Category:  consts.CardRedeemAccountCategoryWalMart,
		DateRange: req.DateRange,
	}, nil)
	if err != nil {
		return nil, err
	}
	return &v1.StatsOverviewRes{
		TotalActiveAccountCount: result.TotalActiveAccountCount,
		TotalAccountCount:       result.TotalAccountCount,
		TotalOrderAmount:        result.TotalOrderAmount,
		TotalOrderAmountSuccess: result.TotalOrderAmountSuccess,
		TotalOrderCount:         result.TotalOrderCount,
		TotalOrderCountSuccess:  result.TotalOrderCountSuccess,
	}, nil
}
