import { Controller, Get, UseGuards } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { AdminServersService, type ServerInfo } from './admin-servers.service';
import { AdminAuthGuard } from '../../common/guards/admin-auth.guard';
import { AdminRolesGuard } from '../../common/guards/admin-roles.guard';
import { AdminRoles } from '../../common/decorators/admin-roles.decorator';
import type { AdminRole } from '../../common/types/admin-role.enum';

@ApiTags('admin-servers')
@Controller('admin-api/servers')
@UseGuards(AdminAuthGuard, AdminRolesGuard)
@ApiBearerAuth()
export class AdminServersController {
  constructor(private readonly serversService: AdminServersService) {}

  @Get('metrics')
  @AdminRoles('SUPER_ADMIN' as AdminRole)
  @ApiOperation({ summary: '服务器实时指标（仅超级管理员）' })
  async getMetrics(): Promise<{ servers: ServerInfo[] }> {
    return this.serversService.getAllMetrics();
  }

  @Get('health')
  @AdminRoles('SUPER_ADMIN' as AdminRole)
  @ApiOperation({ summary: '服务健康检查（Docker/MySQL/Redis/Qdrant/Gitea/Nginx/Worker）' })
  async getHealth() {
    return this.serversService.getHealthChecks();
  }

  @Get('workers')
  @AdminRoles('SUPER_ADMIN' as AdminRole)
  @ApiOperation({ summary: 'Worker 心跳状态（online/stale/offline）' })
  async getWorkers() {
    return this.serversService.getWorkerHeartbeats();
  }
}
