package apiproxy

import (
	"context"

	"go.uber.org/dig"
	"google.golang.org/grpc"

	api "github.com/tidbcloud/aisupport/packages/sdk-go/contracts/api/agent-sidecar/apiproxy/v1"
	"github.com/tidbcloud/aisupport/packages/sdk-go/infra/rpcserver"
	"github.com/tidbcloud/aisupport/services/agent-sidecar/internal/app"
	serviceapi "github.com/tidbcloud/aisupport/services/agent-sidecar/internal/services/apiproxy/api"
)

type HandlerParam struct {
	dig.In

	Service serviceapi.Service
}

type Handler struct {
	api.UnimplementedAPIProxyServiceServer

	service serviceapi.Service
}

func NewHandler(param HandlerParam) app.HandlerResult {
	return app.HandlerResult{Handler: &Handler{service: param.Service}}
}

func (h *Handler) Name() string { return "APIProxyService" }

func (h *Handler) RegisterRPC(server *grpc.Server) error {
	api.RegisterAPIProxyServiceServer(server, h)
	return nil
}

func (h *Handler) ListRoutes(ctx context.Context, _ *api.ListRoutesRequest) (*api.ListRoutesResponse, error) {
	return &api.ListRoutesResponse{Routes: h.service.ListRoutes(ctx)}, nil
}

var _ api.APIProxyServiceServer = (*Handler)(nil)
var _ rpcserver.RPCHandler = (*Handler)(nil)
