package scim

import (
	"context"

	"github.com/tidbcloud/account/idl/pbgen/proto/central"
	"github.com/tidbcloud/account/internel/svc/scim"
	"google.golang.org/protobuf/types/known/emptypb"
)

type ScimGroupService struct {
	central.UnimplementedScimGroupServiceServer
}

func NewScimGroupService() central.ScimGroupServiceServer {
	return &ScimGroupService{}
}

func (s *ScimGroupService) ListScimGroups(ctx context.Context, req *central.ListScimGroupsReq) (*central.ListScimGroupsRsp, error) {
	svc, _ := scim.GetScimGroupSvc()
	if req.Page < 1 {
		req.Page = 1
	}
	var offset = int((req.Page - 1) * req.PerPage)
	var limit = int(req.PerPage)
	total, groups, err := svc.ListScimGroups(ctx, req.OrgId, offset, limit)
	if err != nil {
		return nil, err
	}
	rsp := central.ListScimGroupsRsp{
		Total: uint32(total),
		Group: groups,
	}
	return &rsp, nil
}
func (s *ScimGroupService) UpdateScimGroupRole(ctx context.Context, req *central.UpdateScimGroupRoleReq) (*emptypb.Empty, error) {
	svc, _ := scim.GetScimGroupSvc()
	err := svc.UpdateScimGroupRole(ctx, req.GroupId, req.OrgRole, req.ProjectRole, req.ProjectIds)
	if err != nil {
		return nil, err
	}
	return &emptypb.Empty{}, nil
}
