package card_info_apple

import (
	"context"
	"kami/internal/consts"
	"kami/internal/errHandler"
	"kami/internal/service"

	"kami/api/card_info_apple/v1"

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

func (c *ControllerV1) CardInfoSuspendOrContinue(ctx context.Context, req *v1.CardInfoSuspendOrContinueReq) (res *v1.CardInfoSuspendOrContinueRes, err error) {
	appleAccountService := service.AppleAccount()
	accountInfo, err := appleAccountService.GetDetailById(ctx, req.ID)
	if err != nil || accountInfo == nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotFound, err, "获取苹果账号错误")
		return
	}
	switch accountInfo.Status {
	case int(consts.AppleAccountNormal):
		err = appleAccountService.ModifyStatus(ctx, req.ID, consts.AppleAccountForbidden, nil)
	case int(consts.AppleAccountForbidden):
		err = appleAccountService.ModifyStatus(ctx, req.ID, consts.AppleAccountNormal, nil)
	default:
		err = gerror.NewCode(gcode.CodeInvalidParameter, "苹果账号未处于正常状态")
		return
	}
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotFound, err, "状态修改失败")
	}
	return
}
