import { Controller, Get, Query } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiQuery } from '@nestjs/swagger';
import { LocationService } from './location.service';

@ApiTags('位置服务')
@Controller('app/location')
export class LocationController {
  constructor(private readonly locationService: LocationService) {}

  @Get('geocode')
  @ApiOperation({ summary: '逆地理编码：根据经纬度获取城市名' })
  @ApiQuery({ name: 'latitude', description: '纬度', example: 31.230416 })
  @ApiQuery({ name: 'longitude', description: '经度', example: 121.473701 })
  async reverseGeocode(
    @Query('latitude') latitude: string,
    @Query('longitude') longitude: string,
  ) {
    const lat = parseFloat(latitude);
    const lng = parseFloat(longitude);

    if (isNaN(lat) || isNaN(lng)) {
      return { city: '上海', province: '上海市' };
    }

    return this.locationService.reverseGeocode(lat, lng);
  }
}
