package benefactorkindboxreqvalidator

import (
	"context"
	"errors"
	"testing"

	"git.gocasts.ir/ebhomengo/niki/entity"
	param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
	"github.com/stretchr/testify/assert"
)

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

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

		mockRepository.EXPECT().GetKindBoxReqByID(ctx, req.KindBoxReqID).Return(
			entity.KindBoxReq{ID: req.KindBoxReqID, BenefactorID: req.BenefactorID},
			nil,
		).Once()
		fieldErrors, err := validator.ValidateGetRequest(ctx, req)

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

	t.Run("Empty KindBoxReq id", func(t *testing.T) {
		req := param.KindBoxReqGetRequest{
			BenefactorID: 1,
			KindBoxReqID: 0,
		}

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

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

	t.Run("KindBoxReq does not exist", func(t *testing.T) {
		req := param.KindBoxReqGetRequest{
			BenefactorID: 1,
			KindBoxReqID: 1,
		}

		mockRepository.EXPECT().GetKindBoxReqByID(ctx, req.KindBoxReqID).Return(
			entity.KindBoxReq{},
			errors.New("repo error"),
		).Once()
		fieldErrors, err := validator.ValidateGetRequest(ctx, req)

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

	t.Run("KindBoxReq does not belong to benefactor", func(t *testing.T) {
		req := param.KindBoxReqGetRequest{
			BenefactorID: 1,
			KindBoxReqID: 1,
		}

		mockRepository.EXPECT().GetKindBoxReqByID(ctx, req.KindBoxReqID).Return(
			entity.KindBoxReq{BenefactorID: 2},
			nil,
		).Once()

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

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