/*
 * Copyright 2025 CloudWeGo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package server

import (
	"fmt"

	internal_server "github.com/cloudwego/kitex/internal/server"
	"github.com/cloudwego/kitex/pkg/endpoint"
	"github.com/cloudwego/kitex/pkg/utils"
)

// WithUnaryOptions add unary options for server.
// It is used to isolate options that are only effective for unary/pingpong methods.
func WithUnaryOptions(opts ...UnaryOption) Option {
	return Option{F: func(o *internal_server.Options, di *utils.Slice) {
		var udi utils.Slice
		for _, opt := range opts {
			opt.F(&o.UnaryOptions, &udi)
		}
		di.Push(map[string]interface{}{
			"WithUnaryOptions": udi,
		})
	}}
}

// WithUnaryMiddleware add unary middleware for unary method.
func WithUnaryMiddleware(mw endpoint.UnaryMiddleware) UnaryOption {
	return UnaryOption{F: func(o *UnaryOptions, di *utils.Slice) {
		di.Push(fmt.Sprintf("WithUnaryMiddleware(%+v)", utils.GetFuncName(mw)))

		o.UnaryMiddlewares = append(o.UnaryMiddlewares, mw)
	}}
}

// WithUnaryMiddlewareBuilder add unary middleware builder for unary method.
func WithUnaryMiddlewareBuilder(mwb endpoint.UnaryMiddlewareBuilder) UnaryOption {
	return UnaryOption{F: func(o *UnaryOptions, di *utils.Slice) {
		di.Push(fmt.Sprintf("WithUnaryMiddlewareBuilder(%+v)", utils.GetFuncName(mwb)))

		o.UnaryMiddlewareBuilders = append(o.UnaryMiddlewareBuilders, mwb)
	}}
}
