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 { TagGroupsService } from './tag-groups.service';
import { CreateTagGroupDto } from './dto/create-tag-group.dto';
import { UpdateTagGroupDto } from './dto/update-tag-group.dto';
import { ReorderTagGroupsDto } from './dto/reorder-tag-groups.dto';

@ApiTags('tag-groups')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('tag-groups')
export class TagGroupsController {
  constructor(private readonly service: TagGroupsService) {}

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

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

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

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

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

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