package benefactorkindboxreqvalidator

import (
	"context"
	"errors"
	"fmt"
	"slices"
	"time"

	"git.gocasts.ir/ebhomengo/niki/entity"
	params "git.gocasts.ir/ebhomengo/niki/param"
	addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
	param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
	refertimeparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/refer_time"
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
	validation "github.com/go-ozzo/ozzo-validation/v4"
)

const (
	MinKindBoxReq uint = 1
	MaxKindBoxReq uint = 100
)

//go:generate mockery --name BenefactorSvc
type BenefactorSvc interface {
	BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error)
}

//go:generate mockery --name AddressSvc
type AddressSvc interface {
	AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error)
}

//go:generate mockery --name ReferTimeSvc
type ReferTimeSvc interface {
	GetReferTimeByID(ctx context.Context, req refertimeparam.GetReferTimeRequest) (refertimeparam.GetReferTimeResponse, error)
}

//go:generate mockery --name Repository
type Repository interface {
	KindBoxRequestExist(ctx context.Context, id uint) (bool, error)
	GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error)
}

type Validator struct {
	benefactorSvc BenefactorSvc
	addressSvc    AddressSvc
	referTimeSvc  ReferTimeSvc
	repo          Repository
}

type ValidatorError struct {
	Fields map[string]string `json:"error"`
	Err    error             `json:"message"`
}

func (v ValidatorError) Error() string {
	var err string

	for key, value := range v.Fields {
		err += fmt.Sprintf("%s: %s\n", key, value)
	}

	return err
}

func New(benefactorSvc BenefactorSvc, addressSvc AddressSvc, referTimeSvc ReferTimeSvc, repo Repository) Validator {
	return Validator{benefactorSvc: benefactorSvc, addressSvc: addressSvc, referTimeSvc: referTimeSvc, repo: repo}
}

func (v Validator) doesBenefactorExist(ctx context.Context) validation.RuleFunc {
	return func(value interface{}) error {
		benefactorID, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		res, err := v.benefactorSvc.BenefactorExistByID(ctx, param.BenefactorExistByIDRequest{ID: benefactorID})
		if err != nil {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		if !res.Existed {
			return fmt.Errorf(errmsg.ErrorMsgBenefactorNotFound)
		}

		return nil
	}
}

func (v Validator) doesTypeExist(value interface{}) error {
	kindBoxType, ok := value.(entity.KindBoxType)
	if !ok {
		return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
	}
	if !kindBoxType.IsValid() {
		return fmt.Errorf(errmsg.ErrorMsgNotFound)
	}

	return nil
}

func (v Validator) doesAddressExist(ctx context.Context, benefactorID uint) validation.RuleFunc {
	return func(value interface{}) error {
		addressID, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		address, err := v.addressSvc.AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: addressID})
		if err != nil {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		if address.Address.BenefactorID != benefactorID {
			return fmt.Errorf(errmsg.ErrorMsgNotFound)
		}

		return nil
	}
}

func (v Validator) isDateValid(value interface{}) error {
	date, ok := value.(time.Time)
	if !ok {
		return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
	}
	if date.Before(time.Now()) {
		return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
	}

	return nil
}

func (v Validator) isReferTimeIDValid(ctx context.Context) validation.RuleFunc {
	return func(value interface{}) error {
		referTimeID, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		resp, gErr := v.referTimeSvc.GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{
			ReferTimeID: referTimeID,
		})
		if gErr != nil {
			return fmt.Errorf(errmsg.ErrorMsgReferTimeNotFound)
		}
		if resp.ReferTime.Status != entity.ReferTimeActiveStatus {
			return fmt.Errorf(errmsg.ErrorMsgReferTimeIsNotActive)
		}

		return nil
	}
}

func (v Validator) doesKindBoxBelongToBenefactor(ctx context.Context, benefactorID uint) validation.RuleFunc {
	return func(value interface{}) error {
		kindBoxReqID, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgNotFound)
		}
		kindBoxReq, err := v.repo.GetKindBoxReqByID(ctx, kindBoxReqID)
		if err != nil {
			return fmt.Errorf(errmsg.ErrorMsgNotFound)
		}
		if kindBoxReq.BenefactorID != benefactorID {
			return fmt.Errorf(errmsg.ErrorMsgKindBoxReqDoesntBelongToBenefactor)
		}

		return nil
	}
}

func (v Validator) doesKindBoxRequestHavePendingStatus(ctx context.Context) validation.RuleFunc {
	return func(value interface{}) error {
		kindBoxReqID, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgNotFound)
		}
		kindBoxReq, err := v.repo.GetKindBoxReqByID(ctx, kindBoxReqID)
		if err != nil {
			return fmt.Errorf(errmsg.ErrorMsgNotFound)
		}
		if kindBoxReq.Status != entity.KindBoxReqPendingStatus {
			return fmt.Errorf(errmsg.ErrorMsgInvalidStatus)
		}

		return nil
	}
}

func (v Validator) areFilterFieldsValid(validFilters []string) validation.RuleFunc {
	return func(value interface{}) error {
		filters, ok := value.(params.FilterRequest)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		for filter := range filters {
			if !slices.Contains(validFilters, filter) {
				return fmt.Errorf(errmsg.ErrorMsgFiltersAreNotValid)
			}
		}

		return nil
	}
}

func (v Validator) areSortFieldsValid(validSortFields []string) validation.RuleFunc {
	return func(value interface{}) error {
		sort, ok := value.(params.SortRequest)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		if sort.Field == "" && sort.Direction != "" {
			return fmt.Errorf(errmsg.ErrorMsgSortFieldIsRequired)
		}
		if sort.Direction != "" && sort.Direction != params.AscSortDirection && sort.Direction != params.DescSortDirection {
			return fmt.Errorf(errmsg.ErrorMsgSortDirectionShouldBeAscOrDesc)
		}
		if sort.Field != "" && !slices.Contains(validSortFields, sort.Field) {
			return fmt.Errorf(errmsg.ErrorMsgSortFieldIsNotValid)
		}

		return nil
	}
}

func (v Validator) doesKindBoxRequestExist(ctx context.Context) validation.RuleFunc {
	return func(value interface{}) error {
		kindboxreqID, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		if isExist, err := v.repo.KindBoxRequestExist(ctx, kindboxreqID); !isExist || err != nil {
			if err != nil {
				return err
			}
			if !isExist {
				return errors.New("kind box request is not exist")
			}
		}

		return nil
	}
}

func (v Validator) doesBenefactorAddressExist(ctx context.Context, benefactorID uint) validation.RuleFunc {
	return func(value interface{}) error {
		addressID, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}

		address, aErr := v.addressSvc.AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: addressID})
		if aErr != nil {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		if address.Address.BenefactorID != benefactorID {
			return fmt.Errorf(errmsg.ErrorMsgNotFound)
		}

		return nil
	}
}