import {
  Controller,
  Get,
  Query,
  UseGuards,
  BadRequestException,
} from '@nestjs/common';
import { UserService, UserProfileResult } from '@/application/services';
import { JwtAuthGuard } from '@/shared/guards/jwt-auth.guard';
import { CurrentUser } from '@/shared/decorators/current-user.decorator';

@Controller('auth/user')
@UseGuards(JwtAuthGuard)
export class UserController {
  constructor(private readonly userService: UserService) {}

  /**
   * 获取当前用户信息
   * GET /user/profile
   */
  @Get('profile')
  async getProfile(
    @CurrentUser() user: { accountSequence: string },
  ): Promise<{ success: boolean; data: UserProfileResult }> {
    const result = await this.userService.getProfile(user.accountSequence);
    return { success: true, data: result };
  }

  /**
   * 根据手机号查找用户（用于P2P转账验证）
   * GET /user/lookup?phone=13800138000
   */
  @Get('lookup')
  async lookupByPhone(
    @Query('phone') phone: string,
    @CurrentUser() currentUser: { accountSequence: string },
  ): Promise<{ success: boolean; data: { exists: boolean; nickname?: string; accountSequence?: string } }> {
    if (!phone || phone.length !== 11) {
      throw new BadRequestException('请输入有效的11位手机号');
    }

    const result = await this.userService.lookupByPhone(phone);
    return { success: true, data: result };
  }
}
