import {
  Controller,
  Get,
  Post,
  Body,
  Query,
  Put,
  Patch,
  Delete,
  Param,
} from '@nestjs/common';
import { TagsService } from './tags.service';
import { Tags } from './entities/tags.entity';
import { Public } from '../auth/public.decorator';
import {
  ApiBearerAuth,
  ApiTags,
  ApiBody,
  ApiParam,
  ApiQuery,
  ApiOperation,
} from '@nestjs/swagger';

@ApiTags('tags')
@ApiBearerAuth('access-token')
@Controller('tags')
export class TagsController {
  constructor(private readonly tagsService: TagsService) {}

  // Get all tags
  @Public()
  @Get()
  @ApiOperation({ summary: 'Get all tags' })
  async findAll(): Promise<Tags[]> {
    return this.tagsService.findAll();
  }

  @Public()
  @Get('filtered')
  @ApiOperation({ summary: 'Get Tags Paginated' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  findAllPaginated(
    @Query('page') page: number = 1,
    @Query('limit') limit: number = 10,
  ) {
    return this.tagsService.findAllPaginated(
      Number(page || 1),
      Number(limit || 10),
    );
  }

  // Create a new tag
  @Public()
  @Post()
  @ApiOperation({ summary: 'Create Tag' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: { name: { type: 'string' } },
      required: ['name'],
    },
  })
  async create(@Body() body: any): Promise<Tags> {
    return this.tagsService.create(body.name);
  }

  // Create tag if not exists
  @Public()
  @Post('create_if_not_exists')
  @ApiOperation({ summary: 'Create Tag if Not Exists' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: { name: { type: 'string' } },
      required: ['name'],
    },
  })
  async createIfNotExists(@Body() body: any): Promise<Tags> {
    return this.tagsService.createIfNotExists(body.name);
  }

  @Public()
  @Put(':id')
  @ApiOperation({ summary: 'Update Tag' })
  @ApiParam({ name: 'id', type: Number })
  @ApiBody({
    schema: { type: 'object', properties: { name: { type: 'string' } } },
  })
  update(@Param('id') id: number, @Body() body: any) {
    return this.tagsService.update(id, body);
  }

  @Public()
  @Patch(':id')
  @ApiOperation({ summary: 'Patch Tag' })
  @ApiParam({ name: 'id', type: Number })
  @ApiBody({
    schema: { type: 'object', properties: { name: { type: 'string' } } },
  })
  patch(@Param('id') id: number, @Body() body: any) {
    return this.tagsService.update(id, body);
  }

  @Public()
  @Delete(':id')
  @ApiOperation({ summary: 'Delete tag' })
  @ApiParam({ name: 'id', type: Number })
  remove(@Param('id') id: number) {
    return this.tagsService.remove(id);
  }
}
