package user

import (
	"errors"

	orm "tm/pkg/mongo"
)

var ErrUserNotFound = errors.New("user not found")

func mapUserWriteError(err error) error {
	if err == nil || !orm.IsDuplicateKeyError(err) {
		return err
	}
	switch {
	case orm.DuplicateKeyMatchesField(err, "phone"):
		return errors.New("phone number already exists")
	case orm.DuplicateKeyMatchesField(err, "email"):
		return errors.New("email already exists")
	case orm.DuplicateKeyMatchesField(err, "username"):
		return errors.New("username already exists")
	default:
		return err
	}
}

func isUserConflictError(err error) bool {
	if err == nil {
		return false
	}
	switch err.Error() {
	case "phone number already exists", "email already exists", "username already exists":
		return true
	default:
		return false
	}
}
