import {
  Body,
  Controller,
  Get,
  Param,
  Post,
  Query,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { Throttle } from '@nestjs/throttler';
import { CurrentUser } from '../common/decorators/current-user.decorator';
import type { JwtUserPayload } from '../auth/jwt.types';
import { GpsService } from './gps.service';
import { GpsBatchDto } from './dto/gps.dto';

@ApiTags('gps')
@ApiBearerAuth('access-token')
@Controller('trips/:tripId/gps')
export class GpsController {
  constructor(private readonly gps: GpsService) {}

  @Post('batch')
  @Throttle({ default: { limit: 60, ttl: 60000 } })
  batch(
    @Param('tripId') tripId: string,
    @CurrentUser() actor: JwtUserPayload,
    @Body() dto: GpsBatchDto,
  ) {
    return this.gps.ingestBatch(tripId, actor, dto);
  }

  @Get()
  route(
    @Param('tripId') tripId: string,
    @Query('from') from?: string,
    @Query('to') to?: string,
    @Query('cursor') cursor?: string,
    @Query('limit') limit?: string,
  ) {
    return this.gps.listRoute(tripId, {
      from,
      to,
      cursor,
      limit: limit ? Number(limit) : undefined,
    });
  }
}
