import type { ValueObject } from '@core/shared/domain/ValueObject';
import bcrypt from 'bcrypt';

export interface PasswordHashProps {
  value: string;
}

/**
 * Value Object: PasswordHash
 *
 * Wraps a bcrypt-hashed password string and provides verification.
 */
export class PasswordHash implements ValueObject<PasswordHashProps> {
  public readonly props: PasswordHashProps;

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

  static async create(plain: string): Promise<PasswordHash> {
    const saltRounds = 12;
    const hash = await bcrypt.hash(plain, saltRounds);
    return new PasswordHash(hash);
  }

  static fromHash(hash: string): PasswordHash {
    return new PasswordHash(hash);
  }

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

  async verify(plain: string): Promise<boolean> {
    return bcrypt.compare(plain, this.props.value);
  }

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