import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { AppStatusResponseDto } from './business/shared';

const packageJson: { version: string } = require('../package.json');

/**
 * 应用服务类
 *
 * 功能描述：
 * - 提供应用基础服务
 * - 返回应用运行状态信息
 *
 * @author angjustinl
 * @version 1.0.0
 * @since 2025-12-17
 */
@Injectable()
export class AppService {
  private readonly startTime: number;

  constructor(private readonly configService: ConfigService) {
    this.startTime = Date.now();
  }

  /**
   * 获取应用状态
   *
   * @returns 应用状态信息
   */
  getStatus(): AppStatusResponseDto {
    const isDatabaseConfigured = this.isDatabaseConfigured();

    return {
      service: 'Pixel Game Server',
      version: packageJson.version,
      status: 'running',
      timestamp: new Date().toISOString(),
      uptime: Math.floor((Date.now() - this.startTime) / 1000),
      environment: this.configService.get<string>('NODE_ENV', 'development'),
      storageMode: isDatabaseConfigured ? 'database' : 'memory'
    };
  }

  /**
   * 检查数据库配置是否完整
   *
   * @returns 是否配置了数据库
   */
  private isDatabaseConfigured(): boolean {
    const requiredEnvVars = ['DB_HOST', 'DB_PORT', 'DB_USERNAME', 'DB_PASSWORD', 'DB_NAME'];
    return requiredEnvVars.every(varName => this.configService.get<string>(varName));
  }
}
