package openapi

import (
	"context"

	"wechat-api/ent/contact"
	"wechat-api/internal/svc"
	"wechat-api/internal/types"
	"wechat-api/internal/utils/dberrorhandler"

	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"github.com/zeromicro/go-zero/core/logx"
)

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

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

func (l *OpenapiGetContactByWxidLogic) OpenapiGetContactByWxid(req *types.WxidReq) (resp *types.ContactInfoResp, err error) {

	data, err := l.svcCtx.DB.Contact.Query().Where(contact.Wxid(req.Wxid)).First(l.ctx)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	return &types.ContactInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Code: 0,
			Msg:  errormsg.Success,
		},
		Data: types.ContactInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:        &data.ID,
				CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
				UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
			},
			Status:    &data.Status,
			WxWxid:    &data.WxWxid,
			Type:      &data.Type,
			Wxid:      &data.Wxid,
			Account:   &data.Account,
			Nickname:  &data.Nickname,
			Markname:  &data.Markname,
			Headimg:   &data.Headimg,
			Sex:       &data.Sex,
			Starrole:  &data.Starrole,
			Dontseeit: &data.Dontseeit,
			Dontseeme: &data.Dontseeme,
			Lag:       &data.Lag,
			Gid:       &data.Gid,
			Gname:     &data.Gname,
			V3:        &data.V3,
		},
	}, nil
}
