package middleware

import (
	"context"
	"github.com/UpmeshLTD/urus-aio/infrastructure/logging"
	"github.com/UpmeshLTD/urus-aio/infrastructure/logging/mocks"
	"github.com/UpmeshLTD/urus-aio/infrastructure/server/endpoint"
	"github.com/UpmeshLTD/urus-aio/infrastructure/testing"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
	"github.com/stretchr/testify/mock"
)

var _ = Describe("logging", func() {
	Describe("LoggerInjection", func() {
		It("should inject logger", func() {
			logger := &mocks.Logger{}
			var actualLogger logging.Logger

			ep := LoggerInjection(logger)(endpoint.EndpointFunc(func(ctx context.Context, request interface{}) (response interface{}, err error) {
				actualLogger = logging.FromContext(ctx)
				return nil, nil
			}))

			_, _ = ep.Invoke(context.TODO(), nil)

			Expect(actualLogger).Should(Equal(logger))
		})
	})

	Describe("RequestLogging", func() {
		It("should log request", func() {
			logged := false
			logger := &mocks.Logger{}

			ep := LoggerInjection(logger)(
				RequestLogging()(
					endpoint.EndpointFunc(func(ctx context.Context, request interface{}) (response interface{}, err error) {
						return nil, nil
					}),
				),
			)

			logger.On("Infow", testing.MockAnythingSlice(17)...).Run(func(args mock.Arguments) {
				logged = true
			})

			_, _ = ep.Invoke(context.TODO(), nil)

			Expect(logged).Should(BeTrue())
		})
	})
})
