import { Body, Controller, Delete, Get, Param, Post, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { PaginationQueryDto } from '../../common/dto/pagination-query.dto';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { JwtPayload } from '../../common/interfaces/jwt-payload.interface';
import { ToggleSaveDto } from './dto/toggle-save.dto';
import { SavesService } from './saves.service';

@ApiTags('Saves')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('saves')
export class SavesController {
  constructor(private readonly savesService: SavesService) {}

  @Post()
  async save(@CurrentUser() user: JwtPayload, @Body() dto: ToggleSaveDto) {
    return this.savesService.save(user.sub, dto);
  }

  @Delete(':postId')
  async unsave(@CurrentUser() user: JwtPayload, @Param('postId') postId: string) {
    return this.savesService.unsave(user.sub, { postId });
  }

  @Get('status/:postId')
  async getStatus(@CurrentUser() user: JwtPayload, @Param('postId') postId: string) {
    return this.savesService.getStatus(user.sub, { postId });
  }

  @Post('toggle')
  async toggle(@CurrentUser() user: JwtPayload, @Body() dto: ToggleSaveDto) {
    return this.savesService.toggle(user.sub, dto);
  }

  @Get('me')
  async getMySavedPosts(@CurrentUser() user: JwtPayload, @Query() query: PaginationQueryDto) {
    return this.savesService.getMySavedPosts(user.sub, query);
  }
}
