import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
import { ReviewService } from '../providers/review.service';
import { CreateReviewDto } from '../dto/create-review.dto';
import { UpdateReviewDto } from '../dto/update-review.dto';
import { FindReviewsDto, FindRestuarantReviewsDto } from '../dto/find-reviews.dto';
import { ChangeStatusDto } from '../dto/change-status.dto';
import {
  ApiTags,
  ApiOperation,
  ApiCreatedResponse,
  ApiOkResponse,
  ApiQuery,
  ApiBody,
  ApiParam,
  ApiBearerAuth,
  ApiHeader,
} from '@nestjs/swagger';
import { AuthGuard } from 'src/modules/auth/guards/auth.guard';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { UserId } from 'src/common/decorators/user-id.decorator';
import { RestId } from 'src/common/decorators/rest-id.decorator';
import { ReviewStatus } from '../enums/review-status.enum';
import { Permissions } from 'src/common/decorators/permissions.decorator';
import { Permission } from 'src/common/enums/permission.enum';
import { API_HEADER_SLUG } from 'src/common/constants';

@ApiTags('review')
@Controller()
export class ReviewController {
  constructor(private readonly reviewService: ReviewService) {}

  @UseGuards(AuthGuard)
  @ApiBearerAuth()
  @Post('public/reviews')
  @ApiOperation({ summary: 'Create a new review and rating' })
  @ApiHeader(API_HEADER_SLUG)
  @ApiCreatedResponse({ description: 'The review has been successfully created.' })
  @ApiBody({ type: CreateReviewDto })
  create(@Body() createReviewDto: CreateReviewDto, @UserId() userId: string) {
    return this.reviewService.create(userId, createReviewDto);
  }

  @Get('public/reviews/:foodId')
  @ApiOperation({ summary: 'Get all reviews of food by id(public - only approved)' })
  @ApiHeader(API_HEADER_SLUG)
  @ApiOkResponse({ description: 'List of approved reviews' })
  @ApiParam({ name: 'foodId', required: true, type: String })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiQuery({ name: 'orderBy', required: false, type: String })
  @ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] })
  findAll(@Query() dto: FindReviewsDto, @Param('foodId') foodId: string) {
    // Only show approved reviews for public endpoint
    return this.reviewService.findAll({ ...dto, status: ReviewStatus.APPROVED, foodId });
  }

  @Get('public/reviews/restuarant/:restuarantSlug')
  @ApiOperation({ summary: 'Get all reviews of restuarant by slug(public - only approved)' })
  @ApiHeader(API_HEADER_SLUG)
   @ApiParam({ name: 'restuarantSlug', required: true, type: String })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiQuery({ name: 'orderBy', required: false, type: String })
  @ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] })
  findRestuarantAllReviews(@Query() dto: FindRestuarantReviewsDto, @Param('restuarantSlug') restuarantSlug: string) {
    // Only show approved reviews for public endpoint
    return this.reviewService.findRestuarantAllReviews({ ...dto, status: ReviewStatus.APPROVED, restuarantSlug });
  }

  // @Get('public/reviews/:id')
  // @ApiOperation({ summary: 'Get a review by id' })
  // @ApiParam({ name: 'id', required: true })
  // @ApiOkResponse({ description: 'The review' })
  // @ApiNotFoundResponse({ description: 'Review not found' })
  // findById(@Param('id') id: string) {
  //   return this.reviewService.findById(id);
  // }

  @UseGuards(AuthGuard)
  @ApiBearerAuth()
  @Patch('public/reviews/:reviewId')
  @ApiOperation({ summary: 'Update a review (own reviews only)' })
  @ApiHeader(API_HEADER_SLUG)
  @ApiParam({ name: 'reviewId', required: true })
  @ApiBody({ type: UpdateReviewDto })
  @ApiOkResponse({ description: 'The updated review' })
  update(@Param('reviewId') reviewId: string, @Body() updateReviewDto: UpdateReviewDto, @UserId() userId: string) {
    return this.reviewService.update(reviewId, userId, updateReviewDto, false);
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth()
  @Delete('public/reviews/:id')
  @ApiOperation({ summary: 'Delete a review (own reviews only)' })
  @ApiHeader(API_HEADER_SLUG)
  @ApiParam({ name: 'id', required: true })
  remove(@Param('id') id: string, @UserId() userId: string) {
    return this.reviewService.remove(id, userId, false);
  }

  /********************  Admin Routes **********************/

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.MANAGE_REVIEWS)
  @Get('admin/reviews')
  @ApiOperation({ summary: 'Get all reviews (admin - including unapproved)' })
  @ApiOkResponse({ description: 'List of all reviews' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiQuery({ name: 'foodId', required: false, type: String })
  @ApiQuery({ name: 'userId', required: false, type: String })
  @ApiQuery({ name: 'status', required: false, enum: ReviewStatus })
  @ApiQuery({ name: 'orderBy', required: false, type: String })
  @ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] })
  findAllAdmin(@Query() dto: FindReviewsDto, @RestId() restId: string) {
    return this.reviewService.findAll({ ...dto, restId: restId });
  }

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.MANAGE_REVIEWS)
  @Patch('admin/reviews/:id')
  @ApiOperation({ summary: 'Update a review (admin - can change approval status)' })
  @ApiParam({ name: 'id', required: true })
  @ApiBody({ type: UpdateReviewDto })
  updateAdmin(@Param('id') id: string, @Body() updateReviewDto: UpdateReviewDto, @UserId() userId: string) {
    return this.reviewService.update(id, userId, updateReviewDto, true);
  }

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.MANAGE_REVIEWS)
  @Get('admin/reviews/:id')
  @ApiOperation({ summary: 'review detail' })
  @ApiParam({ name: 'id', required: true })
  reviewDetail(@Param('id') reviewId: string, @RestId() restaurantId: string) {
    return this.reviewService.findById(reviewId, restaurantId);
  }

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.MANAGE_REVIEWS)
  @Patch('admin/reviews/:id/status')
  @ApiOperation({ summary: 'Change review status (pending, approved, rejected)' })
  @ApiParam({ name: 'id', required: true })
  @ApiBody({ type: ChangeStatusDto })
  changeStatus(
    @Param('id') reviewId: string,
    @Body() changeStatusDto: ChangeStatusDto,
    @RestId() restaurantId: string,
  ) {
    return this.reviewService.changeStatus(reviewId, changeStatusDto.status, restaurantId);
  }

  @UseGuards(AdminAuthGuard)
  @Permissions(Permission.MANAGE_REVIEWS)
  @ApiBearerAuth()
  @Delete('admin/reviews/:id')
  @ApiOperation({ summary: 'Delete a review (admin)' })
  @ApiParam({ name: 'id', required: true })
  removeAdmin(@Param('id') id: string, @UserId() userId: string) {
    return this.reviewService.remove(id, userId, true);
  }
}
