package benefactorkindboxreqvalidator

import (
	"testing"

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

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

	t.Run("Successful validation", func(t *testing.T) {
		req := kindboxreqparam.GetAllRequest{
			Pagination: param.PaginationRequest{},
			Sort: param.SortRequest{
				Field:     "id",
				Direction: "asc",
			},
			Filter: map[string]any{"id": 1},
		}

		fieldErrors, err := validator.ValidateGetAll(req)

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

	t.Run("Invalid sort field", func(t *testing.T) {
		req := kindboxreqparam.GetAllRequest{
			Pagination: param.PaginationRequest{},
			Sort: param.SortRequest{
				Field:     "invalid",
				Direction: "asc",
			},
			Filter: map[string]any{"id": 1},
		}

		fieldErrors, err := validator.ValidateGetAll(req)

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

	t.Run("Invalid sort direction", func(t *testing.T) {
		req := kindboxreqparam.GetAllRequest{
			Pagination: param.PaginationRequest{},
			Sort: param.SortRequest{
				Field:     "id",
				Direction: "invalid",
			},
			Filter: map[string]any{"id": 1},
		}

		fieldErrors, err := validator.ValidateGetAll(req)

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

	t.Run("Invalid filter", func(t *testing.T) {
		req := kindboxreqparam.GetAllRequest{
			Pagination: param.PaginationRequest{},
			Sort: param.SortRequest{
				Field:     "id",
				Direction: "desc",
			},
			Filter: map[string]any{"invalid": 1},
		}

		fieldErrors, err := validator.ValidateGetAll(req)

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