package de.effigenix.shared.common;

/**
 * Value Object representing contact information.
 * All fields are optional, but email format is validated when present.
 * Immutable.
 */
public record ContactInfo(
        String phone,
        String email,
        String contactPerson
) {

    public ContactInfo {
        if (email != null && !email.isBlank() && !isValidEmail(email)) {
            throw new IllegalArgumentException("Invalid email format: " + email);
        }
    }

    public static Result<String, ContactInfo> create(String phone, String email, String contactPerson) {
        try {
            return Result.success(new ContactInfo(phone, email, contactPerson));
        } catch (IllegalArgumentException e) {
            return Result.failure(e.getMessage());
        }
    }

    private static boolean isValidEmail(String email) {
        return email.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$");
    }
}
