package agentkindboxvalidator

import (
	"context"
	"fmt"

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

type Repository interface {
	KindBoxExistForAgent(ctx context.Context, kindBoxID, agentID uint) (bool, error)
}

type Validator struct {
	repo Repository
}

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

func (v Validator) doesKindBoxExistForAgent(ctx context.Context, agentID uint) validation.RuleFunc {
	return func(value interface{}) error {
		kindBoxID, ok := value.(uint)
		if !ok {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		exists, err := v.repo.KindBoxExistForAgent(ctx, kindBoxID, agentID)
		if err != nil {
			return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
		}
		if !exists {
			return fmt.Errorf(errmsg.ErrorMsgNotFound)
		}

		return nil
	}
}