import { Controller, Post, Get, Patch, Param, Body, Query, UseGuards, HttpException, HttpStatus } from '@nestjs/common'
import { FeedbackService } from './feedback.service'
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard'
import { AdminGuard } from '../../common/guards/admin.guard'
import { CurrentUser } from '../../common/decorators/current-user.decorator'

@Controller('feedback')
export class FeedbackController {
  constructor(private service: FeedbackService) {}

  @UseGuards(JwtAuthGuard)
  @Post()
  async create(@CurrentUser('userId') userId: string, @Body() body: { type?: string; content: string; contact?: string }) {
    if (!body.content || body.content.length < 2) {
      throw new HttpException('请填写反馈内容', HttpStatus.BAD_REQUEST)
    }
    return this.service.create({ userId, type: body.type || 'suggestion', content: body.content, contact: body.contact })
  }

  @UseGuards(JwtAuthGuard, AdminGuard)
  @Get()
  async list(@Query('page') page?: string, @Query('limit') limit?: string) {
    return this.service.findAll(Number(page) || 1, Number(limit) || 20)
  }

  @UseGuards(JwtAuthGuard, AdminGuard)
  @Patch(':id/resolve')
  async resolve(@Param('id') id: string) {
    return this.service.markResolved(id)
  }
}
