package user_admin

import (
	"context"
	"git.noahlan.cn/n-admin/n-admin-server/api/internal/svc"
	"git.noahlan.cn/n-admin/n-admin-server/api/internal/types"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"

	"net/http"
)

type GetUserListAdminLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	r      *http.Request
}

func NewGetUserListAdminLogic(r *http.Request, ctx context.Context, svcCtx *svc.ServiceContext) *GetUserListAdminLogic {
	return &GetUserListAdminLogic{
		r:      r,
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserListAdminLogic) GetUserListAdmin(req *types.UserReq) (resp *types.UserListResp, err error) {
	rpcResp, err := l.svcCtx.CoreRpc.GetUserList(l.ctx, &core.UserReq{
		ID:              req.ID,
		Username:        req.Username,
		PhoneNumber:     req.PhoneNumber,
		Email:           req.Email,
		WithRole:        false,
		WithSocial:      false,
		WithDepartment:  false,
		WithToken:       false,
		WithLoginRecord: true,
		Page:            l.svcCtx.Convert.ConvertPaginationRpc(req.Pagination),
	})
	if err != nil {
		return nil, err
	}
	resp = &types.UserListResp{
		Page: l.svcCtx.Convert.ConvertPaginationApi(rpcResp.Page),
		List: make([]*types.UserInfo, len(rpcResp.Data)),
	}
	for i, item := range rpcResp.Data {
		resp.List[i] = l.svcCtx.Convert.ConvertUser(l.ctx, item)
	}
	return
}
