import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  ParseIntPipe,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { YachtsService } from './yachts.service';
import { CreateYachtDto } from './dto/create-yacht.dto';
import { UpdateYachtDto } from './dto/update-yacht.dto';

@Controller('yachts')
export class YachtsController {
  constructor(private readonly yachtsService: YachtsService) {}

  @Get()
  async findAll(@Query('userId') userId?: string) {
    if (userId) {
      return this.yachtsService.findByUserId(parseInt(userId));
    }
    return this.yachtsService.findAll();
  }

  @Get(':id')
  async findOne(@Param('id', ParseIntPipe) id: number) {
    return this.yachtsService.findById(id);
  }

  @Post()
  @HttpCode(HttpStatus.CREATED)
  async create(@Body() createYachtDto: CreateYachtDto) {
    return this.yachtsService.create(createYachtDto);
  }

  @Put(':id')
  async update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateYachtDto: UpdateYachtDto,
  ) {
    return this.yachtsService.update(id, updateYachtDto);
  }

  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  async delete(@Param('id', ParseIntPipe) id: number) {
    return this.yachtsService.delete(id);
  }
}
