import { Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
import { TenantAwareRepository } from '@it0/database';
import { HealthCheckResult } from '../../domain/entities/health-check-result.entity';

@Injectable()
export class HealthCheckResultRepository extends TenantAwareRepository<HealthCheckResult> {
  constructor(dataSource: DataSource) {
    super(dataSource, HealthCheckResult);
  }

  async findByServerId(serverId: string): Promise<HealthCheckResult[]> {
    return this.withRepository((repo) =>
      repo.find({ where: { serverId } as any, order: { checkedAt: 'DESC' } as any }),
    );
  }

  async findRecent(limit = 50): Promise<HealthCheckResult[]> {
    return this.withRepository((repo) =>
      repo.find({ order: { checkedAt: 'DESC' } as any, take: limit }),
    );
  }
}
