import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
import { FoodService } from '../providers/food.service';
import { CreateFoodDto } from '../dto/create-food.dto';
import { UpdateFoodDto } from '../dto/update-food.dto';
import { FindFoodsDto } from '../dto/find-foods.dto';
import {
  ApiTags,
  ApiOperation,
  ApiQuery,
  ApiBody,
  ApiParam,
  ApiBearerAuth,
  ApiHeader,
} from '@nestjs/swagger';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { RestId, UserId } from 'src/common/decorators';
import { AuthGuard } from 'src/modules/auth/guards/auth.guard';
import { OptionalAuthGuard } from 'src/modules/auth/guards/optinalAuth.guard';
import { API_HEADER_SLUG } from 'src/common/constants';
import { Permission } from 'src/common/enums/permission.enum';
import { Permissions } from 'src/common/decorators/permissions.decorator';

@ApiTags('foods')
@Controller()
export class FoodController {
  constructor(private readonly foodsService: FoodService) { }

  @Get('public/foods/restaurant/:slug')
  @ApiOperation({ summary: 'Get all foods by restaurant slug' })
  @ApiHeader(API_HEADER_SLUG)
  @ApiParam({ name: 'slug', required: true, description: 'Restaurant Slug' })
  findAllByRestaurant(@Param('slug') slug: string) {
    return this.foodsService.findByRestuarantSlug(slug);
  }

  @Get('public/foods/:foodId')
  @UseGuards(OptionalAuthGuard)
  @ApiHeader(API_HEADER_SLUG)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get a food by id' })
  @ApiParam({ name: 'foodId', required: true })
  findPublicFoodById(@Param('foodId') foodId: string, @UserId() userId?: string): Promise<any> {
    const a = this.foodsService.findByIdPublic(foodId, userId);
    return a;
  }

  @Post('public/foods/favorite/:foodId')
  @UseGuards(AuthGuard)
  @ApiHeader(API_HEADER_SLUG)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'toggle a food as favorite' })
  @ApiParam({ name: 'foodId', required: true })
  setAsFavorute(@Param('foodId') foodId: string, @UserId() userId: string) {
    return this.foodsService.toggleFavorite(userId, foodId);
  }

  @Get('public/foods/favorite')
  @UseGuards(AuthGuard)
  @ApiHeader(API_HEADER_SLUG)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'get my favorites' })
  getMyFavorites(@UserId() userId: string, @RestId() restId: string) {
    return this.foodsService.getMyFavorites(userId, restId);
  }

  /* ---------------------------------- Admin ---------------------------------- */
  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.MANAGE_FOODS)
  @Post('admin/foods')
  @ApiOperation({ summary: 'Create a new food' })
  @ApiBody({ type: CreateFoodDto })
  create(@Body() createFoodDto: CreateFoodDto, @RestId() restId: string) {
    return this.foodsService.create(restId, createFoodDto);
  }

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.MANAGE_FOODS)
  @Get('admin/foods')
  @ApiOperation({ summary: 'Get a paginated list of foods' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiQuery({ name: 'search', required: false, type: String })
  @ApiQuery({ name: 'orderBy', required: false, type: String })
  @ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] })
  @ApiQuery({ name: 'categoryId', required: false, type: String })
  @ApiQuery({ name: 'isActive', required: false, type: Boolean })
  async findAll(@Query() dto: FindFoodsDto, @RestId() restId: string) {
    const result = await this.foodsService.findAll(restId, dto);
    return result;
  }

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.MANAGE_FOODS)
  @Get('admin/foods/:id')
  @ApiOperation({ summary: 'Get a food by id' })
  @ApiParam({ name: 'id', required: true })
  findById(@Param('id') id: string, @RestId() restId: string) {
    return this.foodsService.findAdminById(restId, id);
  }

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.MANAGE_FOODS)
  @Patch('admin/foods/:id')
  @ApiOperation({ summary: 'Update a food' })
  @ApiParam({ name: 'id', required: true })
  @ApiBody({ type: UpdateFoodDto })
  update(@Param('id') id: string, @Body() updateFoodDto: UpdateFoodDto, @RestId() restId: string) {
    return this.foodsService.update(restId, id, updateFoodDto);
  }
  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.MANAGE_FOODS)
  @Delete('admin/foods/:id')
  @ApiOperation({ summary: 'Delete (soft) a food' })
  @ApiParam({ name: 'id', required: true })
  remove(@Param('id') id: string, @RestId() restId: string) {
    return this.foodsService.remove(restId, id);
  }
}
