import type { IValueObject } from '@core/shared/domain';
import type { EmailValidationResult } from '../types/EmailAddress';
import { validateEmail, isDisposableEmail } 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 IValueObject<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: IValueObject<EmailAddressProps>): boolean {
    return this.props.value === other.props.value;
  }

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

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