import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Param, Post, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { Throttle } from '../../common/decorators/throttle.decorator';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { JwtPayload } from '../../common/interfaces/jwt-payload.interface';
import { TrackEngagementBatchDto, TrackEngagementDto } from './dto/track-engagement.dto';
import { EngagementService } from './engagement.service';

@ApiTags('Engagement')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('engagement')
export class EngagementController {
  constructor(private readonly engagementService: EngagementService) {}

  @Post('events') @HttpCode(HttpStatus.ACCEPTED) @Throttle(240, 60_000)
  track(@CurrentUser() user: JwtPayload, @Body() dto: TrackEngagementDto) {
    return this.engagementService.track(user.sub, dto);
  }

  @Post('events/batch') @HttpCode(HttpStatus.ACCEPTED) @Throttle(30, 60_000)
  trackBatch(@CurrentUser() user: JwtPayload, @Body() dto: TrackEngagementBatchDto) {
    return this.engagementService.trackBatch(user.sub, dto.events);
  }

  @Post('posts/:postId/not-interested')
  hide(@CurrentUser() user: JwtPayload, @Param('postId') postId: string) {
    return this.engagementService.setNotInterested(user.sub, postId, true);
  }

  @Delete('posts/:postId/not-interested')
  restore(@CurrentUser() user: JwtPayload, @Param('postId') postId: string) {
    return this.engagementService.setNotInterested(user.sub, postId, false);
  }

  @Get('me/summary')
  summary(@CurrentUser() user: JwtPayload) { return this.engagementService.getMySummary(user.sub); }
}
