package com.competition.modules.pub.controller;

import com.competition.common.result.PageResult;
import com.competition.common.result.Result;
import com.competition.common.util.SecurityUtil;
import com.competition.modules.pub.service.PublicContentReviewService;
import com.competition.modules.ugc.entity.UgcReviewLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "内容审核")
@RestController
@RequestMapping("/content-review")
@RequiredArgsConstructor
public class ContentReviewController {

    private final PublicContentReviewService publicContentReviewService;

    @GetMapping("/works/stats")
    @Operation(summary = "获取审核状态统计")
    public Result<Map<String, Long>> getStats() {
        return Result.success(publicContentReviewService.getStats());
    }

    @GetMapping("/works")
    @Operation(summary = "审核作品列表")
    public Result<PageResult<Map<String, Object>>> getWorkQueue(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) String sortBy,
            @RequestParam(required = false) Boolean isRecommended) {
        return Result.success(publicContentReviewService.getWorkQueue(
                page, pageSize, status, keyword, startTime, endTime, sortBy, isRecommended));
    }

    @GetMapping("/works/{id}")
    @Operation(summary = "审核作品详情")
    public Result<Map<String, Object>> getWorkDetail(@PathVariable Long id) {
        return Result.success(publicContentReviewService.getWorkDetail(id));
    }

    @PostMapping("/works/{id}/approve")
    @Operation(summary = "通过审核")
    public Result<Void> approve(@PathVariable Long id, @RequestBody(required = false) Map<String, String> body) {
        Long operatorId = SecurityUtil.getCurrentUserId();
        String note = body != null ? body.get("note") : null;
        publicContentReviewService.approve(id, note, operatorId);
        return Result.success();
    }

    @PostMapping("/works/{id}/reject")
    @Operation(summary = "驳回")
    public Result<Void> reject(@PathVariable Long id, @RequestBody Map<String, String> body) {
        Long operatorId = SecurityUtil.getCurrentUserId();
        publicContentReviewService.reject(id, body.get("reason"), body.get("note"), operatorId);
        return Result.success();
    }

    @PostMapping("/works/batch-approve")
    @Operation(summary = "批量通过")
    public Result<Void> batchApprove(@RequestBody Map<String, Object> body) {
        Long operatorId = SecurityUtil.getCurrentUserId();
        @SuppressWarnings("unchecked")
        List<Long> ids = ((List<Number>) body.get("ids")).stream().map(Number::longValue).toList();
        publicContentReviewService.batchApprove(ids, operatorId);
        return Result.success();
    }

    @PostMapping("/works/batch-reject")
    @Operation(summary = "批量驳回")
    public Result<Void> batchReject(@RequestBody Map<String, Object> body) {
        Long operatorId = SecurityUtil.getCurrentUserId();
        @SuppressWarnings("unchecked")
        List<Long> ids = ((List<Number>) body.get("ids")).stream().map(Number::longValue).toList();
        String reason = (String) body.get("reason");
        publicContentReviewService.batchReject(ids, reason, operatorId);
        return Result.success();
    }

    @PostMapping("/works/{id}/revoke")
    @Operation(summary = "撤回审核")
    public Result<Void> revoke(@PathVariable Long id) {
        Long operatorId = SecurityUtil.getCurrentUserId();
        publicContentReviewService.revoke(id, operatorId);
        return Result.success();
    }

    @PostMapping("/works/{id}/takedown")
    @Operation(summary = "下架")
    public Result<Void> takedown(@PathVariable Long id, @RequestBody Map<String, String> body) {
        Long operatorId = SecurityUtil.getCurrentUserId();
        publicContentReviewService.takedown(id, body.get("reason"), operatorId);
        return Result.success();
    }

    @PostMapping("/works/{id}/restore")
    @Operation(summary = "恢复")
    public Result<Void> restore(@PathVariable Long id) {
        Long operatorId = SecurityUtil.getCurrentUserId();
        publicContentReviewService.restore(id, operatorId);
        return Result.success();
    }

    @PostMapping("/works/{id}/recommend")
    @Operation(summary = "切换推荐状态")
    public Result<Map<String, Object>> toggleRecommend(@PathVariable Long id) {
        Long operatorId = SecurityUtil.getCurrentUserId();
        return Result.success(publicContentReviewService.toggleRecommend(id, operatorId));
    }

    @GetMapping("/management/stats")
    @Operation(summary = "管理概览统计")
    public Result<Map<String, Object>> getManagementStats() {
        return Result.success(publicContentReviewService.getManagementStats());
    }

    @GetMapping("/logs")
    @Operation(summary = "审核日志")
    public Result<PageResult<UgcReviewLog>> getLogs(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Long workId) {
        return Result.success(publicContentReviewService.getLogs(page, pageSize, workId));
    }
}
