package service

import (
	"context"
	"github.com/go-ozzo/ozzo-validation/v4"
	"regexp"
)

func (a Authorization) validateStoreRole(req StoreRoleRequest) error {
	return validation.ValidateStruct(&req,
		validation.Field(&req.Title,
			validation.Required,
			validation.Length(4, 0),
			validation.Match(regexp.MustCompile(`^[a-zA-Z\s]+$`))),

		validation.Field(&req.TitleFa,
			validation.Required,
			validation.Length(4, 0),
			validation.Match(regexp.MustCompile(`^[\x{0600}-\x{06FF}\s]+$`))),
	)
}

func (a Authorization) validateUpdateRole(ctx context.Context, req UpdateRoleRequest) error {
	return validation.ValidateStruct(&req,
		validation.Field(&req.ID,
			validation.Required,
			validation.By(func(value interface{}) error {
				err := a.roleRepo.IsRoleExistsByID(ctx, req.ID)
				if err != nil {
					return err
				}
				return nil
			}),
		),
		validation.Field(&req.Title,
			validation.Required,
			validation.Length(4, 0),
			validation.Match(regexp.MustCompile(`^[a-zA-Z\s]+$`))),

		validation.Field(&req.TitleFa,
			validation.Required,
			validation.Length(4, 0),
			validation.Match(regexp.MustCompile(`^[\x{0600}-\x{06FF}\s]+$`))),
	)
}
