import { Inject, Injectable } from '@nestjs/common';
import type { Pool } from 'pg';
import type Redis from 'ioredis';
import {
  POSTGRES_POOL,
  REDIS_CLIENT,
} from '../../../../libs/infrastructure/src';

@Injectable()
export class ReadinessService {
  constructor(
    @Inject(POSTGRES_POOL) private readonly postgresPool: Pool,
    @Inject(REDIS_CLIENT) private readonly redis: Redis,
  ) {}

  async check(): Promise<{ status: 'ok' }> {
    await this.postgresPool.query('SELECT 1');
    const pong = await this.redis.ping();
    if (pong !== 'PONG') throw new Error('Redis ping failed');
    return { status: 'ok' as const };
  }
}
