import { DomainException } from '../../shared/exceptions/domain.exception';

/**
 * 构建号值对象
 * 格式: 自由格式字符串，通常为日期+序号 (e.g., 20250101.1)
 */
export class BuildNumber {
  private readonly _value: string;

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

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

  static create(value: string): BuildNumber {
    if (!value || value.trim() === '') {
      throw new DomainException('BuildNumber cannot be empty');
    }

    const trimmed = value.trim();
    if (trimmed.length > 64) {
      throw new DomainException('BuildNumber cannot exceed 64 characters');
    }

    // 允许字母、数字、点、下划线、横线
    if (!/^[a-zA-Z0-9._-]+$/.test(trimmed)) {
      throw new DomainException('BuildNumber can only contain alphanumeric characters, dots, underscores, and hyphens');
    }

    return new BuildNumber(trimmed);
  }

  equals(other: BuildNumber): boolean {
    return this._value === other._value;
  }

  toString(): string {
    return this._value;
  }
}
