package customer

import (
	"errors"

	orm "tm/pkg/mongo"
)

var ErrCustomerNotFound = errors.New("customer not found")

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

func isCustomerConflictError(err error) bool {
	if err == nil {
		return false
	}
	switch err.Error() {
	case "customer with this phone number already exists",
		"customer with this email already exists",
		"customer with this username already exists",
		"company with this name already exists",
		"company with this registration number already exists",
		"company with this tax ID already exists":
		return true
	default:
		return false
	}
}
