package benefactorkindboxvalidator

import (
	"context"
	"errors"
	"testing"

	param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
	"github.com/stretchr/testify/assert"
)

func TestValidator_ValidateGetRequest(t *testing.T) {
	mockRepository := NewMockRepository(t)
	mockAddressSvc := NewMockAddressSvc(t)
	mockReferTimeSvc := NewMockReferTimeSvc(t)
	validator := New(mockRepository, mockAddressSvc, mockReferTimeSvc)
	ctx := context.Background()

	t.Run("Successful validation", func(t *testing.T) {
		req := param.KindBoxGetRequest{
			BenefactorID: 1,
			KindBoxID:    1,
		}

		mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(true, nil).Once()

		fieldErrors, err := validator.ValidateGetRequest(ctx, req)

		assert.NoError(t, err)
		assert.Nil(t, fieldErrors)
	})

	t.Run("Empty KindBox id", func(t *testing.T) {
		req := param.KindBoxGetRequest{
			BenefactorID: 1,
			KindBoxID:    0,
		}

		fieldErrors, err := validator.ValidateGetRequest(ctx, req)

		assert.Error(t, err)
		assert.NotNil(t, fieldErrors)
		assert.Contains(t, fieldErrors, "KindBoxID")
	})

	t.Run("KindBox does not exist", func(t *testing.T) {
		req := param.KindBoxGetRequest{
			BenefactorID: 1,
			KindBoxID:    1,
		}

		mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(false, nil).Once()

		fieldErrors, err := validator.ValidateGetRequest(ctx, req)

		assert.Error(t, err)
		assert.NotNil(t, fieldErrors)
		assert.Equal(t, errmsg.ErrorMsgNotFound, fieldErrors["KindBoxID"])
	})

	t.Run("KindBox exist error", func(t *testing.T) {
		req := param.KindBoxGetRequest{
			BenefactorID: 1,
			KindBoxID:    1,
		}

		mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(false, errors.New("repo error")).Once()

		fieldErrors, err := validator.ValidateGetRequest(ctx, req)

		assert.Error(t, err)
		assert.NotNil(t, fieldErrors)
		assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["KindBoxID"])
	})
}
