/**
 * License Status Value Object
 *
 * Represents the status of a user license within a subscription.
 */

export type LicenseStatusType = 'ACTIVE' | 'REVOKED';

export class LicenseStatus {
  private constructor(private readonly status: LicenseStatusType) {}

  static create(status: LicenseStatusType): LicenseStatus {
    if (status !== 'ACTIVE' && status !== 'REVOKED') {
      throw new Error(`Invalid license status: ${status}`);
    }
    return new LicenseStatus(status);
  }

  static fromString(value: string): LicenseStatus {
    const upperValue = value.toUpperCase() as LicenseStatusType;
    if (upperValue !== 'ACTIVE' && upperValue !== 'REVOKED') {
      throw new Error(`Invalid license status: ${value}`);
    }
    return new LicenseStatus(upperValue);
  }

  static active(): LicenseStatus {
    return new LicenseStatus('ACTIVE');
  }

  static revoked(): LicenseStatus {
    return new LicenseStatus('REVOKED');
  }

  get value(): LicenseStatusType {
    return this.status;
  }

  isActive(): boolean {
    return this.status === 'ACTIVE';
  }

  isRevoked(): boolean {
    return this.status === 'REVOKED';
  }

  /**
   * Revoke this license, returning a new revoked status
   */
  revoke(): LicenseStatus {
    if (this.status === 'REVOKED') {
      throw new Error('License is already revoked');
    }
    return LicenseStatus.revoked();
  }

  /**
   * Reactivate this license, returning a new active status
   */
  reactivate(): LicenseStatus {
    if (this.status === 'ACTIVE') {
      throw new Error('License is already active');
    }
    return LicenseStatus.active();
  }

  equals(other: LicenseStatus): boolean {
    return this.status === other.status;
  }

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