import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Query,
} from "@nestjs/common"
import { RatingService } from "./rating.service"
import { CreateRatingDto } from "../dto/create-rating.dto"
import { UpdateRatingDto } from "../dto/update-rating.dto"

import { ApiBearerAuth, ApiTags } from "@nestjs/swagger"
import { AuthGuardMiddleware } from "src/middleware/auth-guard.middleware"
import { GetRatingsDto } from "../dto/get-ratings.dto"
import { CustomerAuthGuardMiddleware } from "src/middleware/customer-auth-guard.middleware"
import { CreateNoRatingDto } from "../dto/create-no-rating.dto"

@ApiTags("Rating")
@Controller("rating")
export class RatingController {
  constructor(private readonly ratingService: RatingService) {}

  @UseGuards(CustomerAuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @Get("customer-tags")
  getCustomerRatingTags() {
    return this.ratingService.getCustomerRatingTags()
  }

  @UseGuards(AuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @Get("driver-tags")
  getDriverRatingTags() {
    return this.ratingService.getDriverRatingTags()
  }

  @UseGuards(AuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @Post()
  create(@Body() createRatingDto: CreateRatingDto) {
    return this.ratingService.create(createRatingDto, false)
  }

  @UseGuards(CustomerAuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @Post("customer")
  createCustomerRating(@Body() createRatingDto: CreateRatingDto) {
    return this.ratingService.create(createRatingDto, true)
  }

  @Get()
  findAll(@Query() queryParams: GetRatingsDto) {
    return this.ratingService.findAll(queryParams)
  }

  @UseGuards(AuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @Patch(":id")
  update(@Param("id") id: string, @Body() updateRatingDto: UpdateRatingDto) {
    return this.ratingService.update(+id, updateRatingDto)
  }

  @UseGuards(AuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @Delete(":id")
  remove(@Param("id") id: string) {
    return this.ratingService.remove(+id)
  }

  @UseGuards(CustomerAuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @Post("no-rating")
  setNoRating(@Body() createNoRatingDto: CreateNoRatingDto) {
    return this.ratingService.setNoRating(createNoRatingDto)
  }

  @UseGuards(AuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @Post("driver/no-rating")
  setDriverNoRating(@Body() createNoRatingDto: CreateNoRatingDto) {
    return this.ratingService.setNoRating(createNoRatingDto)
  }

  // Get driver's last trip with rating status
  @UseGuards(AuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @Get("driver/last-trip")
  getDriverLastTripWithRatingStatus(@Query("driver_id") driverId: string) {
    return this.ratingService.getDriverLastTripWithRatingStatus(+driverId)
  }

  // Get customer's last trip with rating status
  @UseGuards(CustomerAuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @Get("customer/last-trip")
  getCustomerLastTripWithRatingStatus(
    @Query("customer_id") customerId: string,
  ) {
    return this.ratingService.getCustomerLastTripWithRatingStatus(+customerId)
  }
}
