import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { CategoryService } from '../providers/category.service';
import { CreateCategoryDto } from '../dto/create-category.dto';
import { UpdateCategoryDto } from '../dto/update-category.dto';
import {
  ApiTags,
  ApiOperation,
  ApiCreatedResponse,
  ApiOkResponse,
  ApiNotFoundResponse,
  ApiParam,
  ApiBody,
  ApiBearerAuth,
  ApiHeader,
} from '@nestjs/swagger';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { UseGuards } from '@nestjs/common';
import { RestId } from 'src/common/decorators';
import { Permission } from 'src/common/enums/permission.enum';
import { Permissions } from 'src/common/decorators/permissions.decorator';
import { API_HEADER_SLUG } from 'src/common/constants';

@ApiTags('category')
@Controller()
export class CategoryController {
  constructor(private readonly categoryService: CategoryService) { }

  @Get('public/categories/restaurant/:slug')
  @ApiOperation({ summary: 'Get all categories by restaurant slug' })
  @ApiHeader(API_HEADER_SLUG)
  @ApiParam({ name: 'slug', required: true, description: 'Restaurant Slug' })
  @ApiOkResponse({ description: 'List of all categories for the restaurant' })
  findAllByRestaurant(@Param('slug') slug: string) {
    return this.categoryService.findAllByRestaurant(slug);
  }

  /*** Admin ***/

  @ApiOperation({ summary: 'Create category' })
  @ApiBody({ type: CreateCategoryDto })
  @Permissions(Permission.MANAGE_CATEGORIES)
  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Post('admin/categories')
  create(@Body() dto: CreateCategoryDto, @RestId() restId: string) {
    return this.categoryService.create(restId, dto);
  }
  @Permissions(Permission.MANAGE_CATEGORIES)
  @ApiOperation({ summary: 'my restaurant categories' })
  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Get('admin/categories')
  findAll(@RestId() restId: string) {
    return this.categoryService.findAllByRestaurantId(restId);
  }

  @Permissions(Permission.MANAGE_CATEGORIES)
  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Get('admin/categories/:id')
  @ApiOperation({ summary: 'Get category' })
  @ApiParam({ name: 'id', required: true, type: String })
  findOne(@Param('id') id: string, @RestId() restId: string) {
    return this.categoryService.findOneOrFail(restId, id);
  }

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.MANAGE_CATEGORIES)
  @Patch('admin/categories/:id')
  @ApiOperation({ summary: 'Update category' })
  @ApiParam({ name: 'id' })
  @ApiBody({ type: UpdateCategoryDto })
  update(@Param('id') id: string, @Body() dto: UpdateCategoryDto, @RestId() restId: string) {
    return this.categoryService.update(restId, id, dto);
  }

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.MANAGE_CATEGORIES)
  @Delete('admin/categories/:id')
  @ApiOperation({ summary: 'Delete category' })
  @ApiParam({ name: 'id' })
  @ApiOkResponse({ description: 'Deleted' })
  remove(@Param('id') id: string, @RestId() restId: string) {
    return this.categoryService.remove(restId, id);
  }

}
