import {
  Body,
  Controller,
  Get,
  Param,
  Post,
  UseGuards,
} from '@nestjs/common';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { Roles } from '../../common/decorators/roles.decorator';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { RolesGuard } from '../../common/guards/roles.guard';
import type { AuthUser } from '../../common/types/auth-user.type';
import { CreateGpsPositionDto } from './dto/create-gps-position.dto';
import { GpsService } from './gps.service';

@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('super_admin', 'agency_manager', 'agent', 'driver')
@Controller('gps')
export class GpsController {
  constructor(private readonly gpsService: GpsService) {}

  @Post('positions')
  create(
    @Body() data: CreateGpsPositionDto,
    @CurrentUser() user: AuthUser,
  ) {
    return this.gpsService.createForUser(data, user);
  }

  @Get('positions')
  findAll(@CurrentUser() user: AuthUser) {
    return this.gpsService.findAllForUser(user);
  }

  @Get('vehicles/:vehicleId/latest')
  getLatestByVehicle(
    @Param('vehicleId') vehicleId: string,
    @CurrentUser() user: AuthUser,
  ) {
    return this.gpsService.getLatestByVehicleForUser(
      Number(vehicleId),
      user,
    );
  }

  @Get('vehicles/:vehicleId/history')
  getHistoryByVehicle(
    @Param('vehicleId') vehicleId: string,
    @CurrentUser() user: AuthUser,
  ) {
    return this.gpsService.getHistoryByVehicleForUser(
      Number(vehicleId),
      user,
    );
  }

  @Get('live-map')
  getLiveMap(@CurrentUser() user: AuthUser) {
    return this.gpsService.getLiveMapForUser(user);
  }
}