import type { ValueObject } from '@core/shared/domain/ValueObject';
import type { EmailValidationResult } from '../types/EmailAddress';
import { isDisposableEmail, validateEmail } from '../types/EmailAddress';

export interface EmailAddressProps {
  value: string;
}

/**
 * Value Object: EmailAddress
 *
 * Wraps a validated, normalized email string and provides equality semantics.
 * Validation and helper utilities live in domain/types/EmailAddress.
 */
export class EmailAddress implements ValueObject<EmailAddressProps> {
  public readonly props: EmailAddressProps;

  private constructor(value: string) {
    this.props = { value };
  }

  static create(raw: string): EmailAddress {
    const result: EmailValidationResult = validateEmail(raw);
    if (!result.success) {
      throw new Error(result.error);
    }
    return new EmailAddress(result.email);
  }

  static fromValidated(value: string): EmailAddress {
    return new EmailAddress(value);
  }

  get value(): string {
    return this.props.value;
  }

  equals(other: ValueObject<EmailAddressProps>): boolean {
    return this.props.value === other.props.value;
  }

  isDisposable(): boolean {
    return isDisposableEmail(this.props.value);
  }
}

export { isDisposableEmail, validateEmail } from '../types/EmailAddress';
export type { EmailValidationResult } from '../types/EmailAddress';

