import { Controller, Get } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { PrismaService } from '../../infrastructure/database/prisma.service';
import { RedisService } from '../../infrastructure/redis/redis.service';
import { Public } from '../../common/decorators/public.decorator';

@ApiTags('health')
@Controller()
export class SystemController {
  constructor(
    private readonly prisma: PrismaService,
    private readonly redis: RedisService,
  ) {}

  @Public()
  @Get()
  @ApiOperation({ summary: '服务健康检查' })
  @ApiResponse({ status: 200, description: 'API 运行正常' })
  getRoot() {
    return {
      status: 'ok',
      timestamp: new Date().toISOString(),
      uptime: process.uptime(),
    };
  }

  @Public()
  @Get('health')
  @ApiOperation({ summary: '详细健康检查' })
  @ApiResponse({ status: 200, description: '服务状态信息' })
  async getHealth() {
    let dbStatus = 'disconnected';
    let redisStatus = 'disconnected';
    try {
      await this.prisma.$queryRaw`SELECT 1`;
      dbStatus = 'connected';
    } catch {}
    try {
      redisStatus = this.redis.isHealthy() ? 'connected' : 'disconnected';
    } catch {}
    return { status: 'ok', database: dbStatus, redis: redisStatus };
  }

  @Get('secure-test')
  @ApiBearerAuth()
  @ApiOperation({ summary: '认证测试端点' })
  @ApiResponse({ status: 200, description: '已认证' })
  @ApiResponse({ status: 401, description: '未认证' })
  secureTest() {
    return { message: '你已通过认证' };
  }
}
