package camel_oil

import (
	"context"

	"github.com/gogf/gf/v2/errors/gcode"
	v1 "kami/api/camel_oil/v1"
	"kami/internal/consts"
	"kami/internal/errHandler"
	"kami/internal/service"
)

func (c *ControllerV1) CheckAccount(ctx context.Context, req *v1.CheckAccountReq) (res *v1.CheckAccountRes, err error) {
	_, err = service.SysAuth().LoginOnlyIFrame(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}
	account, err := service.CamelOil().GetAccountInfo(ctx, req.AccountId)
	if err != nil {
		return nil, err
	}

	res = &v1.CheckAccountRes{
		IsOnline:      account.Status == int(consts.CamelOilAccountStatusOnline),
		Status:        consts.CamelOilAccountStatus(account.Status),
		StatusText:    getAccountStatusTextForStatus(account.Status),
		FailureReason: account.FailureReason,
	}

	return res, nil
}

// getAccountStatusTextForStatus 获取账号状态文本
func getAccountStatusTextForStatus(status int) string {
	switch status {
	case 1:
		return "待登录"
	case 2:
		return "在线"
	case 3:
		return "暂停"
	case 4:
		return "已失效"
	case 5:
		return "登录失败"
	default:
		return "未知"
	}
}
