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.MartialScore;
import org.springblade.modules.martial.service.IMartialScoreService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评分记录 控制器
 *
 * @author BladeX
 */
@RestController
@AllArgsConstructor
@RequestMapping("/martial/score")
@Tag(name = "评分记录管理", description = "评分记录接口")
public class MartialScoreController extends BladeController {

	private final IMartialScoreService scoreService;

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

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

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Operation(summary = "新增或修改", description = "传入实体")
	public R submit(@RequestBody MartialScore score) {
		return R.status(scoreService.saveOrUpdate(score));
	}

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

	/**
	 * Task 2.3: 获取异常评分列表
	 */
	@GetMapping("/anomalies")
	@Operation(summary = "异常评分列表", description = "获取偏差较大的评分记录")
	public R<List<MartialScore>> getAnomalies(
		@RequestParam Long athleteId,
		@RequestParam Long projectId
	) {
		List<MartialScore> anomalies = scoreService.getAnomalyScores(athleteId, projectId);
		return R.data(anomalies);
	}

	/**
	 * Task 2.2: 批量验证评分
	 */
	@PostMapping("/validate")
	@Operation(summary = "批量验证评分", description = "验证运动员项目的所有评分是否有效")
	public R validateScores(
		@RequestParam Long athleteId,
		@RequestParam Long projectId
	) {
		boolean valid = scoreService.validateScores(athleteId, projectId);
		return valid ? R.success("所有评分有效") : R.fail("存在无效评分");
	}

}
