package adminvalidator

import (
	"context"
	"fmt"

	"git.gocasts.ir/ebhomengo/niki/entity"
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
)

const (
	phoneNumberRegex   = "^09\\d{9}$"
	minLengthFirstName = 3
	maxLengthFirstName = 40
	minLengthLastName  = 3
	maxLengthLastName  = 40
	minLengthPassword  = 8
	maxLengthPassword  = 32
)

type Repository interface {
	AdminExistByPhoneNumber(ctx context.Context, phoneNumber string) (bool, error)
	AdminExistByEmail(ctx context.Context, email string) (bool, error)
}
type Validator struct {
	repo Repository
}

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

func (v Validator) doesAdminExistByPhoneNumber(value interface{}) error {
	phoneNumber, ok := value.(string)
	if !ok {
		return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
	}
	adminExisted, err := v.repo.AdminExistByPhoneNumber(context.Background(), phoneNumber)
	if err != nil {
		return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
	}
	if !adminExisted {
		return fmt.Errorf(errmsg.ErrorMsgPhoneNumberOrPassIsIncorrect)
	}

	return nil
}

func (v Validator) IsPhoneNumberUnique(value interface{}) error {
	phoneNumber, ok := value.(*string)
	if !ok {
		return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
	}
	adminExisted, err := v.repo.AdminExistByPhoneNumber(context.Background(), *phoneNumber)
	if err != nil {
		return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
	}
	if adminExisted {
		return fmt.Errorf(errmsg.ErrorMsgPhoneNumberIsNotUnique)
	}

	return nil
}

func (v Validator) doesAdminExistByEmail(value interface{}) error {
	email, ok := value.(*string)
	if !ok {
		return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
	}
	adminExisted, err := v.repo.AdminExistByEmail(context.Background(), *email)
	if err != nil {
		return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
	}
	if adminExisted {
		return fmt.Errorf(errmsg.ErrorMsgEmailIsNotUnique)
	}

	return nil
}

func (v Validator) IsRoleValid(value interface{}) error {
	role, ok := value.(*entity.AdminRole)
	if !ok {
		return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
	}

	if isValid := role.IsValid(); !isValid {
		return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
	}

	return nil
}

func (v Validator) IsGenderValid(value interface{}) error {
	gender, ok := value.(*entity.Gender)
	if gender == nil {
		return nil
	}
	if !ok {
		return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
	}

	if isValid := gender.IsValid(); !isValid {
		return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
	}

	return nil
}

func (v Validator) IsStatusValid(value interface{}) error {
	status, ok := value.(*entity.AdminStatus)
	if !ok {
		return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
	}

	if isValid := status.IsValid(); !isValid {
		return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
	}

	return nil
}
