import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { DailyActiveStats } from '../../domain/entities/daily-active-stats.entity';
import { IDailyActiveStatsRepository } from '../../domain/repositories/daily-active-stats.repository.interface';

@Injectable()
export class DailyActiveStatsRepository implements IDailyActiveStatsRepository {
  constructor(
    @InjectRepository(DailyActiveStats) private readonly repo: Repository<DailyActiveStats>,
  ) {}

  async findByDateRange(startDate: string, endDate: string): Promise<DailyActiveStats[]> {
    return this.repo
      .createQueryBuilder('d')
      .where('d.day >= :startDate AND d.day <= :endDate', { startDate, endDate })
      .orderBy('d.day', 'ASC')
      .getMany();
  }

  async upsert(data: Partial<DailyActiveStats>): Promise<void> {
    await this.repo.upsert(data as any, ['day']);
  }
}
