import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { Injectable } from '@nestjs/common';
import { RecordHeartbeatCommand } from './record-heartbeat.command';
import { PresenceRedisRepository } from '../../../infrastructure/redis/presence-redis.repository';

export interface RecordHeartbeatResult {
  ok: boolean;
  serverTs: number;
}

@Injectable()
@CommandHandler(RecordHeartbeatCommand)
export class RecordHeartbeatHandler implements ICommandHandler<RecordHeartbeatCommand> {
  constructor(private readonly presenceRedisRepository: PresenceRedisRepository) {}

  async execute(command: RecordHeartbeatCommand): Promise<RecordHeartbeatResult> {
    const now = Math.floor(Date.now() / 1000);
    await this.presenceRedisRepository.updateUserPresence(command.userId, now);
    return { ok: true, serverTs: now };
  }
}
