/**
 * Bandwidth tracker — monitors usage and enforces limits
 */

export interface BandwidthStats {
  bytesProxied: number;
  requestsProxied: number;
  bytesLimitHard: number;
  limitReached: boolean;
}

export class BandwidthTracker {
  private bytesProxied = 0;
  private requestsProxied = 0;
  private readonly bytesLimit: number;

  constructor(maxGb: number) {
    this.bytesLimit = maxGb * 1024 * 1024 * 1024;
  }

  addBytes(n: number): void {
    this.bytesProxied += n;
  }

  incrementRequests(): void {
    this.requestsProxied++;
  }

  isLimitReached(): boolean {
    return this.bytesProxied >= this.bytesLimit;
  }

  getStats(): BandwidthStats {
    return {
      bytesProxied: this.bytesProxied,
      requestsProxied: this.requestsProxied,
      bytesLimitHard: this.bytesLimit,
      limitReached: this.isLimitReached(),
    };
  }

  getUsedGb(): number {
    return this.bytesProxied / (1024 * 1024 * 1024);
  }

  reset(): void {
    this.bytesProxied = 0;
    this.requestsProxied = 0;
  }
}
