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

import { LengthClassDto } from '../dtos/length.class.dto'
import { ApiTags } from '@nestjs/swagger'

@ApiTags('Setting')
@Controller('lengthClass')
export class LengthClassController {
  constructor(private readonly lengthClassService: LengthClassService) {}

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

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

  @Get(':id')
  async get(@Param('id') id: number): Promise<LengthClass> {
    const lengthClass = await this.lengthClassService.get(id)
    return lengthClass
  }

  @Post()
  async create(@Body() lengthClassDto: LengthClassDto): Promise<LengthClass> {
    const lengthClass = await this.lengthClassService.create(lengthClassDto)
    return lengthClass
  }

  @Put(':id')
  async update(
    @Param('id')
    id: number,
    @Body() lengthClassDto: LengthClassDto,
  ): Promise<LengthClass> {
    const lengthClass = await this.lengthClassService.update(id, lengthClassDto)
    return lengthClass
  }

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