package com.dora.controller;

import com.dora.common.Result;
import com.dora.dto.PlazaWorkReportDto;
import com.dora.service.PlazaWorkReportService;
import com.dora.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 广场作品投诉管理控制器（管理员端）
 * 
 * @author 1818AI
 * @since 2025-11-14
 */
@Slf4j
@RestController
@RequestMapping("/admin/plaza/reports")
@Tag(name = "广场作品投诉管理", description = "管理员端API - 审核和处理广场作品投诉")
@RequiredArgsConstructor
public class AdminPlazaWorkReportController {

    private final PlazaWorkReportService reportService;

    /**
     * 查询投诉列表
     */
    @GetMapping("/list")
    @Operation(
        summary = "查询投诉列表", 
        description = "管理员查询投诉列表，支持按状态、类型、作品编号、投诉人筛选"
    )
    public Result<PlazaWorkReportDto.AdminReportListResponse> getReportList(
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") Integer page,
            
            @Parameter(description = "每页数量", example = "20")
            @RequestParam(defaultValue = "20") Integer size,
            
            @Parameter(description = "投诉状态筛选：pending-待审核，approved-投诉成立，rejected-投诉不成立")
            @RequestParam(required = false) String reportStatus,
            
            @Parameter(description = "投诉类型筛选")
            @RequestParam(required = false) String reportType,
            
            @Parameter(description = "作品编号")
            @RequestParam(required = false) String workNo,
            
            @Parameter(description = "投诉人用户ID")
            @RequestParam(required = false) Long reporterId) {
        
        try {
            PlazaWorkReportDto.AdminReportQueryRequest request = PlazaWorkReportDto.AdminReportQueryRequest.builder()
                    .page(page)
                    .size(size)
                    .reportStatus(reportStatus)
                    .reportType(reportType)
                    .workNo(workNo)
                    .reporterId(reporterId)
                    .build();
            
            PlazaWorkReportDto.AdminReportListResponse response = reportService.getAdminReportList(request);
            return Result.success(response);
        } catch (Exception e) {
            log.error("查询投诉列表失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 审核投诉
     */
    @PostMapping("/audit")
    @Operation(
        summary = "审核投诉", 
        description = "管理员审核投诉，投诉成立则下架作品，投诉不成立则驳回。需要填写处理备注。"
    )
    public Result<Boolean> auditReport(
            @Valid @RequestBody PlazaWorkReportDto.AuditReportRequest request) {
        try {
            Long adminId = SecurityUtil.getCurrentUserId();
            String adminName = SecurityUtil.getUsername();
            
            boolean success = reportService.auditReport(adminId, adminName, request);
            return Result.success(success);
        } catch (Exception e) {
            log.error("审核投诉失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取待审核投诉数量
     */
    @GetMapping("/pending/count")
    @Operation(
        summary = "获取待审核投诉数量", 
        description = "获取待审核的投诉数量，用于显示待处理提醒"
    )
    public Result<Long> getPendingReportCount() {
        try {
            Long count = reportService.getPendingReportCount();
            return Result.success(count);
        } catch (Exception e) {
            log.error("获取待审核投诉数量失败", e);
            return Result.error(e.getMessage());
        }
    }
}

