package benefactorkindboxvalidator

import (
	"context"
	"errors"
	"testing"
	"time"

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

func TestValidator_ValidateRegisterEmptyingRequest(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.KindBoxRegisterEmptyingRequest{
			KindBoxID:         1,
			BenefactorID:      1,
			ReturnAddressID:   1,
			ReturnReferTimeID: 1,
			ReturnReferDate:   time.Now().Add(time.Hour * 24),
		}

		mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(true, nil).Once()
		mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once()
		mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return(
			addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}},
			nil,
		).Once()
		mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return(
			refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}},
			nil,
		).Once()

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

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

	t.Run("Empty KindBox id", func(t *testing.T) {
		req := param.KindBoxRegisterEmptyingRequest{
			KindBoxID:         0,
			BenefactorID:      1,
			ReturnAddressID:   1,
			ReturnReferTimeID: 1,
			ReturnReferDate:   time.Now().Add(time.Hour * 24),
		}

		mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return(
			addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}},
			nil,
		).Once()
		mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return(
			refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}},
			nil,
		).Once()

		fieldErrors, err := validator.ValidateRegisterEmptyingRequest(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.KindBoxRegisterEmptyingRequest{
			KindBoxID:         2,
			BenefactorID:      1,
			ReturnAddressID:   1,
			ReturnReferTimeID: 1,
			ReturnReferDate:   time.Now().Add(time.Hour * 24),
		}

		mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(2)).Return(false, nil).Once()
		mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return(
			addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}},
			nil,
		).Once()
		mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return(
			refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}},
			nil,
		).Once()

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

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

	t.Run("Invalid KindBox status", func(t *testing.T) {
		req := param.KindBoxRegisterEmptyingRequest{
			KindBoxID:         3,
			BenefactorID:      1,
			ReturnAddressID:   1,
			ReturnReferTimeID: 1,
			ReturnReferDate:   time.Now().Add(time.Hour * 24),
		}

		mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(3)).Return(true, nil).Once()
		mockRepository.EXPECT().GetKindBox(ctx, uint(3)).Return(entity.KindBox{Status: entity.KindBoxAssignedReceiverAgentStatus}, nil).Once()
		mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return(
			addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}},
			nil,
		).Once()
		mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return(
			refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}},
			nil,
		).Once()

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

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

	t.Run("KindBox exist error", func(t *testing.T) {
		req := param.KindBoxRegisterEmptyingRequest{
			KindBoxID:         1,
			BenefactorID:      1,
			ReturnAddressID:   1,
			ReturnReferTimeID: 1,
			ReturnReferDate:   time.Now().Add(time.Hour * 24),
		}

		mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(false, errors.New("repo error")).Once()
		mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return(
			addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}},
			nil,
		).Once()
		mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return(
			refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}},
			nil,
		).Once()

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

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

	t.Run("Empty ReturnAddress id", func(t *testing.T) {
		req := param.KindBoxRegisterEmptyingRequest{
			KindBoxID:         1,
			BenefactorID:      1,
			ReturnAddressID:   0,
			ReturnReferTimeID: 1,
			ReturnReferDate:   time.Now().Add(time.Hour * 24),
		}

		mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(true, nil).Once()
		mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once()
		mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return(
			refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}},
			nil,
		).Once()

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

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

	t.Run("ReturnAddress does not exist", func(t *testing.T) {
		req := param.KindBoxRegisterEmptyingRequest{
			KindBoxID:         1,
			BenefactorID:      1,
			ReturnAddressID:   2,
			ReturnReferTimeID: 1,
			ReturnReferDate:   time.Now().Add(time.Hour * 24),
		}

		mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(true, nil).Once()
		mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once()
		mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 2}).Return(
			addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 3}},
			nil,
		).Once()
		mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return(
			refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}},
			nil,
		).Once()

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

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

	t.Run("ReturnAddress exist error", func(t *testing.T) {
		req := param.KindBoxRegisterEmptyingRequest{
			KindBoxID:         1,
			BenefactorID:      1,
			ReturnAddressID:   1,
			ReturnReferTimeID: 1,
			ReturnReferDate:   time.Now().Add(time.Hour * 24),
		}

		mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(true, nil).Once()
		mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once()
		mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return(
			addressparam.GetAddressByIDResponse{},
			errors.New("service error"),
		).Once()
		mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return(
			refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}},
			nil,
		).Once()

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

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

	t.Run("Empty ReturnReferDate", func(t *testing.T) {
		req := param.KindBoxRegisterEmptyingRequest{
			KindBoxID:         1,
			BenefactorID:      1,
			ReturnAddressID:   1,
			ReturnReferTimeID: 1,
		}

		mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(true, nil).Once()
		mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once()
		mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return(
			addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}},
			nil,
		).Once()
		mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return(
			refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}},
			nil,
		).Once()

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

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

	t.Run("Invalid ReturnReferDate", func(t *testing.T) {
		req := param.KindBoxRegisterEmptyingRequest{
			KindBoxID:         1,
			BenefactorID:      1,
			ReturnAddressID:   1,
			ReturnReferTimeID: 1,
			ReturnReferDate:   time.Now().Add(time.Hour * -24),
		}

		mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(true, nil).Once()
		mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once()
		mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return(
			addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}},
			nil,
		).Once()
		mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return(
			refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}},
			nil,
		).Once()

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

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

	t.Run("Empty ReturnReferTime id", func(t *testing.T) {
		req := param.KindBoxRegisterEmptyingRequest{
			KindBoxID:         1,
			BenefactorID:      1,
			ReturnAddressID:   1,
			ReturnReferTimeID: 0,
			ReturnReferDate:   time.Now().Add(time.Hour * 24),
		}

		mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(true, nil).Once()
		mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once()
		mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return(
			addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}},
			nil,
		).Once()

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

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

	t.Run("Invalid ReturnReferTime status", func(t *testing.T) {
		req := param.KindBoxRegisterEmptyingRequest{
			KindBoxID:         1,
			BenefactorID:      1,
			ReturnAddressID:   1,
			ReturnReferTimeID: 2,
			ReturnReferDate:   time.Now().Add(time.Hour * 24),
		}

		mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(true, nil).Once()
		mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once()
		mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return(
			addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}},
			nil,
		).Once()
		mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 2}).Return(
			refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeInactiveStatus}},
			nil,
		).Once()

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

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

	t.Run("ReturnReferTime exist error", func(t *testing.T) {
		req := param.KindBoxRegisterEmptyingRequest{
			KindBoxID:         1,
			BenefactorID:      1,
			ReturnAddressID:   1,
			ReturnReferTimeID: 1,
			ReturnReferDate:   time.Now().Add(time.Hour * 24),
		}

		mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(true, nil).Once()
		mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once()
		mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return(
			addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}},
			nil,
		).Once()
		mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return(
			refertimeparam.GetReferTimeResponse{},
			errors.New("service error"),
		).Once()

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

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