package benefactorkindboxvalidator

import (
	"context"
	"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"
	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"
)

//go:generate mockery --name Repository
type Repository interface {
	BenefactorKindBoxExist(ctx context.Context, benefactorID uint, kindBoxID uint) (bool, error)
	GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, 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)
}

type Validator struct {
	repo         Repository
	addressSvc   AddressSvc
	referTimeSvc ReferTimeSvc
}

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

func (v Validator) doesBenefactorKindBoxExist(ctx context.Context, benefactorID uint) validation.RuleFunc {
	return func(value interface{}) error {
		kbID, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		exist, err := v.repo.BenefactorKindBoxExist(ctx, benefactorID, kbID)
		if err != nil {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		if !exist {
			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) doesKindBoxHaveDeliveredStatus(ctx context.Context) validation.RuleFunc {
	return func(value interface{}) error {
		kindBoxID, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		kindBox, err := v.repo.GetKindBox(ctx, kindBoxID)
		if err != nil {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		if kindBox.Status != entity.KindBoxDeliveredStatus {
			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
	}
}
