/**
 * Value Object: VersionName
 *
 * Encapsulates semantic version string validation (e.g., "1.2.3").
 * Supports standard semver format: MAJOR.MINOR.PATCH with optional
 * pre-release suffix (e.g., "1.2.3-beta.1").
 */
export class VersionName {
  private static readonly SEMVER_REGEX =
    /^\d+\.\d+\.\d+(-[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*)?$/;

  private readonly value: string;

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

  static create(value: string): VersionName {
    if (!value || value.length > 32) {
      throw new Error(
        `Invalid version name: "${value}". Must be non-empty and at most 32 characters.`,
      );
    }
    if (!VersionName.SEMVER_REGEX.test(value)) {
      throw new Error(
        `Invalid version name: "${value}". Must follow semantic versioning (e.g., 1.2.3).`,
      );
    }
    return new VersionName(value);
  }

  getValue(): string {
    return this.value;
  }

  getMajor(): number {
    return parseInt(this.value.split('.')[0], 10);
  }

  getMinor(): number {
    return parseInt(this.value.split('.')[1], 10);
  }

  getPatch(): number {
    return parseInt(this.value.split('.')[2].split('-')[0], 10);
  }

  equals(other: VersionName): boolean {
    return this.value === other.value;
  }

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