import { Controller, Param, UseGuards, Req, Query, Body, Post, Get, Patch } from '@nestjs/common';
import { RouteService } from './route.service';
import { ApiBearerAuth, ApiBody, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger';
import { LANGUAGES } from 'src/shared/enum/languages.enum';
import { AuthUserGuard } from 'src/auth/user/authUser.guard';
import { CustomUserRequest } from 'src/auth/user/interface/customUserReq';
import { CreateRouteReq } from './dto/create-route-req';
import { RouteStatus } from './entities/route.entity';
import { UpdateRouteStatusDto } from './dto/updateRouteStatus.dto';

@Controller('route')
@ApiTags('route')
export class RouteController {
  constructor(private readonly routeService: RouteService) {}

  @Get('/info/:idRoute')
  @ApiParam({ name: 'idRoute', type: Number })
  @ApiBearerAuth('jwt')
  // @UseGuards(AuthUserGuard)
  async getRouteInfo(@Param('idRoute') idRoute: number) {
    return await this.routeService.getRouteInfoById(idRoute);
  }

  @Post('/:idTown')
  @ApiParam({ name: 'idTown', type: Number })
  @ApiBody({ type: CreateRouteReq })
  @ApiBearerAuth('jwt')
  @UseGuards(AuthUserGuard)
  async recommendRoute(
    @Req() req: CustomUserRequest,
    @Query('lang') lang: string,
    @Param('idTown') idTown: number,
    @Body() createRouteReq: CreateRouteReq,
  ) {
    try {
      const { email } = req.user;
      const res = await this.routeService.recommend(
        idTown,
        email,
        lang as LANGUAGES,
        createRouteReq.start,
        createRouteReq.end,
      );
      console.log('HELOOO: ', res);
      return res;
    } catch (error) {
      console.log(error);
      return error;
    }
  }

  @Get('')
  @ApiQuery({ name: 'routeStatus', type: String, schema: { enum: Object.values(RouteStatus) } })
  @ApiBearerAuth('jwt')
  @UseGuards(AuthUserGuard)
  async getRecommendRoute(@Req() req: CustomUserRequest, @Query('routeStatus') routeStatus: RouteStatus) {
    try {
      const { email } = req.user;
      return await this.routeService.getRouteAndPlacesByUser(email, routeStatus);
    } catch (error) {
      console.log(error);
      return error;
    }
  }

  @Get('/:idRoute/:lang')
  @ApiParam({ name: 'idRoute', type: Number })
  @ApiParam({ name: 'lang', type: String, enum: Object.values(LANGUAGES) })
  @ApiBearerAuth('jwt')
  @UseGuards(AuthUserGuard)
  async getRoute(@Param('idRoute') idRoute: number, @Param('lang') lang: LANGUAGES, @Req() req: CustomUserRequest) {
    const { email } = req.user;

    return await this.routeService.getRouteById(idRoute, email, lang);
  }

  @Patch('/:idRoute')
  @ApiParam({ name: 'idRoute', type: Number })
  @ApiBody({ type: UpdateRouteStatusDto, description: 'accepted, pending' })
  @ApiBearerAuth('jwt')
  @UseGuards(AuthUserGuard)
  async updateRoute(@Body() updateRouteStatusDto: UpdateRouteStatusDto, @Param('idRoute') idRoute: number) {
    return await this.routeService.updateRoute(idRoute, updateRouteStatusDto.status);
  }
}
