import { Controller, Get } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { ChainConfigService } from '@/domain/services/chain-config.service';
import { ChainType } from '@/domain/value-objects';

@ApiTags('Health')
@Controller('health')
export class HealthController {
  constructor(private readonly chainConfig: ChainConfigService) {}

  @Get()
  @ApiOperation({ summary: '健康检查' })
  @ApiResponse({ status: 200, description: '服务健康' })
  check() {
    return {
      status: 'ok',
      service: 'blockchain-service',
      timestamp: new Date().toISOString(),
    };
  }

  @Get('ready')
  @ApiOperation({ summary: '就绪检查' })
  @ApiResponse({ status: 200, description: '服务就绪' })
  ready() {
    return {
      status: 'ready',
      service: 'blockchain-service',
      timestamp: new Date().toISOString(),
    };
  }

  @Get('network')
  @ApiOperation({ summary: '网络配置信息' })
  @ApiResponse({ status: 200, description: '返回当前网络配置' })
  network() {
    const supportedChains = this.chainConfig.getSupportedChains();
    const chains: Record<string, unknown> = {};

    for (const chainTypeEnum of supportedChains) {
      const chainType = ChainType.fromEnum(chainTypeEnum);
      const config = this.chainConfig.getConfig(chainType);
      chains[chainTypeEnum] = {
        chainId: config.chainId,
        rpcUrl: config.rpcUrl,
        usdtContract: config.usdtContract,
        isTestnet: config.isTestnet,
      };
    }

    return {
      service: 'blockchain-service',
      networkMode: this.chainConfig.getNetworkMode(),
      isTestnet: this.chainConfig.isTestnetMode(),
      chains,
      timestamp: new Date().toISOString(),
    };
  }
}
