import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { OnlineSnapshot } from '../../domain/entities/online-snapshot.entity';
import { IOnlineSnapshotRepository } from '../../domain/repositories/online-snapshot.repository.interface';

@Injectable()
export class OnlineSnapshotRepository implements IOnlineSnapshotRepository {
  constructor(
    @InjectRepository(OnlineSnapshot) private readonly repo: Repository<OnlineSnapshot>,
  ) {}

  create(data: Partial<OnlineSnapshot>): OnlineSnapshot {
    return this.repo.create(data);
  }

  async save(entity: OnlineSnapshot): Promise<OnlineSnapshot> {
    return this.repo.save(entity);
  }

  async findByTimeRange(startTime: Date, endTime: Date): Promise<OnlineSnapshot[]> {
    return this.repo
      .createQueryBuilder('s')
      .where('s.ts >= :startTime AND s.ts <= :endTime', { startTime, endTime })
      .orderBy('s.ts', 'ASC')
      .getMany();
  }
}
