package adminkindboxreqvalidator

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

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

const (
	MinKindBoxReq        = 1
	MaxKindBoxReq        = 100
	MaxLengthQuerySearch = 32
)

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

type AdminSvc interface {
	AdminExistByID(ctx context.Context, req param.AdminExistByIDRequest) (param.AdminExistByIDResponse, error)
}

type AgentSvc interface {
	AgentExistByID(ctx context.Context, req agentparam.AdminAgentExistByIDRequest) (agentparam.AdminAgentExistByIDResponse, error)
}

type ReferTimeSvc interface {
	GetReferTimeByID(ctx context.Context, req refertimeparam.GetReferTimeRequest) (refertimeparam.GetReferTimeResponse, error)
}

type AddressSvc interface {
	GetAddressByID(ctx context.Context, req addressparam.AddressGetRequest) (addressparam.AddressGetResponse, error)
}

type BenefactorSvc interface {
	BenefactorExistByID(ctx context.Context, req benefactorparam.BenefactorExistByIDRequest) (benefactorparam.BenefactorExistByIDResponse, error)
}

type Validator struct {
	repo          Repository
	adminSvc      AdminSvc
	agentSvc      AgentSvc
	benefactorSvc BenefactorSvc
	referTimeSvc  ReferTimeSvc
	addressSvc    AddressSvc
}

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

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

		return nil
	}
}

func (v Validator) CheckKindBoxReqStatusForRejecting(ctx context.Context) validation.RuleFunc {
	return func(value interface{}) error {
		kindboxreqID, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		kindBox, err := v.repo.GetByID(ctx, kindboxreqID)
		if err != nil {
			return err
		}
		if kindBox.Status != entity.KindBoxReqPendingStatus {
			return fmt.Errorf(errmsg.ErrorMsgRejectKindBoxReqStatus)
		}

		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
	}
}

func (v Validator) checkKindBoxReqStatusForAssigningSenderAgent(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.KindBoxReqAcceptedStatus {
			return fmt.Errorf(errmsg.ErrorMsgAssignSenderAgentKindBoxReqStatus)
		}

		return nil
	}
}

func (v Validator) checkCountAcceptedMustBeLessThanCountRequested(ctx context.Context, kindboxreqID uint) validation.RuleFunc {
	return func(value interface{}) error {
		countAccepted, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		kindBoxReq, err := v.repo.GetByID(ctx, kindboxreqID)
		if err != nil {
			return err
		}
		if kindBoxReq.CountRequested < countAccepted {
			return fmt.Errorf(errmsg.ErrorMsgCountAcceptedOverflow)
		}

		return nil
	}
}

func (v Validator) doesAgentAdminExist(ctx context.Context) validation.RuleFunc {
	return func(value interface{}) error {
		adminID, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		resp, err := v.adminSvc.AdminExistByID(ctx, param.AdminExistByIDRequest{
			AdminID: adminID,
		})
		if err != nil {
			return err
		}
		if resp.Admin.Role != entity.AdminAgentRole {
			return fmt.Errorf(errmsg.ErrorMsgAdminIsNotAgent)
		}

		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) 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.Data.Status != entity.ReferTimeActiveStatus {
			return fmt.Errorf(errmsg.ErrorMsgReferTimeIsNotActive)
		}

		return nil
	}
}

func (v Validator) doesAgentExist(ctx context.Context) validation.RuleFunc {
	return func(value interface{}) error {
		agentID, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		resp, err := v.agentSvc.AgentExistByID(ctx, agentparam.AdminAgentExistByIDRequest{AgentID: agentID})
		if err != nil {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		if !resp.Exist {
			return fmt.Errorf(errmsg.ErrorMsgNotFound)
		}

		return nil
	}
}

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

		kindBoxReq, err := v.repo.GetByID(ctx, kindBoxReqID)
		if err != nil {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}

		address, gErr := v.addressSvc.GetAddressByID(ctx, addressparam.AddressGetRequest{AddressID: addressID})
		if gErr != nil {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		if address.Data.BenefactorID != kindBoxReq.BenefactorID {
			return fmt.Errorf(errmsg.ErrorMsgNotFound)
		}

		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) 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) 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) AreSearchValid() validation.RuleFunc {
	return func(value interface{}) error {
		search, ok := value.(params.SearchRequest)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		if len(search.Query) > MaxLengthQuerySearch {
			return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
		}

		return nil
	}
}
