package wxcard

import (
	"context"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"github.com/zeromicro/go-zero/core/errorx"
	"wechat-api/ent/wxcard"
	"wechat-api/internal/utils/dberrorhandler"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetApiWxCardDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetApiWxCardDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApiWxCardDetailLogic {
	return &GetApiWxCardDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx}
}

func (l *GetApiWxCardDetailLogic) GetApiWxCardDetail(req *types.IDReq) (*types.WxCardInfoResp, error) {
	if req.Id == 0 {
		return nil, errorx.NewDefaultError("参数错误")
	}

	data, err := l.svcCtx.DB.WxCard.Query().Where(wxcard.ID(req.Id)).Only(l.ctx)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	var showChat, showAi bool
	if data.APIKey != "" && data.APIBase != "" {
		showChat = true
	}
	if data.AiInfo != "" {
		showAi = true
	}

	return &types.WxCardInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Code: 0,
			Msg:  errormsg.Success,
		},
		Data: types.WxCardInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:        &data.ID,
				CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
				UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
			},
			Name:            &data.Name,
			Avatar:          &data.Avatar,
			Logo:            &data.Logo,
			Company:         &data.Company,
			Address:         &data.Address,
			Phone:           &data.Phone,
			OfficialAccount: &data.OfficialAccount,
			WechatAccount:   &data.WechatAccount,
			Email:           &data.Email,
			Intro:           &data.Intro,
			ShowChat:        &showChat,
			ShowAi:          &showAi,
		},
	}, nil
}
