package org.springblade.modules.martial.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.martial.pojo.entity.MartialJudgeProject;
import org.springblade.modules.martial.service.IMartialJudgeProjectService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 裁判项目关联 控制器
 *
 * @author BladeX
 */
@RestController
@AllArgsConstructor
@RequestMapping("/martial/judge-project")
@Tag(name = "裁判项目管理", description = "裁判权限分配接口")
public class MartialJudgeProjectController extends BladeController {

	private final IMartialJudgeProjectService judgeProjectService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@Operation(summary = "详情", description = "传入ID")
	public R<MartialJudgeProject> detail(@RequestParam Long id) {
		MartialJudgeProject detail = judgeProjectService.getById(id);
		return R.data(detail);
	}

	/**
	 * 分页列表
	 */
	@GetMapping("/list")
	@Operation(summary = "分页列表", description = "分页查询")
	public R<IPage<MartialJudgeProject>> list(MartialJudgeProject judgeProject, Query query) {
		IPage<MartialJudgeProject> pages = judgeProjectService.page(Condition.getPage(query), Condition.getQueryWrapper(judgeProject));
		return R.data(pages);
	}

	/**
	 * Task 2.5: 批量分配裁判到项目
	 */
	@PostMapping("/assign")
	@Operation(summary = "分配裁判到项目", description = "批量分配裁判权限")
	public R assign(
		@RequestParam Long competitionId,
		@RequestParam Long projectId,
		@RequestParam String judgeIds
	) {
		List<Long> judgeIdList = Func.toLongList(judgeIds);
		judgeProjectService.assignJudgesToProject(competitionId, projectId, judgeIdList);
		return R.success("分配成功");
	}

	/**
	 * Task 2.5: 获取裁判负责的项目列表
	 */
	@GetMapping("/judge-projects")
	@Operation(summary = "裁判负责的项目", description = "获取裁判可以评分的项目列表")
	public R<List<Long>> getJudgeProjects(
		@RequestParam Long judgeId,
		@RequestParam Long competitionId
	) {
		List<Long> projectIds = judgeProjectService.getJudgeProjects(judgeId, competitionId);
		return R.data(projectIds);
	}

	/**
	 * Task 2.5: 获取项目的裁判列表
	 */
	@GetMapping("/project-judges")
	@Operation(summary = "项目的裁判列表", description = "获取负责该项目的所有裁判")
	public R<List<Long>> getProjectJudges(@RequestParam Long projectId) {
		List<Long> judgeIds = judgeProjectService.getProjectJudges(projectId);
		return R.data(judgeIds);
	}

	/**
	 * Task 2.5: 检查裁判权限
	 */
	@GetMapping("/check-permission")
	@Operation(summary = "检查裁判权限", description = "检查裁判是否有权限给项目打分")
	public R<Boolean> checkPermission(
		@RequestParam Long judgeId,
		@RequestParam Long projectId
	) {
		boolean hasPermission = judgeProjectService.hasPermission(judgeId, projectId);
		return R.data(hasPermission);
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@Operation(summary = "删除", description = "传入ID")
	public R remove(@RequestParam String ids) {
		return R.status(judgeProjectService.removeByIds(Func.toLongList(ids)));
	}

}
