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

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

  constructor() {
    this.timeWindow = TimeWindow.default();
  }

  /**
   * 判断用户是否在线
   */
  isOnline(lastHeartbeatTs: number, now: Date = new Date()): boolean {
    const threshold = this.timeWindow.getThresholdTimestamp(now);
    return lastHeartbeatTs > threshold;
  }

  /**
   * 获取在线判定阈值时间戳
   */
  getOnlineThreshold(now: Date = new Date()): number {
    return this.timeWindow.getThresholdTimestamp(now);
  }

  /**
   * 获取窗口秒数
   */
  getWindowSeconds(): number {
    return this.timeWindow.windowSeconds;
  }
}
