package oauthprovider_admin

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

	"git.noahlan.cn/n-admin/n-admin-server/api/internal/svc"
	"git.noahlan.cn/n-admin/n-admin-server/api/internal/types"

	"net/http"
)

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

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

func (l *GetOauthProviderListAdminLogic) GetOauthProviderListAdmin(req *types.OauthProviderReq) (resp *types.OauthProviderListResp, err error) {
	rpcResp, err := l.svcCtx.CoreRpc.GetOauthProviderList(l.ctx, &core.OauthProviderReq{
		ID:       req.ID,
		Name:     req.Name,
		ClientId: req.ClientId,
		System:   req.System,
		Init:     req.Init,
		Page:     l.svcCtx.Convert.ConvertPaginationRpc(req.Pagination),
	})
	if err != nil {
		return nil, err
	}
	resp = &types.OauthProviderListResp{
		Page: l.svcCtx.Convert.ConvertPaginationApi(rpcResp.Page),
		List: make([]*types.OauthProviderInfo, len(rpcResp.Data)),
	}
	for i, item := range rpcResp.Data {
		resp.List[i] = l.svcCtx.Convert.ConvertOAuthProvider(l.ctx, item)
	}
	return
}
