import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseIntPipe,
  Patch,
  Post,
  UseGuards,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiTags,
} from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { TagsService } from './tags.service';
import { CreateTagDto } from './dto/create-tag.dto';
import { UpdateTagDto } from './dto/update-tag.dto';
import { ReorderTagsDto } from './dto/reorder-tags.dto';

@ApiTags('tags')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('tags')
export class TagsController {
  constructor(private readonly service: TagsService) {}

  @Get()
  @ApiOperation({ summary: 'List all tags' })
  findAll() {
    return this.service.findAll();
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get one tag' })
  findOne(@Param('id', ParseIntPipe) id: string) {
    return this.service.findOne(BigInt(id));
  }

  @Post()
  @ApiOperation({ summary: 'Create a tag' })
  create(@Body() dto: CreateTagDto) {
    return this.service.create(dto);
  }

  // IMPORTANT: must come BEFORE ':id' to avoid being captured as id
  @Patch('sort')
  @ApiOperation({ summary: 'Batch update sort order and/or group assignment' })
  reorder(@Body() dto: ReorderTagsDto) {
    return this.service.reorder(dto);
  }

  @Patch(':id')
  @ApiOperation({ summary: 'Update a tag' })
  update(
    @Param('id', ParseIntPipe) id: string,
    @Body() dto: UpdateTagDto,
  ) {
    return this.service.update(BigInt(id), dto);
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Delete a tag' })
  remove(@Param('id', ParseIntPipe) id: string) {
    return this.service.remove(BigInt(id));
  }
}
