import {
  Controller, Get, Post, Param, Query, Body,
  ParseIntPipe, UseGuards, Request,
} from '@nestjs/common';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { ContentReviewService } from './content-review.service';

@Controller('content-review')
@UseGuards(JwtAuthGuard)
export class ContentReviewController {
  constructor(private readonly reviewService: ContentReviewService) {}

  // ========== 作品审核 ==========

  @Get('works/stats')
  getWorkStats() {
    return this.reviewService.getWorkStats();
  }

  @Get('works')
  getWorkQueue(
    @Query('page') page?: string,
    @Query('pageSize') pageSize?: string,
    @Query('status') status?: string,
    @Query('keyword') keyword?: string,
    @Query('startTime') startTime?: string,
    @Query('endTime') endTime?: string,
  ) {
    return this.reviewService.getWorkQueue({
      page: page ? parseInt(page) : 1,
      pageSize: pageSize ? parseInt(pageSize) : 10,
      status,
      keyword,
      startTime,
      endTime,
    });
  }

  @Get('works/:id')
  getWorkDetail(@Param('id', ParseIntPipe) id: number) {
    return this.reviewService.getWorkDetail(id);
  }

  @Post('works/:id/approve')
  approveWork(
    @Param('id', ParseIntPipe) id: number,
    @Request() req,
    @Body() dto: { note?: string },
  ) {
    return this.reviewService.approve(id, req.user.userId, dto.note);
  }

  @Post('works/:id/reject')
  rejectWork(
    @Param('id', ParseIntPipe) id: number,
    @Request() req,
    @Body() dto: { reason: string; note?: string },
  ) {
    return this.reviewService.reject(id, req.user.userId, dto.reason, dto.note);
  }

  @Post('works/:id/takedown')
  takedownWork(
    @Param('id', ParseIntPipe) id: number,
    @Request() req,
    @Body() dto: { reason: string },
  ) {
    return this.reviewService.takedown(id, req.user.userId, dto.reason);
  }

  @Post('works/:id/restore')
  restoreWork(@Param('id', ParseIntPipe) id: number, @Request() req) {
    return this.reviewService.restore(id, req.user.userId);
  }

  @Post('works/:id/recommend')
  toggleRecommend(@Param('id', ParseIntPipe) id: number) {
    return this.reviewService.toggleRecommend(id);
  }

  // ========== 作品管理 ==========

  @Get('management/stats')
  getManagementStats() {
    return this.reviewService.getManagementStats();
  }

  // ========== 审核日志 ==========

  @Get('logs')
  getLogs(
    @Query('page') page?: string,
    @Query('pageSize') pageSize?: string,
    @Query('workId') workId?: string,
  ) {
    return this.reviewService.getLogs({
      page: page ? parseInt(page) : 1,
      pageSize: pageSize ? parseInt(pageSize) : 20,
      workId: workId ? parseInt(workId) : undefined,
    });
  }
}
