package jd_cookie

import (
	"context"
	"kami/api/jd_cookie/v1"
	"kami/internal/errHandler"
	"kami/internal/service"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
)

// BatchValidateCookie 批量验证Cookie是否有效
func (c *ControllerV1) BatchValidateCookie(ctx context.Context, req *v1.BatchValidateCookieReq) (res *v1.BatchValidateCookieRes, err error) {
	_, err = service.SysAuth().LoginOnlyIFrame(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}

	results, totalCount, validCount, invalidCount, err := service.JdCookie().BatchValidateCookie(ctx, req.Cookies)
	if err != nil {
		return nil, gerror.WrapCode(gcode.CodeInternalError, err, "批量验证Cookie失败")
	}

	res = &v1.BatchValidateCookieRes{
		TotalCount:   totalCount,
		ValidCount:   validCount,
		InvalidCount: invalidCount,
		Results:      results,
	}
	return
}
