import { Injectable } from '@nestjs/common';
import { TimeWindow } from '../value-objects/time-window.vo';

@Injectable()
export class OnlineDetectionService {
  private readonly timeWindow: TimeWindow;

  constructor() {
    const windowSeconds = parseInt(process.env.PRESENCE_WINDOW_SECONDS || '300', 10);
    this.timeWindow = TimeWindow.ofSeconds(windowSeconds);
  }

  isOnline(lastHeartbeatTs: number, now: Date = new Date()): boolean {
    return lastHeartbeatTs > this.timeWindow.getThresholdTimestamp(now);
  }

  getOnlineThreshold(now: Date = new Date()): number {
    return this.timeWindow.getThresholdTimestamp(now);
  }

  getWindowSeconds(): number {
    return this.timeWindow.windowSeconds;
  }
}
