package proxy

import (
	"context"

	pb "github.com/tidbcloud/cluster-service-ng/api/tidbcloud/dataplane-manager/v1"
	"google.golang.org/protobuf/types/known/structpb"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/client-go/dynamic"
)

type K8sServer struct {
	pb.UnimplementedControlPlaneK8SProxyServiceServer
	k8sClient dynamic.Interface
}

func NewK8sServer(client dynamic.Interface) *K8sServer {
	return &K8sServer{k8sClient: client}
}

func (s *K8sServer) ListResources(ctx context.Context, req *pb.ListResourcesRequest) (*structpb.Struct, error) {
	gvr := schema.GroupVersionResource{Group: req.Group, Version: req.Version, Resource: req.ResourcePlural}
	return listResources(ctx, s.k8sClient.Resource(gvr).Namespace(req.Namespace), req.LabelSelector)
}

func (s *K8sServer) GetResource(ctx context.Context, req *pb.GetResourceRequest) (*structpb.Struct, error) {
	gvr := schema.GroupVersionResource{Group: req.Group, Version: req.Version, Resource: req.ResourcePlural}
	return getResource(ctx, s.k8sClient.Resource(gvr).Namespace(req.Namespace), req.Name)
}
