import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  Param,
  ParseUUIDPipe,
  Patch,
  Post,
} from '@nestjs/common';
import type { AuthenticatedUser } from '../../common/auth/authenticated-user';
import { CurrentUser } from '../../common/auth/current-user.decorator';
import { CreateWatchlistDto, UpdateWatchlistDto } from './dto/watchlist.dto';
import { WatchlistsRepository } from './watchlists.repository';

@Controller('watchlists')
export class WatchlistsController {
  constructor(private readonly watchlists: WatchlistsRepository) {}

  @Get()
  list(@CurrentUser() user: AuthenticatedUser) {
    return this.watchlists.list(user.id);
  }

  @Post()
  create(@CurrentUser() user: AuthenticatedUser, @Body() input: CreateWatchlistDto) {
    return this.watchlists.create(user.id, input);
  }

  @Patch(':watchlistId')
  update(
    @CurrentUser() user: AuthenticatedUser,
    @Param('watchlistId', ParseUUIDPipe) watchlistId: string,
    @Body() input: UpdateWatchlistDto,
  ) {
    return this.watchlists.update(user.id, watchlistId, input);
  }

  @Delete(':watchlistId')
  @HttpCode(204)
  delete(
    @CurrentUser() user: AuthenticatedUser,
    @Param('watchlistId', ParseUUIDPipe) watchlistId: string,
  ) {
    return this.watchlists.delete(user.id, watchlistId);
  }
}
