package adminvalidator

import (
	"context"
	"errors"
	"testing"

	"git.gocasts.ir/ebhomengo/niki/entity"
	adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
	"github.com/stretchr/testify/assert"
)

func TestValidateLoginWithPhoneNumberRequest(t *testing.T) {
	mockRepo := NewMockRepository(t)
	validator := New(mockRepo)
	ctx := context.Background()
	validPhoneNumber := "09123456789"
	validPassword := "validpassword"

	t.Run("Valid request", func(t *testing.T) {
		req := adminserviceparam.LoginWithPhoneNumberRequest{
			PhoneNumber: validPhoneNumber,
			Password:    validPassword,
		}

		mockRepo.EXPECT().AdminExistByPhoneNumber(ctx, req.PhoneNumber).Return(true, nil).Once()
		mockRepo.EXPECT().GetAdminByPhoneNumber(ctx, req.PhoneNumber).Return(entity.Admin{
			Status: entity.AdminActiveStatus,
		}, nil).Once()

		fieldErrors, err := validator.ValidateLoginWithPhoneNumberRequest(ctx, req)
		assert.NoError(t, err)
		assert.Nil(t, fieldErrors)
	})

	t.Run("Empty phone number", func(t *testing.T) {
		req := adminserviceparam.LoginWithPhoneNumberRequest{
			PhoneNumber: "",
			Password:    validPassword,
		}

		fieldErrors, err := validator.ValidateLoginWithPhoneNumberRequest(ctx, req)
		assert.Error(t, err)
		assert.NotNil(t, fieldErrors)
		assert.Contains(t, fieldErrors, "phone_number")
	})

	t.Run("Empty password", func(t *testing.T) {
		req := adminserviceparam.LoginWithPhoneNumberRequest{
			PhoneNumber: validPhoneNumber,
			Password:    "",
		}

		mockRepo.EXPECT().AdminExistByPhoneNumber(ctx, req.PhoneNumber).Return(true, nil).Once()
		mockRepo.EXPECT().GetAdminByPhoneNumber(ctx, req.PhoneNumber).Return(entity.Admin{
			Status: entity.AdminActiveStatus,
		}, nil).Once()

		fieldErrors, err := validator.ValidateLoginWithPhoneNumberRequest(ctx, req)
		assert.Error(t, err)
		assert.NotNil(t, fieldErrors)
		assert.Contains(t, fieldErrors, "password")
	})

	t.Run("Invalid phone number format", func(t *testing.T) {
		req := adminserviceparam.LoginWithPhoneNumberRequest{
			PhoneNumber: "12345",
			Password:    validPassword,
		}

		fieldErrors, err := validator.ValidateLoginWithPhoneNumberRequest(ctx, req)
		assert.Error(t, err)
		assert.NotNil(t, fieldErrors)
		assert.Equal(t, errmsg.ErrorMsgPhoneNumberIsNotValid, fieldErrors["phone_number"])
	})

	t.Run("Invalid password length", func(t *testing.T) {
		req := adminserviceparam.LoginWithPhoneNumberRequest{
			PhoneNumber: validPhoneNumber,
			Password:    "short",
		}

		mockRepo.EXPECT().AdminExistByPhoneNumber(ctx, req.PhoneNumber).Return(true, nil).Once()
		mockRepo.EXPECT().GetAdminByPhoneNumber(ctx, req.PhoneNumber).Return(entity.Admin{
			Status: entity.AdminActiveStatus,
		}, nil).Once()

		fieldErrors, err := validator.ValidateLoginWithPhoneNumberRequest(ctx, req)
		assert.Error(t, err)
		assert.NotNil(t, fieldErrors)
		assert.Contains(t, fieldErrors, "password")
	})

	t.Run("Phone number does not exist", func(t *testing.T) {
		req := adminserviceparam.LoginWithPhoneNumberRequest{
			PhoneNumber: validPhoneNumber,
			Password:    validPassword,
		}

		mockRepo.EXPECT().AdminExistByPhoneNumber(ctx, req.PhoneNumber).Return(false, nil).Once()

		fieldErrors, err := validator.ValidateLoginWithPhoneNumberRequest(ctx, req)
		assert.Error(t, err)
		assert.NotNil(t, fieldErrors)
		assert.Equal(t, errmsg.ErrorMsgPhoneNumberOrPassIsIncorrect, fieldErrors["phone_number"])
	})

	t.Run("Repository error", func(t *testing.T) {
		req := adminserviceparam.LoginWithPhoneNumberRequest{
			PhoneNumber: validPhoneNumber,
			Password:    validPassword,
		}

		mockRepo.EXPECT().AdminExistByPhoneNumber(ctx, req.PhoneNumber).Return(false, errors.New("repo error")).Once()

		fieldErrors, err := validator.ValidateLoginWithPhoneNumberRequest(ctx, req)
		assert.Error(t, err)
		assert.NotNil(t, fieldErrors)
		assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["phone_number"])
	})

	t.Run("Inactive admin is not allowed", func(t *testing.T) {
		req := adminserviceparam.LoginWithPhoneNumberRequest{
			PhoneNumber: validPhoneNumber,
			Password:    validPassword,
		}

		mockRepo.EXPECT().AdminExistByPhoneNumber(ctx, req.PhoneNumber).Return(true, nil).Once()
		mockRepo.EXPECT().GetAdminByPhoneNumber(ctx, req.PhoneNumber).Return(entity.Admin{
			Status: entity.AdminInactiveStatus,
		}, nil).Once()

		fieldErrors, err := validator.ValidateLoginWithPhoneNumberRequest(ctx, req)
		assert.Error(t, err)
		assert.NotNil(t, fieldErrors)
		assert.Equal(t, errmsg.ErrorMsgAdminNotAllowed, fieldErrors["phone_number"])
	})
}
