package card_info_walmart

import (
	"context"
	"kami/api/commonApi"
	"kami/internal/consts"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/service"

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

	v1 "kami/api/card_info_walmart/v1"
)

func (c *ControllerV1) AccountDailySummary(ctx context.Context, req *v1.AccountDailySummaryReq) (res *v1.AccountDailySummaryRes, err error) {
	userInfo, err := service.SysAuth().LoginWithIFrameAndLogin(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}
	if userInfo.Id == "" && req.UserId != "" {
		userInfo.Id = req.UserId
	}
	total, output, err := service.CardRedeemAccount().GetSummary(ctx, &model.AccountDailySummaryGetInput{
		AccountDailySummaryReq: req,
		CreatedUserId:          userInfo.Id,
		Category:               consts.CardRedeemAccountCategoryWalMart,
	}, nil)
	if err != nil {
		return
	}
	res = &v1.AccountDailySummaryRes{
		CommonPageRes: commonApi.CommonPageRes[*v1.CardRedeemAccountSummary]{
			Total: total,
			CommonDataRes: commonApi.CommonDataRes[*v1.CardRedeemAccountSummary]{
				List: slice.Map(output, func(index int, item *model.AccountDailySummaryGetOutput) *v1.CardRedeemAccountSummary {
					item.CardRedeemAccountSummary.Date = item.CardRedeemAccountSummary.Date.EndOfDay()
					return item.CardRedeemAccountSummary
				}),
			},
		},
	}
	return
}
