package com.lesingle.modules.biz.judge.controller;

import com.lesingle.common.result.PageResult;
import com.lesingle.common.result.Result;
import com.lesingle.common.util.SecurityUtil;
import com.lesingle.modules.biz.judge.service.IJudgesManagementService;
import com.lesingle.security.annotation.RequirePermission;
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("/judges-management")
@RequiredArgsConstructor
public class JudgesManagementController {

    private final IJudgesManagementService judgesManagementService;

    @PostMapping
    @RequirePermission("judge:create")
    @Operation(summary = "创建评委账号")
    public Result<Map<String, Object>> create(@RequestBody Map<String, Object> body) {
        return Result.success(judgesManagementService.createJudge(body));
    }

    @GetMapping
    @RequirePermission("judge:read")
    @Operation(summary = "查询评委列表",
            description = "status：enabled/disabled，不传则返回全部状态。向活动添加评委、评审进度等「仅用于选择评委」的场景请传 status=enabled，不包含停用账号。")
    public Result<PageResult<Map<String, Object>>> findAll(
            @RequestParam(defaultValue = "1") Long page,
            @RequestParam(defaultValue = "10") Long pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status) {
        return Result.success(judgesManagementService.findAll(page, pageSize, keyword, status));
    }

    @GetMapping("/{id}")
    @RequirePermission("judge:read")
    @Operation(summary = "查询评委详情")
    public Result<Map<String, Object>> findDetail(@PathVariable Long id) {
        return Result.success(judgesManagementService.findDetail(id));
    }

    @PatchMapping("/{id}")
    @RequirePermission("judge:update")
    @Operation(summary = "更新评委信息")
    public Result<Map<String, Object>> update(@PathVariable Long id, @RequestBody Map<String, Object> body) {
        return Result.success(judgesManagementService.updateJudge(id, body));
    }

    @PatchMapping("/{id}/freeze")
    @RequirePermission("judge:update")
    @Operation(summary = "冻结评委账号")
    public Result<Void> freeze(@PathVariable Long id) {
        judgesManagementService.freeze(id);
        return Result.success();
    }

    @PatchMapping("/{id}/unfreeze")
    @RequirePermission("judge:update")
    @Operation(summary = "解冻评委账号")
    public Result<Void> unfreeze(@PathVariable Long id) {
        judgesManagementService.unfreeze(id);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @RequirePermission("judge:delete")
    @Operation(summary = "删除评委")
    public Result<Void> remove(@PathVariable Long id) {
        judgesManagementService.remove(id);
        return Result.success();
    }

    @PostMapping("/batch-delete")
    @RequirePermission("judge:delete")
    @Operation(summary = "批量删除评委")
    @SuppressWarnings("unchecked")
    public Result<Void> batchDelete(@RequestBody Map<String, Object> body) {
        List<Long> ids = ((List<Number>) body.get("ids"))
                .stream().map(Number::longValue).toList();
        judgesManagementService.batchDelete(ids);
        return Result.success();
    }
}
