import { Controller, Get, Post, Delete, Body, Param, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { ContentSafetyService } from './content-safety.service';
import { AdminAuthGuard } from '../../common/guards/admin-auth.guard';
import { AdminRolesGuard } from '../../common/guards/admin-roles.guard';
import { AdminRoles } from '../../common/decorators/admin-roles.decorator';
import { Public } from '../../common/decorators/public.decorator';
import type { AdminRole } from '../../common/types/admin-role.enum';

@ApiTags('content-safety')
@Controller()
export class ContentSafetyController {
  constructor(private readonly svc: ContentSafetyService) {}

  // ═══ CAPI: User report submission ═══

  @Public()
  @Post('reports')
  @ApiOperation({ summary: '用户提交举报' })
  async submitReport(@Body() d: { targetType: string; targetId: string; reason: string; reporterId?: string }) {
    const report = await this.svc.submitReport({
      reporterId: d.reporterId || 'anonymous',
      targetType: d.targetType,
      targetId: d.targetId,
      reason: d.reason,
    });
    return { success: true, data: report };
  }
}

@ApiTags('admin-content-safety')
@Controller('admin-api/content-safety')
@UseGuards(AdminAuthGuard, AdminRolesGuard)
@ApiBearerAuth()
export class AdminContentSafetyController {
  constructor(private readonly svc: ContentSafetyService) {}

  // ── Sensitive words ──

  @Get('words') @AdminRoles('SUPER_ADMIN' as AdminRole)
  async words() { return this.svc.getAllWords() }

  @Post('words') @AdminRoles('SUPER_ADMIN' as AdminRole)
  async addWord(@Body() d: { word: string; category: string; riskLevel: string }) { return this.svc.addWord(d.word, d.category, d.riskLevel) }

  @Delete('words/:id') @AdminRoles('SUPER_ADMIN' as AdminRole)
  async removeWord(@Param('id') id: string) { await this.svc.removeWord(id); return { success: true } }

  // ── AI safety checks log ──

  @Get('checks') @AdminRoles('SUPER_ADMIN' as AdminRole)
  async checks() { return this.svc.getChecks() }

  // ── Reports ──

  @Get('reports') @AdminRoles('ADMIN' as AdminRole)
  @ApiOperation({ summary: '举报列表' })
  async reports(@Query('status') status?: string) {
    return this.svc.getReports(status);
  }

  @Post('reports/:id/handle') @AdminRoles('ADMIN' as AdminRole)
  @ApiOperation({ summary: '处理举报' })
  async handleReport(@Param('id') id: string, @Body() d: { action: string; note?: string }) {
    return this.svc.handleReport(id, d.action, d.note);
  }

  // ── Violations ──

  @Get('violations') @AdminRoles('ADMIN' as AdminRole)
  @ApiOperation({ summary: '违规记录列表' })
  async violations(@Query('userId') userId?: string, @Query('limit') limit = '50') {
    return this.svc.getViolations(userId, parseInt(limit));
  }

  @Post('violations/:id/penalty') @AdminRoles('SUPER_ADMIN' as AdminRole)
  @ApiOperation({ summary: '应用处罚' })
  async applyPenalty(@Param('id') id: string, @Body() d: { penalty: string }) {
    return this.svc.applyPenalty(id, d.penalty);
  }
}
