package jd_cookie

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

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

// CreateAccount 创建Cookie账户
func (c *ControllerV1) CreateAccount(ctx context.Context, req *v1.CreateAccountReq) (res *v1.CreateAccountRes, err error) {
	_, err = service.SysAuth().LoginOnlyIFrame(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}
	cookieId, status, err := service.JdCookie().CreateAccount(ctx, req.CookieValue, req.AccountName, req.Remark)
	if err != nil {
		return nil, gerror.WrapCode(gcode.CodeInternalError, err, "创建Cookie账户失败")
	}

	res = &v1.CreateAccountRes{
		CookieId: cookieId,
		Status:   consts.JdCookieStatus(status),
	}
	return
}
