package adminkindboxreqvalidator

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

	"git.gocasts.ir/ebhomengo/niki/entity"
	adminparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
	param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
	validation "github.com/go-ozzo/ozzo-validation/v4"
)

func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) *ValidatorError {
	const op = "adminkindboxreqvalidator.ValidateAddRequest"

	if err := validation.ValidateStruct(&req,

		validation.Field(&req.CountRequested, validation.Required, validation.Min(uint(MinKindBoxReq)), validation.Max(uint(MaxKindBoxReq))),

		validation.Field(&req.BenefactorID,
			validation.Required,
			validation.By(v.doesBenefactorExist)),

		validation.Field(&req.KindBoxType,
			validation.Required,
			validation.By(v.doesTypeExist)),

		validation.Field(&req.DeliverAddressID,
			validation.Required,
			validation.By(v.doesAddressExist(req.BenefactorID))),

		validation.Field(&req.DeliverReferDate,
			validation.Required,
			validation.By(v.isDateValid),
		),
	); err != nil {

		fieldErrors := make(map[string]string)

		var errV validation.Errors
		if errors.As(err, &errV) {
			for key, value := range errV {
				if value != nil {
					fieldErrors[key] = value.Error()
				}
			}
		}

		return &ValidatorError{
			Fields: fieldErrors,
			Err: richerror.New(op).
				WithMessage(errmsg.ErrorMsgInvalidInput).
				WithKind(richerror.KindInvalid).
				WithMeta(map[string]interface{}{"req": req}).
				WithErr(err),
		}

	}

	return nil
}

func (v Validator) doesBenefactorExist(value interface{}) error {
	benefactorID, ok := value.(uint)
	if !ok {
		return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
	}
	_, err := v.benefactorSvc.BenefactorExistByID(context.Background(), adminparam.BenefactorExistByIDRequest{ID: benefactorID})
	if err != nil {
		return fmt.Errorf(errmsg.ErrorMsgNotFound)
	}

	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(benefactorID uint) validation.RuleFunc {
	return func(value interface{}) error {
		addressID, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		address, err := v.benefactorSvc.AddressExistByID(context.Background(), adminparam.GetAddressByIDRequest{ID: addressID})
		if err != nil {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		if address.Address == nil {
			return fmt.Errorf(errmsg.ErrorMsgNotFound)
		}
		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
}

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