/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.model;

import static de.srsoftware.tools.Optionals.allSet;
import static de.srsoftware.umbrella.core.Errors.INVALID_EMAIL;
import static de.srsoftware.umbrella.core.constants.Field.EMAIL;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.badRequest;

import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.util.Objects;

public class EmailAddress {
	private final String email;

	public EmailAddress(String addr) throws UmbrellaException {
		var parts = addr.split("@");
		if (parts.length != 2 || !allSet(parts[0],parts[1])) throw badRequest(INVALID_EMAIL, EMAIL,addr);
		email = addr;
	}

	@Override
	public boolean equals(Object obj) {
		return obj instanceof EmailAddress other && Objects.equals(email,other.email);
	}

	@Override
	public int hashCode() {
		return email.hashCode();
	}

	@Override
	public String toString() {
		return email;
	}
}
