package agentkindboxreqvalidator

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

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

type Repository interface {
	KindBoxRequestExist(ctx context.Context, id uint) (bool, error)
	GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error)
}

type Validator struct {
	repo Repository
}

func New(repo Repository) Validator {
	return Validator{repo: repo}
}

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) IsSerialNumbersCountValid(ctx context.Context, kindBoxReqID uint) validation.RuleFunc {
	return func(value interface{}) error {
		serialNumbers, ok := value.([]string)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		kindBoxReq, gErr := v.repo.GetByID(ctx, kindBoxReqID)
		if gErr != nil {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		if len(serialNumbers) != 0 && len(serialNumbers) > int(kindBoxReq.CountAccepted) {
			return fmt.Errorf(errmsg.ErrorMsgInvalidSerialNumberRange)
		}

		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) checkKindBoxReqStatusForDelivering(ctx context.Context) validation.RuleFunc {
	return func(value interface{}) error {
		kindboxreqID, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		kindBoxReq, err := v.repo.GetByID(ctx, kindboxreqID)
		if err != nil {
			return err
		}
		if kindBoxReq.Status != entity.KindBoxReqAssignedSenderAgentStatus {
			return fmt.Errorf(errmsg.ErrorMsgDeliverKindBoxReqStatus)
		}

		return nil
	}
}
