package whatsapp_channel

import (
	"context"
	"github.com/suyuan32/simple-admin-core/rpc/types/core"

	"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 GetWhatsappChannelByIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

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

func (l *GetWhatsappChannelByIdLogic) GetWhatsappChannelById(req *types.IDReq) (*types.WhatsappChannelInfoResp, error) {
	data, err := l.svcCtx.DB.WhatsappChannel.Get(l.ctx, req.Id)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: data.OrganizationID})
	if err != nil {
		l.Error("获取部门信息失败", err)
	}
	return &types.WhatsappChannelInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Code: 0,
			Msg:  errormsg.Success,
		},
		Data: types.WhatsappChannelInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:        &data.ID,
				CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
				UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
			},
			Status:           &data.Status,
			Ak:               &data.Ak,
			Sk:               &data.Sk,
			WaId:             &data.WaID,
			WaName:           &data.WaName,
			WabaId:           &data.WabaID,
			BusinessId:       &data.BusinessID,
			OrganizationId:   &data.OrganizationID,
			OrganizationName: departmentInfo.Name,
			VerifyAccount:    &data.VerifyAccount,
		},
	}, nil
}
