package main

func generateHandlersGRPC() {
	generateToFile(headerHandlersGRPC, templateFuncHandlersGRPC, "internal/api/grpc_handler_gen.go", nil, nil)
}

var headerHandlersGRPC = `// Code generated by internal/gen/api/main.go. DO NOT EDIT.

package api

import (
	"context"

	. "github.com/centrifugal/centrifugo/v6/internal/apiproto"

	"go.opentelemetry.io/otel/codes"
	"go.opentelemetry.io/otel/trace"
	"google.golang.org/grpc/status"
)
`

var templateFuncHandlersGRPC = `
// {{ .RequestCapitalized }} ...
func (s *grpcAPIService) {{ .RequestCapitalized }}(ctx context.Context, req *{{ .RequestCapitalized }}Request) (*{{ .RequestCapitalized }}Response, error) {
	resp := s.api.{{ .RequestCapitalized }}(ctx, req)
{{- if ne .RequestCapitalized "Batch" }}
	if s.config.UseOpenTelemetry && resp.Error != nil {
		span := trace.SpanFromContext(ctx)
		span.SetStatus(codes.Error, resp.Error.Error())
	}
	if resp.Error != nil && s.useTransportErrorMode(ctx) {
		incError(s.api.config.Protocol, "{{ .RequestSnake }}", resp.Error.Code)
		statusCode := MapErrorToGRPCCode(resp.Error)
		transportError, _ := status.New(statusCode, resp.Error.Message).WithDetails(resp.Error)
		return nil, transportError.Err()
	}
{{- end}}
{{- if ne .RequestCapitalized "Batch" }}
	if resp.Error != nil {
		incError(s.api.config.Protocol, "{{ .RequestSnake }}", resp.Error.Code)
	}
{{- end}}
	return resp, nil
}
`
