import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
} from '@nestjs/common'
import { SearchFilter, SearchParams, SearchResult } from '@csr/common/entities'
import { WeightClassService } from '../services/weight.class.service'
import { WeightClass } from '../entities/weight.class'

import { WeightClassDto } from '../dtos/weight.class.dto'
import { ApiTags } from '@nestjs/swagger'

@ApiTags('Setting')
@Controller('weightClass')
export class WeightClassController {
  constructor(private readonly weightClassService: WeightClassService) {}

  @Get()
  async search(
    @Query('page') page,
    @Query('limit') limit,
  ): Promise<SearchResult<WeightClass>> {
    page = page || 1
    limit = Math.min(limit || 10, 100)

    const filters: SearchFilter[] = []
    const params = new SearchParams(filters, page, limit)
    return await this.weightClassService.search(params)
  }

  @Get(':id')
  async get(@Param('id') id: number): Promise<WeightClass> {
    const weightClass = await this.weightClassService.get(id)
    return weightClass
  }

  @Post()
  async create(@Body() weightClassDto: WeightClassDto): Promise<WeightClass> {
    const weightClass = await this.weightClassService.create(weightClassDto)
    return weightClass
  }

  @Put(':id')
  async update(
    @Param('id')
    id: number,
    @Body() weightClassDto: WeightClassDto,
  ): Promise<WeightClass> {
    const weightClass = await this.weightClassService.update(id, weightClassDto)
    return weightClass
  }

  @Delete(':id')
  async delete(@Param('id') id: number): Promise<boolean> {
    await this.weightClassService.delete(id)
    return true
  }
}
